Blue
Blue

Reputation: 1448

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?

Upvotes: 0

Views: 447

Answers (2)

Jeffery Thomas
Jeffery Thomas

Reputation: 42588

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

Upvotes: 2

matt
matt

Reputation: 534893

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.

Upvotes: 2

Related Questions