What is the second parameter of NSLocalizedString()?

IosObjective CSwiftXcodeLocalization

Ios Problem Overview


What is the *comment parameter in:

NSString *NSLocalizedString(NSString *key, NSString *comment)

If I do this:

NSLocalizedString(@"Hello_World_Key", @"Hello World")

and have two versions of a Localizable.strings (English and Spanish), does each need the entry:

English.lproj/Localization.strings: @"Hello_World_Key" = @"Hello World";

Spanish.lproj/Localization.strings: @"Hello_World_Key" = @"Hola Mundo";

Isn't the English one redundant?

Ios Solutions


Solution 1 - Ios

The second parameter is a comment that will automatically appear in the strings file if you use the genstrings command-line utility, which can create the strings file for you by scanning your source code.

The comment is useful for your localizers. For example:

NSLocalizedString(@"Save",@"Title of the Save button in the theme saving dialog");

When you run genstrings, this will produce an entry in the Localizable.strings file like this:

/* Title of the Save button in the theme saving dialog */
"Save" = "Save";

Solution 2 - Ios

The comment string is ignored by the application. It is used for a translator's benefit, to add meaning to the contextual usage of the key where it is found in your application.

For example, the Hello_World_Key key may take different values in a given language, depending on how formal or informal the Hello World phrase needs to be in that language ("What's up World", "Yo World", "Good Day World", etc.).

You can add a string in the comment field to hint this usage to the translator, who will (one would presume) be better able to localize your application.

Solution 3 - Ios

The comment parameter is used for ease in translation. It has nothing to do with output of NSLocalizedString function. It will help just translator to translate nothing else.

Solution 4 - Ios

According to Localizing Your App documentation, you can export localizations for a localization team. The comments you put in NSLocalizedString are included in the exported files.

Exporting localizations creates files with xliff extension, which contains XML like the code below.

<trans-unit id="Settings">
    <source>Settings</source>
    <target>설정</target>
    <note>Label of the button to Settings screen</note>
</trans-unit>
<trans-unit id="Take Photo">
    <source>Take Photo</source>
    <target>사진 찍기</target>
    <note>No comment provided by engineer.</note>
</trans-unit>

XLIFF files can be edited using app localization tools like XLIFFTool.

Solution 5 - Ios

It's for just developer understanding on the translation, that is you are giving a key to get corresponding string from the corresponding strings file.

The comment parameter enables developer to understand what the key represents...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question4thSpaceView Question on Stackoverflow
Solution 1 - IosRob KenigerView Answer on Stackoverflow
Solution 2 - IosAlex ReynoldsView Answer on Stackoverflow
Solution 3 - IosMageNativeView Answer on Stackoverflow
Solution 4 - IosalanhchoiView Answer on Stackoverflow
Solution 5 - IosShanmugaraja GView Answer on Stackoverflow