Commits

David Keegan committed 1c8b596

Localize 2.0 - a more powerful function that takes one 'from language' and an array of 'to languages' and nibs. It also wraps the strings generation from the 'from language'. On my mac pro(2.66 GHz Core 2, 4 GB ram) generating everything takes 5.42 seconds, and 0.02 seconds when nothing has to be done.

  • Participants
  • Parent commits 6711775

Comments (0)

Files changed (3)

 {
-    "German.lproj/Projects.xib": "3d7c108b95ff7436ecfa6bcb6932edb9", 
-    "German.lproj/MainMenu.xib": "012d51d8ee26799d88614c77de7613ae", 
-    "French.lproj/Repo.xib": "bb20d4c8074da7465e354543815e83d4", 
-    "French.lproj/MainMenu.xib": "012d51d8ee26799d88614c77de7613ae", 
-    "German.lproj/Repo.xib": "bb20d4c8074da7465e354543815e83d4", 
-    "French.lproj/Projects.xib": "3d7c108b95ff7436ecfa6bcb6932edb9"
+    "English.lproj/MainMenu.xib": "012d51d8ee26799d88614c77de7613ae", 
+    "English.lproj/Projects.xib": "3d7c108b95ff7436ecfa6bcb6932edb9", 
+    "English.lproj/Repo.xib": "bb20d4c8074da7465e354543815e83d4"
 }
 #!/usr/bin/python
-
+import time
 import os, sys
 import subprocess
 from optparse import OptionParser
     md5Sum = runCommand('/usr/bin/openssl md5', file)
     return md5Sum.split('=')[1].strip()
     
-def translateNib(fromFile, toFile):
-    '''create a nib from another nib and strings file'''
-    print 'Localizing', toFile
-    #get the md5 of the fromFile to see if it has been updated
+def generateStringsFile(nibFile):
+    '''generate a .strings file from a nib'''
+    nibFileStrings = nibFile.rstrip('.xib')+'.strings'
+    print '  ', nibFileStrings, 'updated'
+    return runCommand('ibtool', '--generate-strings-file %s %s' % (nibFileStrings, nibFile))
+    
+def writeNib(fromFile, toFile):
+    '''convert one localized nib from one language to another'''
+    toStrings = toFile.rstrip('.xib')+'.strings'
+    print '  ', toFile, 'updated'
+    return runCommand('ibtool', '--strings-file %s --write %s %s' % (toStrings, toFile, fromFile))
+ 
+def localizeNibs(fromLang, toLangs, nibs):
+    '''Localize nibs from one language to others'''
+    
+    #get the data from the json file
     localizeJson = os.path.abspath('Localize.json')
     if os.path.isfile(localizeJson):
         localizeData = open(localizeJson, 'r')
         localizeData.close()
     else:
         jsonData = {}
-    
-    #if the fromFile is not in the jsonData or the md5 is different 
-    #generate a new nib and update the jsonData
-    fromFileMd5 = md5(fromFile)
-    if not(toFile in jsonData) or jsonData[toFile] != fromFileMd5:
-        #generate nib
-        toStrings = toFile.rstrip('.xib')+'.strings'
-        runCommand('ibtool', '--strings-file %s --write %s %s' % (toStrings, toFile, fromFile))
-        print '    ', toFile, 'updated'
         
-        #update Localize.json
-        jsonData[toFile] = fromFileMd5
-        localizeData = open(localizeJson, 'w')
-        json.dump(jsonData, localizeData, indent=4)
-        localizeData.close()
+    fromLangLproj = fromLang+'.lproj'
+    for eachNib in nibs:
+        fromNib = os.path.join(fromLangLproj, eachNib+'.xib')
+        #if the fromFile is not in the jsonData or the md5 is different
+        fromNibMd5 = md5(fromNib)
+        if not(fromNib in jsonData) or jsonData[fromNib] != fromNibMd5:
+            for eachToLang in toLangs:
+                toLangLproj = eachToLang+'.lproj'
+                toNib =  os.path.join(toLangLproj, eachNib+'.xib')
+                generateStringsFile(fromNib)
+                writeNib(fromNib, toNib)
+                jsonData[fromNib] = fromNibMd5
+            
+    #update Localize.json
+    localizeData = open(localizeJson, 'w')
+    json.dump(jsonData, localizeData, indent=4)
+    localizeData.close()
     
 if __name__ == '__main__':
     '''Command line options'''
     
+    startTime = time.time()
+    
     opts = OptionParser()
-    opts.add_option('--write', '-w', dest='write', help='write to this nib file', metavar='NIB')
+    opts.add_option('--from', '-f', dest='fromLang', help='the language to localize from', metavar='LANG')
+    opts.add_option('--to', '-t', dest='toLangs', help='the languages to localize to', metavar='LANG')
+    opts.add_option('--nibs', '-n', dest='nibs', help='the nibs to localize', metavar='LANG')
     options, arguments = opts.parse_args()
     
-    if len(arguments) == 0:
-        sys.stderr.write('no filepath provided')
+    localizeNibs(options.fromLang, options.toLangs.split('|'), options.nibs.split('|'))
     
-    else:
-        translateNib(arguments[0], options.write)
+    print 'Nibs updated in %.2f seconds' % (time.time()-startTime)

Murky.xcodeproj/project.pbxproj

 			);
 			runOnlyForDeploymentPostprocessing = 0;
 			shellPath = /bin/sh;
-			shellScript = "# Extract English strings (use this to check if you added new strings to your nibs)\nibtool --generate-strings-file English.lproj/MainMenu.strings English.lproj/MainMenu.xib\nibtool --generate-strings-file English.lproj/Projects.strings English.lproj/Projects.xib\nibtool --generate-strings-file English.lproj/Repo.strings English.lproj/Repo.xib\n\n# Generate French interface\n${SRCROOT}/Localize.py --write French.lproj/MainMenu.xib English.lproj/MainMenu.xib\n${SRCROOT}/Localize.py --write French.lproj/Projects.xib English.lproj/Projects.xib\n${SRCROOT}/Localize.py --write French.lproj/Repo.xib English.lproj/Repo.xib\n\n# Generate German interface\n${SRCROOT}/Localize.py --write German.lproj/MainMenu.xib English.lproj/MainMenu.xib\n${SRCROOT}/Localize.py --write German.lproj/Projects.xib English.lproj/Projects.xib\n${SRCROOT}/Localize.py --write German.lproj/Repo.xib English.lproj/Repo.xib\n";
+			shellScript = "# Localize nibs\n${SRCROOT}/Localize.py --from English --to \"French|German\" --nibs \"MainMenu|Projects|Repo\"\n";
 		};
 /* End PBXShellScriptBuildPhase section */