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)
  • 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 takes String as a value parameter.

    Declaration

    Swift

    public class func localizedExpressionString(key: String, value: Int, defaultValue: String? = nil, comment: String? = nil) -> String