String Constant as a Localized String

前端 未结 6 2051
深忆病人
深忆病人 2021-02-13 18:14

I would like to localize my constants. Constants are defined and declared the usual way:

extern NSString * const kStringName;

NSString * const kStringName = @\"         


        
6条回答
  •  情话喂你
    2021-02-13 18:34

    Going the normal route by declaring with extern in a header and defining in the implementation using NSLocalizedString() results in this error:

    Initializer element is not a compile-time constant

    Here's one way to get around this problem.

    In the header file declare a class method that returns a string...

    @interface MyGlobals : NSObject
    
    + (NSString *)localizedStringWhatever;
    
    @end
    

    Implement the method...

    @implementation MyGlobals
    
    + (NSString *)localizedStringWhatever {
        return NSLocalizedString(@"Whatever", @"blah blah blah.");
    }
    
    @end
    

    When you need to use it import MyGlobals and ask it for the string...

    NSString *whatever = [MyGlobals localizedStringWhatever];
    

提交回复
热议问题