Update localization documentation

This commit is contained in:
Daniel Saidi 2021-09-08 12:06:36 +02:00
parent f373cce6c7
commit c046f62bcd
5 changed files with 19 additions and 1 deletions

View File

@ -19,6 +19,9 @@ public class BundleTranslator: Translator {
private let bundle: Bundle
/**
Translate the provided key to a localized string.
*/
public func translate(_ key: String) -> String {
bundle.localizedString(forKey: key, value: "", table: nil)
}

View File

@ -16,6 +16,9 @@ import Foundation
notifications when the app locale changes.
*/
public protocol LocalizationService: Translator {
/**
Change the service's locale.
*/
func setLocale(_ locale: Locale) throws
}

View File

@ -41,6 +41,9 @@ open class StandardLocalizationService: LocalizationService {
case lprojFileDoesNotExist(for: Locale)
}
/**
Change the service's locale.
*/
open func setLocale(_ locale: Locale) throws {
guard let languageCode = locale.languageCode else { throw LocaleError.languageCodeIsMissing(for: locale) }
guard loadBundle(for: languageCode) else { throw LocaleError.lprojFileDoesNotExist(for: locale) }
@ -49,6 +52,9 @@ open class StandardLocalizationService: LocalizationService {
notificationCenter.post(name: .localization(.localeDidChange), object: nil)
}
/**
Translate the provided key to a localized string.
*/
open func translate(_ key: String) -> String {
translator.translate(key)
}

View File

@ -15,6 +15,9 @@ public class StandardTranslator: Translator {
public init() {}
/**
Translate the provided key to a localized string.
*/
public func translate(_ key: String) -> String {
NSLocalizedString(key, comment: "")
}

View File

@ -14,5 +14,8 @@ import Foundation
*/
public protocol Translator: AnyObject {
/**
Translate the provided key to a localized string.
*/
func translate(_ key: String) -> String
}