Search code examples
objective-cswiftxcodenslocalizedstring

NSLocalized String in Objective C


In swift to create a localized string use `NSLocalizedString(key: , value: , comment:) But in the older objective C https://developer.apple.com/documentation/foundation/nslocalizedstring There only appears to be key and comment and key appears to act as both the key and the value in the xliff file.

My question is how do I give a key and value to a nslocalizedstring in objective C?


Solution

  • matt has the correct answer; however, to spell it out directly:

    In Swift:

    NSLocalizedString(key, value: value, comment: comment)
    

    In Objective C:

    NSLocalizedStringWithDefaultValue(key, nil, NSBundle.mainBundle, value, comment)
    

    The Objective C version lets you look under the hood to see how it's done. Here is the code for the NSLocalizedString* macros in NSBundle.h.

    #define NSLocalizedString(key, comment) \
            [NSBundle.mainBundle localizedStringForKey:(key) value:@"" table:nil]
    #define NSLocalizedStringFromTable(key, tbl, comment) \
            [NSBundle.mainBundle localizedStringForKey:(key) value:@"" table:(tbl)]
    #define NSLocalizedStringFromTableInBundle(key, tbl, bundle, comment) \
            [bundle localizedStringForKey:(key) value:@"" table:(tbl)]
    #define NSLocalizedStringWithDefaultValue(key, tbl, bundle, val, comment) \
            [bundle localizedStringForKey:(key) value:(val) table:(tbl)]