skip to Main Content

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?

2

Answers


  1. You’re looking at a reduced form of the macro. The full form is called saying

    NSString * NSLocalizedStringWithDefaultValue(
      NSString *key,
      NSString *tableName,
      NSBundle *bundle,
      NSString *value,
      NSString *comment
    )
    

    See https://developer.apple.com/documentation/foundation/nslocalizedstringwithdefaultvalue

    Just set the stuff you don’t care about at nil.

    Login or Signup to reply.
  2. 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)]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search