What Is the Second Parameter of Nslocalizedstring()

What is the second parameter of NSLocalizedString()?

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.

Standards for comments in NSLocalizedString

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";

In your specific example, it's fairly obvious what the comment means, but not the context. You should probably add some context like so:

NSLocalizedString(@"Tap your account to sign in", @"Instruct user to tap their account to sign in (Facebook account, main game preferences)");

That way the localizer knows exactly what button you're referring to.

This becomes even more important for buttons labelled "Share" or some other non-specific label:

NSLocalizedString(@"Share", @"Label for sharing button on main image editing screen");

(This is a modified version of my answer to this similar question).

Possible to use variables and/or parameters with NSLocalizedString?

It turns out that a missing target entry is to blame. Just checking that my current build target includes the Localizable.string file solved the problem!

What does comment means in NSLocalizedString(_:tableName:bundle:value:comment:)

When you create *.strings files using command line tool called genstrings, those comments will appear as comments above every entry. It can be used by translation team to know what is this string for.

How to use NSLocalizedString function with variables in Swift?

You can use the sprintf format parameters within NSLocalizedString, so your example can look like this:

let myString = String(format: NSLocalizedString(" - %d Notifica", comment: "sottotitolo prescrizione per le notifiche al singolare"), count)

What do NSLocalizedString() parameters value and tableName do?

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"

Best practice using NSLocalizedString

NSLocalizedString has a few limitations, but it is so central to Cocoa that it's unreasonable to write custom code to handle localization, meaning you will have to use it. That said, a little tooling can help, here is how I proceed:

Updating the strings file

genstrings overwrites your string files, discarding all your previous translations.
I wrote update_strings.py to parse the old strings file, run genstrings and fill in the blanks so that you don't have to manually restore your existing translations.
The script tries to match the existing string files as closely as possible to avoid having too big a diff when updating them.

Naming your strings

If you use NSLocalizedString as advertised:

NSLocalizedString(@"Cancel or continue?", @"Cancel notice message when a download takes too long to proceed");

You may end up defining the same string in another part of your code, which may conflict as the same english term may have different meaning in different contexts (OK and Cancel come to mind).
That is why I always use a meaningless all-caps string with a module-specific prefix, and a very precise description:

NSLocalizedString(@"DOWNLOAD_CANCEL_OR_CONTINUE", @"Cancel notice window title when a download takes too long to proceed");

Using the same string in different places

If you use the same string multiple times, you can either use a macro as you did, or cache it as an instance variable in your view controller or your data source.
This way you won't have to repeat the description which may get stale and get inconsistent among instances of the same localization, which is always confusing.
As instance variables are symbols, you will be able to use auto-completion on these most common translations, and use "manual" strings for the specific ones, which would only occur once anyway.

I hope you'll be more productive with Cocoa localization with these tips!

Keep getting null using NSLocalizedString in Swift

Modify your declaration to this:

let showDiffrence = "\(arrowUp) \(NSLocalizedString("Day", comment: "Show Diffrence")) \(percentageDif)"


Related Topics



Leave a reply



Submit