1

I was wondering if I could define a dictionary of key/value pairs to use as my language localization strings at run time in an ios app. The current method of changing language localization setting depends on an appropriate strings file being defined in the bundle when the app is initially built.

So instead of saying something like

NSLocalizedStringFromTableInBundle(@"Greeting", nil, localeBundle, nil);

Say something like

NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Hello", @"Greeting"];
NSLocalizedStringFromTableInBundle(@"Greeting", nil, myDictionary, nil);
3
  • Why don't you just build up your dictionary and use it? Commented Apr 3, 2014 at 12:22
  • I have my dictionary - I want some way of globally setting the app to use it. Commented Apr 3, 2014 at 12:26
  • To clarify my dictionary is an unknown at compile time - I request it via a rest call on app load. This is where I want to then set my localized strings Commented Apr 3, 2014 at 12:33

1 Answer 1

1

Haeder

#define MyLocalizedString(key, dict) \
  [LocalizationUtils localizedStringWithKey:(key) fromDict:(dict)]

@interface LocalizationUtils : NSObject

+ (NSString *) localizedStringWithKey:(NSString *)key fromDict:(NSDictionary *)dict;

@end

Implementation

+ (NSString *) localizedStringWithKey:(NSString *)key fromDict:(NSDictionary *)dict
{
   NSString *localizedString;
   if (!dict) {
       // use NSLocalizedString or sth as a fallback.
       // ...
   }

   // get your string from dict

   return localizedString;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the quick reply - give me a sec while I implement this!
So yes this does in effect allow me to use my dictionary defined key/values however I was hoping for a more "global" solution, that would mean using NSLocalizedString as usual but somehow 'tricking' the application to use my dictionary as its strings table. This method would mean modifying every use of NSLocalizedString to MyLocalizedString
What's the problem with replacing the calls? Special functionality -> special calls. The other way you could try to redefine the original macro or replace the original implementation of [NSBundle localizedStringForKey:value:table:] through a category. But I would not suggest that.
Haha I was just looking at subclassing NSBundle to try and do it 'globally'. So yes I think I agree with you - wrapping my own function that then falls back to the default string values does seem a reasonable solution. I did come across this SO question and answer that says largely the same things as you've said. Thanks for the help.
Overriding the standard NSBundle method you can also intercept storyboard's strings, without the need to set an outlet to every single localisable interface element. Can be useful.
|

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.