What's Nslocalizedstring Equivalent in Swift

What's NSLocalizedString equivalent in Swift?

The NSLocalizedString exists also in the Swift's world.

func NSLocalizedString(
key: String,
tableName: String? = default,
bundle: NSBundle = default,
value: String = default,
#comment: String) -> String

The tableName, bundle, and value parameters are marked with a default keyword which means we can omit these parameters while calling the function. In this case, their default values will be used.

This leads to a conclusion that the method call can be simplified to:

NSLocalizedString("key", comment: "comment")

Swift 5 - no change, still works like that.

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.

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 is the correct way to use NSLocalizedString?

The correct way is

 subjectLabel.text = NSLocalizedString(@"Kind Information", @"Info Label");

iOS Sample Image 45

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.

NSLocalizedString with variable in Swift

In your localisable, you can't setup a custom text directly, you can only use text and format flags. So, in order to get your goal, you can do this:

NEUEARTIKEL="Add an item to %@:";

After that, get your title well-formatted using NSString(format: <#NSString#>, <#args: CVarArgType#>...)

let title = NSString(format: NSLocalizedString("NEUEARTIKEL", nil), titelArr[sender.tag])
let alert = UIAlertController(title: title, message: nil, preferredStyle: .Alert)

Once that done, your localizable string will be formatted as you want.

Keep getting null using NSLocalizedString in Swift

Modify your declaration to this:

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

NSLocalizedString with format specifiers in Swift yields garbage

String.localizedStringWithFormat takes a String and CVarArg... as arguments. You passed in an array of Any - values as the second argument. It is forced to convert an array to a decimal number, resulting in the weird result.

To solve this problem, you just need to find an overload that takes an [CVarArg] instead. Luckily, there is an init overload like that:

 return String.init(format: 
NSLocalizedString(self, comment: ""), arguments: values)

However, values is an [Any], which is not compatible with the expected [CVarArg]. You should probably change the parameter type.

So your whole extension looks like this:

func localized(with values: CVarArg...) -> String {
return String.init(format: NSLocalizedString(self, comment: ""), arguments: values)
}


Related Topics



Leave a reply



Submit