Check if Localizable.strings file is valid
Asked Answered
F

6

46

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?

Foliation answered 21/8, 2015 at 7:40 Comment(1)
According to wikipedia, xx.strings file is kind of reduced old-style ASCII property list; it's a dictionary without parenthesis. So, plutil works fine to verify its format.Jaehne
D
108

Use plutil from the Terminal:

plutil -lint Localizable.strings
Dimitri answered 21/8, 2015 at 7:49 Comment(1)
great thanks. Xcode 9 and terminal are used and still working to help be debugMauceri
H
38

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

  1. cd into your project root
  2. cd en.lproj - you can replace this with any localisation you are working with.
  3. 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

Hundredpercenter answered 7/7, 2016 at 10:29 Comment(2)
You can simply run plutil -lint **/*.string.Silken
find . -name \*.strings -exec plutil -lint {} + works OK, too.Jaehne
A
11

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).

Almund answered 15/7, 2017 at 15:41 Comment(0)
I
1

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

Immoderate answered 21/8, 2015 at 8:9 Comment(0)
S
0

There are many answers but they wasn't focus on the main points is "programmatically check at runtime". My suggestion is:

  1. 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"];
    
  2. Convert it to array of strings

    NSString *fileContents = [NSString stringWithContentsOfFile:localizablePath encoding:NSUTF8StringEncoding error:nil];
    NSArray *allLinedStrings = [fileContents componentsSeparatedByCharactersInSet: [NSCharacterSet newlineCharacterSet]];
    
  3. 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");
        }
    }
    
Stony answered 5/12, 2017 at 7:36 Comment(1)
Maybe it's better to invoke plutil to check string files, instead of write your own method.Jaehne
R
-1

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";
Ridgley answered 22/1, 2019 at 5:56 Comment(2)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewPolytechnic
@juzraai, Sure will add more info this post.Ridgley

© 2022 - 2024 — McMap. All rights reserved.