'Check if Localizable.strings file is valid
I am creating Localizable.strings file programatically. I am downloading the file from server and showing the localization from that file.
But if there is some error in file then my localization don't work. It shows me the keys. If on server I edit the localization file and I added string as
"HELLO_WORLD" = Hello
Here, the key is correct but value is not in correct format. The format should be as
"HELLO_WORLD" = "Hello";
How can I programatically check at runtime if my Localizable.strings file does not contain any error and is valid?
Solution 1:[1]
Use plutil from the Terminal:
plutil -lint Localizable.strings
Solution 2:[2]
In addition to @Aderstedt's answer:
plutil -lint Localizable.strings does work, but you have to run it for each version of the file. E.g
cdinto your project rootcd en.lproj- you can replace this with any localisation you are working with.plutil -lint Localizable.strings
When you run step 3, you will either be shown an error, telling you what is wrong with your file. Or you will be told the file is OK
Solution 3:[3]
As mentioned above plutil (property list utility) is a great tool to validate your .plist and .strings files if you edit them manually. You can apply it to all your .strings file by combining it with find. In your project directory run
find . -name *.strings -exec plutil -lint {} \;
or use
find . -path ./DerivedData -prune -o -name *.strings -exec plutil -lint {} \;
if you like to exclude your DerivedData directory (as I usually do).
Solution 4:[4]
I had the same issue and found plutil isn't 'detailed' enough. The folks editing the files wanted something that would tell them more exactly what is wrong
plutil is just too broad.
so I wrote a quick&dirty java tool to test a strings file:
https://github.com/Daij-Djan/parseAndValidateAppleStringsFile
disclaimer: my code
Solution 5:[5]
There are many answers but they wasn't focus on the main points is "programmatically check at runtime". My suggestion is:
Programmatically find the path of your downloaded file program (Like .../Documents/YourApp.bundle/fr-FR.lproj/Localizable.strings)
NSString *path = [[NSBundle mainBundle] pathForResource:@"Localizable" ofType:@"strings" inDirectory:nil forLocalization:@"ja"];Convert it to array of strings
NSString *fileContents = [NSString stringWithContentsOfFile:localizablePath encoding:NSUTF8StringEncoding error:nil]; NSArray *allLinedStrings = [fileContents componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]];Check all line from allLinedStrings manually or with Regular Expression. Following code is an example of manually check with some simple rules:
for (NSString *line in allLinedStrings) { if (line.length >= 2) { NSString *firstTwoCharacters = [line substringToIndex:2]; if (![firstTwoCharacters isEqualToString:@"//"]){ if (![line containsString:@"\";"]) { NSLog(@"Invalid line"); } NSUInteger numberOfOccurrences = [[line componentsSeparatedByString:@"\""] count]; if (numberOfOccurrences < 4) { NSLog(@"Invalid line"); } } } else if (line.length > 0) { NSLog(@"Invalid line"); } }
Solution 6:[6]
Use the tool below, which is better than plutil. It has other features to detect missing localized string.
https://github.com/Rocker007/XCLocale
and use below option to validate your localized string file
./XCLocale.py -cf <iOS project path>
This tool will tell you exact line no for syntax error and also detect some of syntax error which is not detected by your latest Xcode version
For example:
#"screen.title" = " screen 1";
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Aderstedt |
| Solution 2 | |
| Solution 3 | Patru |
| Solution 4 | Iulian Onofrei |
| Solution 5 | |
| Solution 6 | Iulian Onofrei |
