Swifternalization
This is the main class of Swifternalization library. It exposes methods that can be used to get localized keys with or without expressions from Localizable.strings files.
It also uses Expressions.strings files to manage shared expressions that can have theirs identifiers placed in many versions of the Localizable.strings file.
Internal classes of the Swifternalization contains logic that is responsible for detecting which value should be used for the given key and value.
It is able to work with genstrings command-line tool like NSLocalizedString() macro does.
It looks for content in the NSBundle you can provide and try to find:
- Localizable.strings (Base),
- Localizable.strings of preferred language, e.g. Localizable.strings (en)
- Expressions.strings (Base),
- Expressions.strings of preferred language, e.g. Expressions.strings (en)
-
Struct to keep shared instance of
See moreSwifternalization
Declaration
Swift
private struct Static
-
Bundle when Localizable and Expressions .strings files are located.
Declaration
Swift
private let bundle: NSBundle
-
key-value from base Localizable.strings file
Declaration
Swift
private var basePairs = [TranslatablePair]()
-
key-value airs from preferred language Localizable.strings file
Declaration
Swift
private var preferredPairs = [TranslatablePair]()
-
Swifternalization takes NSBundle when Localizable.strings file is located. This method return instance of the class but you don’t need it because shared instance is set automatically.
It get Localizable.strings file version based on the first language from the prefferedLocalizations property of NSBundle. If Localizable.strings for preferred language isn’t exist then Base is used instead.
Declaration
Swift
public init(bundle: NSBundle)
Parameters
bundle
bundle when .strings files are located.
-
Returns localized string for simple key that does not contain any expression.
I18n.localizedString("car") I18n.localizedString("car", defaultValue: "Audi") I18n.localizedString("car", defaultValue: "Audi", comment: "Comment") I18n.localizedString("car", comment: "Comment")
Declaration
Swift
public class func localizedString(key: String, defaultValue: String? = nil, comment: String? = nil) -> String
Parameters
key
key placed in Localizable.strings file.
defaultValue
default value that will be returned when there is no translation for passed key. Default is nil.
comment
comment used by genstrings tool to generate description of a key. Default is nil.
Return Value
Returns translation for passed key if found. If not found and defaultValue is not nil it return defaultValue, otherwise returns key.
-
Returns localized string for key which contains expression.
I18n.localizedExpressionString("cars", value: "10") I18n.localizedExpressionString("cars", value: "10", defaultValue: "Few cars") I18n.localizedExpressionString("cars", value: "10", defaultValue: "10", comment: "This is a comment") I18n.localizedExpressionString("cars", value: "10", comment: "This is a comment")
Declaration
Swift
public class func localizedExpressionString(key: String, value: String, defaultValue: String? = nil, comment: String? = nil) -> String
Parameters
key
key placed in Localizable.strings file.
value
value used when validating expressions.
defaultValue
default value that will be returned when there is no translation for passed key. Default is nil.
comment
comment used by genstrings tool to generate description of a key. Default is nil.
Return Value
Returns translation for passed key if found. If not found and defaultValue is not nil it return defaultValue, otherwise returns key.
-
This method is just extension to method
localizedExpressionString(_:value:defaultValue:comment:)
that takesString
as avalue
parameter.Declaration
Swift
public class func localizedExpressionString(key: String, value: Int, defaultValue: String? = nil, comment: String? = nil) -> String
-
Method that set shared instance of Swifternalization
Declaration
Swift
private class func setSharedInstance(instance: Swifternalization)
-
Method that returns shared instance of Swifternalization
Declaration
Swift
private class func sharedInstance() -> Swifternalization!
-
Method responsible for loading content into Swifternaliztion library. It takes preferred language of user’s device, and tries to find Localizable.strings (Preferred Language) and Localizable.strings (Base) to get keys and values of words to translate.
It also tries to find and load Expressions.strings (Preferred Language) and Expressions.strings (Base) files when shared expressions might be and tries to combine them together with built-in shared expression, and at the end enumeate through key-value pairs for translation and changes keys that have only expression shortcuts to full expressions.
Declaration
Swift
private func load()
-
Gets preferred language of user’s device
Declaration
Swift
private func getPreferredLanguage() -> Language
-
Enumerate through translatable pairs dict and check if there are some shared expression identifiers that needs to be replaced with full expressions.
Next create translatable pairs with susch updated expressions to make it ready to be used by framework.
Declaration
Swift
private func createTranslablePairs(translatableDict: KVDict, expressions: [SharedExpression]) -> [TranslatablePair]