4

IOS application provide the support of localization through the Localizable.strings file. If I want to change the file name for some obvious reasons where would I have to put that reference.

Can anyone please help.

3
  • 1
    Could you let us know what is the obvious reasons? Commented Jan 28, 2016 at 8:36
  • i have two projects A, B. In A there is a folder name B. I work the project B separately and add all the files to the Project A. As in project A there is already a file named Localizable.strings so i can not use Localizable.stringss in B also. that is the reason Commented Jan 28, 2016 at 8:51
  • I don't really get it ;) do you want A or B or do you want to merge A and B ? Commented Jan 28, 2016 at 8:56

2 Answers 2

7

How iOS localization works:

As you would already know, iOS provides a nice API for getting localized string as following.

NSString *stringValue = [[NSBundle mainBundle] localizedStringForKey:key
                                                                   value:@""
                                                                   table:nil];

And it also provides a macro for quick access as:

#define NSLocalizedString(key, comment) \
        [[NSBundle mainBundle] localizedStringForKey:(key) value:@"" table:nil]

iOS, by default, looks for strings in Localizable.strings file. However, we can also provide a custom file for iOS to look for strings into. And this is where things get interesting.

To provide a custom file, we can use the API as mentioned above in following manner.

NSString *localizedString = [[NSBundle mainBundle] localizedStringForKey:key
                                                                   value:@""
                                                                   table:@"AnotherLocalizableStringsFile"];

The table parameter takes a string argument AnotherLocalizableStringsFile which is the file containing the strings.

Another interesting parameter is the value parameter that takes in a string that should be returned in case no string is found matching the given key.

So following piece of code would return Invalid Key assuming the provided key does not exist in the file.

NSString *stringValue = [[NSBundle mainBundle] localizedStringForKey:@"Wrong_key_passed"
                                                                   value:@"Invalid Key"
                                                                   table:@"TargetLocalizable"];

The solution:

By using the combination of these two interesting parameters, we can devise a method to suit our requirements. We will ask iOS to look for strings specific to target in target specific strings file and fall back to Localizablestrings file when it comes to loading generic strings common to all targets.

Here’s how the piece of code looks like.

NSString *stringValue = [[NSBundle mainBundle] localizedStringForKey:@"Key"
                                                                   value:NSLocalizedString(@"Key", nil)
                                                                   table:@"TargetLocalizable"];

So, iOS looks for the string first in the given TargetLocalizable.strings file. If it doesn’t find it there, it would search in the base Localizable.strings file.

So all I had to do was to place all the strings common to all targets in Localizable.strings file and put the additional and overridden strings specific to the target in TargetLocalizable.strings file.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your nice explanation. You save my day.
I found another way NSString *string = NSLocalizedStringFromTable(@"Key", @"TableName", @"Default Text");
-1

The name is 'fixed' - a localizable is named Localizable. you can only decide to not use NSBundle localization and roll your own stuff

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.