close

 

測試一

find ./-name *.m -print0 | xargs -0 genstrings -o en.lproj

失敗 原因懶得查

 

測試二

find ./-name \*.m -print0 | xargs -0 genstrings -o en.lproj

成功

 

相關說明網站

http://blog.spritebandits.com/2012/01/25/ios-iphone-app-localization-genstrings-tips/

iOS / iPhone app localization – genstrings tips

A friend of mine asked me recently to help him to localize his iPhone app. The app is not that big so I agreed and he handled me over his code. Unfortunately NSLocalizedString* macros were nowhere to be seen. I had to go through every file and add them first. Then I was able to use genstrings command on the project files. He told me later that when he started, he had no idea how to do this properly, how to make his app localizable proof. It was his first iOS project.

On top of the above code changes he had some strings hardcoded in XIB files (e.g.: navigation bar titles, tab bar item titles). As some of XIBs are ‘shared’ between two projects I decided to modify the code slightly, rather than create localizable XIBs, so when he changes something in UI he only has to do this once.

With all the code prepared I could finally run genstrings on the project. I’d like to share a few tips on how to use this command.

In a nutshell:

To generate strings from *.m files located in the current folder execute:

1
$ genstrings -o en.lproj *.m

To generate strings from *.m files located in the current folder + subfolders execute:

1
$ find . -name *.m | xargs genstrings -o en.lproj

To generate strings from *.m files located in the current folder + subfolders but excluding Folder1 execute:

1
$ find . -name *.m -not -path "./Folder1/*" -print0 | xargs -0 genstrings -o en.lproj -1

 

 

The basic genstrings syntax is shown below (for the full details please refer to the command’s man page):

genstrings [-a] [-s <routine>] [-q] [-o <outputDir>] sourcefile(s)

 

Options:

-a appends the command output to previously generated/existing files.
-s replaces NSLocalizedString* with a different routine e.g.: – s SBLocalizedString. This will catch all calls in the code to SBLocalizedString and SBLocalizedStringFromTable.
-q turns off multiple key/value pairs warnings.
-o output folder.

 

So the basic call can look like this:

1
$ genstrings -o en.lproj *.m

The command goes through all *.m files in the current folder, locates lines with NSLocalizedString* macros and creates Localizable.strings file. Subfolders are not being searched in this case, so it’s not very useful if your project is nicely structured.

The following call looks for strings in all *.m files inside the current folder, this time subfolders are included, and creates Localizable.strings file.

1
$ find . -name *.m | xargs genstrings -o en.lproj

I tried this approach on my friend’s project and it failed miserably…

His project uses ShareKit framework which comes with its own Localizable.strings file. It also replaces NSLocalizedString calls with a custom method. This causes the following error:

1
2
$ find . -name *.m | xargs genstrings -o en.lproj
Bad entry in file ./ShareKit/Core/SHK.m (line = 636): Argument is not a literal string.

Just to give you an idea what’s behind this error message take a look at the code below. Genstrings simply does not know how to handle key variable it just parses files.

1
2
3
4
5
6
7
8
9
10
11
12
NSString* SHKLocalizedString(NSString* key, ...)
{
    // Localize the format
    NSString *localizedStringFormat = NSLocalizedString(key, key);
     
    va_list args;
    va_start(args, key);
    NSString *string = [[[NSString alloc] initWithFormat:localizedStringFormat arguments:args] autorelease];
    va_end(args);
     
    return string;
}

I ended up doing the following:

1
$ find . -name *.m -not -path "./ShareKit/*" -print0 | xargs -0 genstrings -o en.lproj -1

This command simply ignores the whole ShareKit folder. To check beforehand what will be parsed you can use the find command:

1
$ find . -name *.m -not -path "./ShareKit/*"

You can also exclude from the search multiple folders like this:

1
$ find . -name *.m ( ! -path "./Forder1/*" -a ! -path "./Folder2/Subfolder/*" ) -print0 | xargs -0 genstrings -o en.lproj -1

My friend’s project localization is still in progress. Technical part is almost done. It wasn’t that hard after all… just a bit annoying sometimes. We decided to update ShareKit so it uses its own table (file) for localization and updated the code that was causing problems. In the project itself we decided to use project specific table as well. Just in case someone at some point decides to use or simply use a different framework with its own Localizable.strings.

Now all it’s pretty much about translation and testing… that’s the most difficult part…

If you have any questions, suggestions or comments on this post, please join the discussion below.

I hope you found this post useful. If you did, please consider signing up for my newsletter to be notified of new blog posts (appropriate form can be found near the top of the page). Please also share this post by tweeting it or use one of the sharing buttons located at the bottom of the page.

Resources:
1. man genstrings
2. String Resources

 

arrow
arrow
    全站熱搜

    zer931 發表在 痞客邦 留言(0) 人氣()