What is the second parameter of NSLocalizedString()?

30,923

Solution 1

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 2

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 3

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

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.

Share:
30,923
4thSpace
Author by

4thSpace

Updated on December 06, 2020

Comments

  • 4thSpace
    4thSpace over 3 years

    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?

  • Manni
    Manni over 11 years
    @JuandelaTorre: This question is some years old, maybe 4thSpace (the person who asked the question) hasn't have a look at his question since he accept one of the answers.
  • Oscar
    Oscar about 11 years
    It's also what will be used if the strings file doesn't contain an entry that matches the identifier you specified.
  • Nick Lockwood
    Nick Lockwood almost 11 years
    No, the key is used in that case, not the comment.
  • mike
    mike almost 7 years
    Thanks for the answer, I'm aware the comment argument doesn't affect the string returned by NSLocalizedString. The docs state that the string in the comment argument is placed above the key-value pair in the strings file, this isn't happening. I'd like to find out if I've misunderstood the documentation, or if I haven't, why the comment isn't appearing in my Localizable.strings file.
  • MageNative
    MageNative almost 7 years
    Nope it will not appear unless you manually put it there,that's what I am saying the comment is not going to appear on strings file it is just for translators benefit.
  • user924
    user924 about 6 years
    what is the point? yes it is useful for short id ("id" - "word";, like in your example "Save" = "Save";), but you have to manually add something longer"id" - "word 1 word 2 word 3";
  • Rob Keniger
    Rob Keniger about 6 years
    The comment (second parameter) is for localizers. Someone who is adding strings for your app in another language needs context in order to know what translation to apply. The first parameter just specifies the key in the localization strings file, the strings file is what actually stores the text to display in the UI.