Skip to content

Instantly share code, notes, and snippets.

@kostapappas
Last active October 2, 2018 12:25
Show Gist options
  • Save kostapappas/23dc8d8aeb78089ef6f775a3eb0bf7e3 to your computer and use it in GitHub Desktop.
Save kostapappas/23dc8d8aeb78089ef6f775a3eb0bf7e3 to your computer and use it in GitHub Desktop.
Localization techniques
https://medium.com/@marcosantadev/app-localization-tips-with-swift-4e9b2d9672c9
1. Add localization language on Project
2. Add a string File "Localizable" (or many string files for better organization ex: "SpecificCategoryTableName" )
3. add following code
extension String {
func localized(bundle: Bundle = .main, tableName: String = "Localizable") -> String {
return NSLocalizedString(self, tableName: tableName, value: "**\(self)**", comment: "")
}
}
protocol Localizable {
var tableName: String { get }
var localized: String { get }
}
// 1
extension Localizable where Self: RawRepresentable, Self.RawValue == String {
var localized: String {
return rawValue.localized(tableName: tableName)
}
}
4. use:
private let typeSomethingText = FirstScreen.Texts.TypeSomethingPrompt.localized
extension FirstScreen {
private enum Texts: String, Localizable {
case TypeSomethingPrompt = "type_something_prompt"
case NoResults = "no_results"
var tableName: String {
return "Localizable" //or "SpecificCategoryTableName"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment