Reputation: 1469
I have a NSString as a global constant. This constant is defined by using
extern NSString *const kConstant;
in the .h file.
The value is set in the .m file (before @implementation):
NSString *const kConstant = @"myValue";
So far so good. As soon as I want to use the NSLocalizedString
macro
NSString *const kConstant = NSLocalizedString(@"myValue",@"the value");
I get the error: initializer element is not a compile-time constant.
Any idea how to get a global string value localized?
Upvotes: 1
Views: 454
Reputation: 7552
What I do is make the constant the key used in your localization. You have to use NSLocalizedString()
at the point where you need the string, because that expands into a macro/function that looks up the files in your app bundle and figures out which string to use (fallback for missing translations, etc.). I'm afraid there's no way around it; the localization has to happen at runtime.
Upvotes: 0
Reputation: 122468
You should make it a class method of a class, and call NSLocalizedString()
if it's not been allocated. This is similar to the Singleton Pattern:
MyStatics.h:
@interface MyStatics : NSObject
+ (NSString *)globalString
@end
MyStatics.m:
#import "MyStatics.h"
static NSString *_globalString = nul;
@implemenetation MyStatics
+ (NSString *)globalString
{
if (!_globalString)
_globalString = NSLocalizedString(@"myValue",@"the value");
return _globalString;
}
@end
Upvotes: 3