Returns a localized version of a string from the default table, which Xcode autogenerates when exporting localizations.
String interpolation is a way to construct a new String value from a mix of constants, variables, literals, and expressions by including their values inside a string literal. You can use string interpolation in both single-line and multiline string literals.
DESCRIPTION. The genstrings utility generates one or more . strings files from the C, Objective-C, C++, Objective-C++, or Swift source code files provided as arguments. A . strings file is used for localizing an application for different languages, as described under Internationalization in the Developer Documentation.
A localized string can have different values depending on the language in which the project is being build. There are two categories of localized strings: the strings included in the installation package's UI, common to every MSI file.
I use next solution:
1) create extension:
extension String {
var localized: String {
return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "")
}
}
2) in Localizable.strings file:
"Hi" = "Привет";
3) example of use:
myLabel.text = "Hi".localized
enjoy! ;)
--upd:--
for case with comments you can use this solution:
1) Extension:
extension String {
func localized(withComment:String) -> String {
return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: withComment)
}
}
2) in .strings file:
/* with !!! */
"Hi" = "Привет!!!";
3) using:
myLabel.text = "Hi".localized(withComment: "with !!!")
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.
A variation of the existing answers:
Swift 5.1:
extension String {
func localized(withComment comment: String? = nil) -> String {
return NSLocalizedString(self, comment: comment ?? "")
}
}
You can then simply use it with or without comment:
"Goodbye".localized()
"Hello".localized(withComment: "Simple greeting")
Please note that genstrings
won't work with this solution.
By using this way its possible to create a different implementation for different types (i.e. Int or custom classes like CurrencyUnit, ...). Its also possible to scan for this method invoke using the genstrings utility. Simply add the routine flag to the command
genstrings MyCoolApp/Views/SomeView.swift -s localize -o .
extension:
import UIKit
extension String {
public static func localize(key: String, comment: String) -> String {
return NSLocalizedString(key, comment: comment)
}
}
usage:
String.localize("foo.bar", comment: "Foo Bar Comment :)")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With