Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Localization - Updating Localizable.strings with just new strings

I have searched Google and StackOverflow and still have no clear answer on an easy and automated way of doing this but here is the scenario:

  1. I have an app with 1000 strings localized into en, fr, de, es, it.
  2. I build a new feature that makes 10 distinctly new NSLocalizedString() keys.
  3. I just want those 10 new strings appended onto the ends of the files:
    • en.lproj/Localizable.strings
    • fr.lproj/Localizable.strings
    • es.lproj/Localizable.strings
    • de.lproj/Localizable.strings
    • it.lproj/Localizable.strings

genstrings will retrieve all 1010 distinct strings. This is a pain since I'll need to "needle in a haystack" find those 10 strings every time I do an update.

UPDATE 19-SEP-2014 -- XCode 6 - Apple has finally released support for XLIFF export and import of your .strings files Whats new in XCode 6? Localisation

Linguan (v1.1.3) whilst it is a lovely tool most of the time, it is starting to be a tool in the other sense. It merges the changes but some strings aren't matching correctly when it merges, so everytime it does a Scan Sources it creates 100 new duplicate keys as well as the 10 strings I am after so it is making more work.

FileMerge As suggested below try doing a diff between old and new versions of the genstrings output files. The genstrings output has the strings sorted alphabetically so 10 strings scattered throughout 1000 means that there are 200 differences to review. it keeps matching the /*...*/ and the "..." = "..." and saying that the ... has been updated. It hasn't been updated, just shifted to a new location in the file. More and more it is looking like I am going to have to write a custom tool.

MacHG + FileMerge on a side note, for some strange reason doesn't like doing diffs out of the repository with the working copy of Localizable.strings. Both the left and right panes appear empty. UPDATE: Turns out variations in some changesets being saved as UTF-16 and some as UTF-8 are screwing with it being able to do a proper diff.

Bash Script + FileMerge I have written the following script to help maintain my english reference file after each time I add new NSLocalizedString entries:

#LOCALISATION UPDATE SCRIPT # #This will create a temporary copy of the current 'en' reference file then generate the  #latest reference file using the 'genstrings' tool. Finally forcing FileMerge to launch  #and diff the changes. # #Last Updated: 2014-JAN-06 #Author(s): Josh Wilson  clear #assuming this script is run from $SRCROOT  #Backup Existing 'en' reference cp "en.lproj/Localizable.strings" "en.lproj/Localizable-src.strings"  #Scan source files for 'NSLocalizableString' macros genstrings -q -u -o en.lproj Classes/*.{m,mm} genstrings -q -u -a -o en.lproj Classes/iPad/*.{m,mm} genstrings -q -u -a -o en.lproj Classes/iPhone/*.{m,mm}  #Force FileMerge to launch and diff the update (NOTE: piping to cat forces GUI to open) opendiff "en.lproj/Localizable-src.strings" "en.lproj/Localizable.strings" | cat  #Cleanup up temporary file rm "en.lproj/Localizable-src.strings" 

But this only updates the EN file and I am lacking a way of having the other language files updated with the new keys. This one has been good for instances where I don't have an english word as the key and genstrings bombs my "welcome_message" = "Welcome!" with "welcome_message" = "welcome_message"

POEditor http://poeditor.com/. This is an online tool and subscription based after 1000 strings. Seems to work well but it would be good if there was a non subscription based tool.

Traducto Pro Seems to do an alright job of integrating with XCode and extracting the strings and merging things together. But it is impossible to get anything back out of it until it is fully translated so you are coerced into using their translation services.

Surely this functionality has been implemented before. How does Apple keep their Apps localised?

Script junkies, I call upon thee! iOS development has been going on for some time now and localisation is kind of common, surely there is a mature solution to this by now?

Python Script update_strings.py: Stackoverflow finally recommended a related question and the python script in this answer Best practice using NSLocalizedString looks promising...

Tested it and in its current form (31-MAY-2013) it doesn't handle multiline comments if you have duplicate comments entries (expects single line comments).

Might just need to tweak the regex's a bit.

like image 609
Josh Peak Avatar asked May 29 '13 06:05

Josh Peak


People also ask

How do I add localizable strings?

To add Localizable. strings file, go to File->New->File , choose Strings File under Resource tab of iOS, name it Localizable. strings , and create the file. Now, you have a Localizable.

How does NSLocalizedString work?

NSLocalizedString is a Foundation macro that returns a localized version of a string. It has two arguments: key , which uniquely identifies the string to be localized, and comment , a string that is used to provide sufficient context for accurate translation.

How do I localize a string in Swift?

Select the project and under “Localizations”, click the “+” icon. Add any language you want (I selected Italian) then click on “Finish”. Now go back to Localizable. string file, select it and on the File Inspector (right menu) select “Localize”.


2 Answers

Checkout BartyCrouch, it perfectly solves your problem. Also it is open source, actively maintained and can be easily installed and integrated within your project.


Install BartyCrouch via Homebrew:

brew install bartycrouch 

Alternatively, install it via Mint:

mint install Flinesoft/BartyCrouch 

Incrementally update your Localizable.strings files:

$ bartycrouch update 

This will do exactly what you were looking for.


In order to keep your Storyboards/XIBs Strings files updated over time I highly recommend adding a build script (instructions on how to add a build script here):

if which bartycrouch > /dev/null; then     bartycrouch update -x     bartycrouch lint -x else     echo "warning: BartyCrouch not installed, download it from https://github.com/Flinesoft/BartyCrouch" fi 

In addition to incrementally updating your Storyboards/XIBs Strings files this will also make sure your Localizable.strings files stay updated with newly added keys in code using NSLocalizedString and show warnings for duplicate keys or empty values.

Make sure to checkout BartyCrouch on GitHub for additional information.

like image 123
Jeehut Avatar answered Oct 02 '22 21:10

Jeehut


if you have the genstrings for the previous version, just a "diff" between new and old could do the tricks

EDIT: best use vimdiff to deal with utf-16 files

like image 41
Luis Avatar answered Oct 02 '22 21:10

Luis