Преглед изворни кода

parse plurals from android xml format to stringsdict

cyberta пре 6 година
родитељ
комит
4c73d38db8
2 измењених фајлова са 93 додато и 20 уклоњено
  1. 89 17
      convertTranslations.js
  2. 4 3
      updateTranslations.sh

+ 89 - 17
convertTranslations.js

@@ -77,19 +77,84 @@ function parseAndroid(data) {
 function isEmpty(line) {
   return /^\s*$/.test(line);
 }
+function toStringsDict(pluralsMap) {
+    if (!pluralsMap || pluralsMap.length == 0) {
+      return;
+    } 
+
+    const rgxZero = /<item quantity="zero">(.*)<\/item>/;
+    const rgxOne = /<item quantity="one">(.*)<\/item>/;
+    const rgxTwo = /<item quantity="two">(.*)<\/item>/;
+    const rgxFew = /<item quantity="few">(.*)<\/item>/;
+    const rgxMany = /<item quantity="many">(.*)<\/item>/;
+    const rgxOther = /<item quantity="other">(.*)<\/item>/;
+
+    let out = '<plist version="1.0">\n';
+    out += '\t<dict>\n';
+    for (keyValuePair of pluralsMap) {
+        let key = keyValuePair[0];
+        out += '\t\t<key>' + key + '</key>\n';
+        out += '\t\t<dict>\n';
+        out += '\t\t\t<key>NSStringLocalizedFormatKey</key>\n'
+        out += '\t\t\t<string>%#@localized_format_key@</string>\n'
+        out += '\t\t\t<key>localized_format_key</key>\n'
+        out += '\t\t\t<dict>\n'
+        out += '\t\t\t\t<key>NSStringFormatSpecTypeKey</key>\n'
+        out += '\t\t\t\t<string>NSStringPluralRuleType</string>\n'
+        out += '\t\t\t\t<key>NSStringFormatValueTypeKey</key>\n'
+        out += '\t\t\t\t<string>d</string>\n'
+        let lines = keyValuePair[1];
+        let zero = lines.filter( value => value.match(rgxZero));
+        let one = lines.filter( value => value.match(rgxOne));
+        let two = lines.filter( value => value.match(rgxTwo));
+        let few = lines.filter( value => value.match(rgxFew));
+        let many = lines.filter( value => value.match(rgxMany));
+        let other = lines.filter( value => value.match(rgxOther))
+        if (zero.length > 0) {
+          out += '\t\t\t\t<key>zero</key>\n';
+          out += '\t\t\t\t<string>'+zero[0].match(rgxZero)[1]+'</string>\n';
+        }
+        if (one.length > 0) {
+          out += '\t\t\t\t<key>one</key>\n';
+          out += '\t\t\t\t<string>'+one[0].match(rgxOne)[1]+'</string>\n';
+        }
+        if (two.length > 0) {
+          out += '\t\t\t\t<key>two</key>\n';
+          out += '\t\t\t\t<string>'+two[0].match(rgxTwo)[1]+'</string>\n';
+        }
+        if (few.length > 0) {
+          out += '\t\t\t\t<key>few</key>\n';
+          out += '\t\t\t\t<string>'+few[0].match(rgxFew)[1]+'</string>\n';
+        }
+        if (many.length > 0) {
+          out += '\t\t\t\t<key>many</key>\n';
+          out += '\t\t\t\t<string>'+many[0].match(rgxMany)[1]+'</string>\n';
+        }
+        if (other.length > 0) {
+          out += '\t\t\t\t<key>other</key>\n';
+          out += '\t\t\t\t<string>'+other[0].match(rgxOther)[1]+'</string>\n';
+        }
+        out += '\t\t\t</dict>\n'
+        out += '\t\t<dict>\n';
+    }
+    out += '\t</dict>\n';
+    out += '</plist>\n';
+
+    return out;
+}
 
-function toIOS(lines) {
+function toLocalizableStrings(lines) {
   let out = '';
   for (let line of lines) {
-                if (typeof line === 'string') {
-            if (line === '') {
-              out += '\n';
-                                continue;
-      }
+    if (typeof line === 'string') {
+      if (line === '') {
+        out += '\n';
+        continue;
+    }
         
-      if (/\n/.test(line))
-              out += '/* ' + line + ' */';
-      else
+    if (/\n/.test(line))
+            out += '/* ' + line + ' */';
+    else
               out += '// ' + line;
     } else {
       let key = line[0];
@@ -98,7 +163,6 @@ function toIOS(lines) {
     }
     out += '\n';
   }
-  
   return out;
 }
 
@@ -118,8 +182,6 @@ function merge(base, addendum){
     }
     if (add) {
       out.push(addendum[i]);
-    } else {
-      console.warn(addendum[i] + " is already included. Consider removing it from the additional string source!");
     }
   }
   return out;
@@ -151,13 +213,23 @@ function convertAndroidToIOS(stringsXMLArray, appleStrings) {
 
   for (entry of stringsXMLArray) {
     allElements = parseXMLAndAppend(allElements, entry)
-    console.log("parsed " + allElements.parsed.length + " elements of " + entry)
+    console.log("parsed " + allElements.parsed.length + " entries of " + entry + " for Localizable.strings and " + allElements.parsedPlurals.size + " entries for Localizable.stringsdict");
   }
 
-  console.log(allElements.parsedPlurals)
 
-  let iosFormatted = toIOS(allElements.parsed);
-  fs.writeFile(output, iosFormatted, function (err) {
+  let iosFormatted = toLocalizableStrings(allElements.parsed);
+  let iosFormattedPlurals = toStringsDict(allElements.parsedPlurals);
+
+  let localizableStrings = output + "/Localizable.strings";
+  let stringsDict = output + "/Localizable.stringsdict";
+  fs.writeFile(localizableStrings, iosFormatted, function (err) {
+    if (err) {
+      console.error("Error converting " + stringsXMLArray + " to " + appleStrings);
+      throw err;
+    }
+  });
+
+  fs.writeFile(stringsDict, iosFormattedPlurals, function (err) {
     if (err) {
       console.error("Error converting " + stringsXMLArray + " to " + appleStrings);
       throw err;
@@ -167,7 +239,7 @@ function convertAndroidToIOS(stringsXMLArray, appleStrings) {
 
 if (process.argv.length < 4) {
     console.error('Too less arguments provided. \nExample:\n ' + 
-    "node convertTranslations.js stringsInputfile1.xml stringsInputfile2.xml stringsInputfileN.xml stringsOutputfile.strings");
+    "node convertTranslations.js stringsInputfile1.xml stringsInputfile2.xml stringsInputfileN.xml path/to/outputfolder");
     process.exit(1);
 }
 

+ 4 - 3
updateTranslations.sh

@@ -33,12 +33,13 @@ tx pull --all --source
 cd ..
 for (( i=0; i<${#IOS_TRANSLATIONS[@]}; i++ )) {
     LANG_DIR=`echo ${IOS_TRANSLATIONS[i]} | cut -d "." -f2 | cut -c 2-`
-    echo "convertTranslations: $TMP_ANDROID_TRANSLATIONS/$LANG_DIR/strings.xml -> ${IOS_TRANSLATIONS[i]}"
+#    echo "convertTranslations: $TMP_ANDROID_TRANSLATIONS/$LANG_DIR/strings.xml -> ${IOS_TRANSLATIONS[i]}"
+    OUTPUT_DIR=`echo ${IOS_TRANSLATIONS[i]} | sed 's/\/Localizable.strings//g'`
     if [[ $LANG_DIR == "en" && -f untranslated.xml ]] 
     then
-        node convertTranslations.js $TMP_ANDROID_TRANSLATIONS/$LANG_DIR/strings.xml untranslated.xml ${IOS_TRANSLATIONS[i]}
+        node convertTranslations.js $TMP_ANDROID_TRANSLATIONS/$LANG_DIR/strings.xml untranslated.xml ${OUTPUT_DIR}
     else 
-        node convertTranslations.js $TMP_ANDROID_TRANSLATIONS/$LANG_DIR/strings.xml ${IOS_TRANSLATIONS[i]}
+        node convertTranslations.js $TMP_ANDROID_TRANSLATIONS/$LANG_DIR/strings.xml ${OUTPUT_DIR}
     fi
 }