Setting and creating localization strings at runtime in an ios app

I was wondering if I could define a dictionary of key / value pairs to use as language localization strings at runtime in an ios app. The current method for changing the language localization setting depends on the corresponding strings file that is defined in the bundle when the application is initially created.

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

      

+1


source to share


1 answer


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;
}

      

+1


source







All Articles