Commits

David Keegan committed 582ab9d

refining the logic for when nibs and strings need to be updated

Comments (0)

Files changed (1)

     md5Sum = runCommand('/usr/bin/openssl md5', file)
     return md5Sum.split('=')[1].strip()
     
+def langProjName(language):
+    return language.strip()+'.lproj'
+    
+def nibToStringFileName(nibFile):
+    return nibFile.rstrip('.xib')+'.strings'
+    
 def generateStringsFile(nibFile, utf8=False):
     '''
     Generate a .strings file from a nib
     If utf8 is True the .strings files will be re-encoded as utf-8
     '''
-    nibFileStrings = nibFile.rstrip('.xib')+'.strings'
+    nibFileStrings = nibToStringFileName(nibFile)
     runCommand('ibtool', '--generate-strings-file %s %s' % (nibFileStrings, nibFile))
     
     if utf8:
     
 def writeNib(fromFile, toFile, utf8=False):
     '''convert one localized nib from one language to another'''
-    toStrings = toFile.rstrip('.xib')+'.strings'
+    toStrings = nibToStringFileName(toFile)
     runCommand('ibtool', '--strings-file %s --write %s %s' % (toStrings, toFile, fromFile))
     
     if utf8:
  
 def genStrings(toLangs, globString, utf8=False):
     for eachToLang in toLangs:
-        toLangLproj = eachToLang.strip()+'.lproj'
+        toLangLproj = langProjName(eachToLang)
         runCommand('genstrings', '-o %s %s' % (toLangLproj, globString))
         localizableStrings = os.path.join(toLangLproj, 'Localizable.strings')
         if utf8:
     else:
         jsonData = {}
         
-    fromLangLproj = fromLang.strip()+'.lproj'
-    for eachToLang in toLangs:
-        toLangLproj = eachToLang.strip()+'.lproj'
-        for eachNib in nibs:
-            eachNib = eachNib.strip()
-            if not(eachNib.endswith('.xib')):
-                eachNib += '.xib'
-            fromNib = os.path.join(fromLangLproj, eachNib)
+    fromLangLproj = langProjName(fromLang)
+    for eachNib in nibs:
+        eachNib = eachNib.strip()
+        if not(eachNib.endswith('.xib')):
+            eachNib += '.xib'
+        fromNib = os.path.join(fromLangLproj, eachNib)
+        
+        #get md5 and update the json data
+        fromNibMd5 = md5(fromNib)
+        #check if the strings for the fromNib need to the updated
+        if not(os.path.isfile(nibToStringFileName(fromNib))) or not(fromNib in jsonData) or jsonData[fromNib] != fromNibMd5:
+            generateStringsFile(fromNib, utf8)
+        
+        #write the localized nibs
+        for eachToLang in toLangs:
+            toLangLproj = langProjName(eachToLang)
             toNib =  os.path.join(toLangLproj, eachNib)
-            fromNibMd5 = md5(fromNib)
-            
-            #if the 'to nib' does not exist, or the 'from nib' is not in the json data, or the checksum does not match
             if not(os.path.isfile(toNib)) or not(fromNib in jsonData) or jsonData[fromNib] != fromNibMd5:
-                generateStringsFile(fromNib, utf8)
                 writeNib(fromNib, toNib, utf8)
-                jsonData[fromNib] = fromNibMd5
-            
+                
+        jsonData[fromNib] = fromNibMd5
+                
     #update Localize.json
     localizeData = open(localizeJson, 'w')
     json.dump(jsonData, localizeData, indent=4)