Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do NSLocalizedString() parameters "value" and "tableName" do?

The Apple docs for this (both in Xcode and webpage) have exactly no explanation of the parameters.

https://developer.apple.com/documentation/foundation/1418095-nslocalizedstring

For reference, the function signature is

NSLocalizedString(
    _ key    : String, 
    tableName: String? = default,  // ??
    bundle   : Bundle = default, 
    value    : String = default,   // ????
    comment  : String
) -> String

I have a vague idea what the tableName is -- but more information would be helpful. (Is it merely the filename for a strings file?) I have no idea what value is for.

like image 238
Andrew Duncan Avatar asked Nov 10 '17 16:11

Andrew Duncan


People also ask

How does NSLocalizedString work?

NSLocalizedString is a Foundation macro that returns a localized version of a string. It has two arguments: key , which uniquely identifies the string to be localized, and comment , a string that is used to provide sufficient context for accurate translation.

How do you force NSLocalizedString to use a specific language?

struct CustomLanguage { func createBundlePath () -> Bundle { let selectedLanguage = //recover the language chosen by the user (in my case, from UserDefaults) let path = Bundle. main. path(forResource: selectedLanguage, ofType: "lproj") return Bundle(path: path!) ! } }

What is LocalizedStringKey?

The key used to look up an entry in a strings file or strings dictionary file. iOS 13.0+ iPadOS 13.0+ macOS 10.15+ Mac Catalyst 13.0+ tvOS 13.0+ watchOS 6.0+


1 Answers

The Objective-C documentation for NSLocalizedStringWithDefaultValue explains the parameters:

Parameters

key
The key for a string in the specified table.

tableName
The name of the table containing the key-value pairs. Also, the suffix for the strings file (a file with the .strings extension) to store the localized string.

bundle
The bundle containing the strings file.

value
The value to return if key is nil or if a localized string for key can’t be found in the table.

comment
The comment to place above the key-value pair in the strings file.

Basically, the key is looked up in a file named tableName.strings in the specified bundle. That strings file will have the format:

# comment
"key" = "value"
like image 52
rmaddy Avatar answered Dec 03 '22 06:12

rmaddy