Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS applications Localization.strings file name change

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.

like image 678
Saleh Enam Shohag Avatar asked Oct 30 '25 16:10

Saleh Enam Shohag


1 Answers

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.

like image 153
Aneeq Anwar Avatar answered Nov 02 '25 05:11

Aneeq Anwar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!