Skip to content

NSBundle: localizedStringForKey() method ​

The localizedStringForKey() method reads the translation of one key from a .strings table in the bundle.

Syntax ​

localizedStringForKey(key[, { value, table, localizations }])

Parameters ​

key

The key, a string. Converted as a string: numbers become strings, and null and undefined become an empty string.

options.value

The value to give when the key is not found, a string. When omitted or empty, the key itself is given when not found.

options.table

The table name, without .strings. When omitted, it is Localizable.

options.localizations

Which languages to look in, an array of strings. When omitted, the languages the app is using.

Return value ​

A string: the translation; when not found, value or the key itself.

Exceptions ​

TypeError

  • localizations is not an array: localizations needs an array of strings.

Description ​

Projects generated by Nacre have no Localizable.strings; an app's interface text usually lives in JS. This method is for reading translations shipped in native modules or third-party bundles. Reading real translations is not verified in the simulator.

Examples ​

js
const main = NSBundle.mainBundle;
console.log(main.localizedStringForKey('nacre.missing', { value: 'fallback' }));
console.log(main.localizedStringForKey('nacre.missing'));

Result: The bundle does not have this key, so it prints fallback, then nacre.missing.

Corresponding iOS API ​

Depending on whether localizations is given, calls localizedStringForKey:value:table: or localizedStringForKey:value:table:localizations:. The commonly used ObjC macro NSLocalizedString is the former.

See also ​