String Localization & find the missing key in Localize string
Most of apps work on various languages , for supporting the various languages we have to take Language string file of respective language .
Here i will provide the full information regarding
a: How to take this file and add to project.
b: Localized using small method.
c: Track all the missing key Value:
The Above “a” point you can cover by this blog:
b: Write one Extension
1 2 3 4 5 6 7 |
extension String { var localized: String { return GlobalData.sharedInstance.language(key: self) } } |
Note: GlobalData.sharedInstance.language(key: self)
– this is method that will provide the corresponding key value from localize string file.
like home = “Home”;
Here is code Snippet :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
func language(key:String) ->String{ let languageCode = UserDefaults.standard if languageCode.object(forKey: "language") != nil { let language = languageCode.object(forKey: "language") if let path = Bundle.main.path(forResource: language as! String?, ofType: "lproj") { languageBundle = Bundle(path: path) } else{ languageBundle = Bundle(path: Bundle.main.path(forResource: "en", ofType: "lproj")!) } } else { languageCode.set("en", forKey: "language") languageCode.synchronize() let language = languageCode.string(forKey: "language")! var path = Bundle.main.path(forResource: language, ofType: "lproj")! if path .isEmpty { path = Bundle.main.path(forResource: "en", ofType: "lproj")! } languageBundle = Bundle(path: path) } return languageBundle.localizedString(forKey: key, value: "", table: nil) } |
It will return corresponding value;
Now its time to use this functionality:
Exam:
self.navigationItem.title = “applicationname”.localized
it will set the value of this key from string file – “applicationname”
Now we have to track which string are missing in file , for that we have to do some steps:
1: Open project and follow this:
2: Click on the edit scheme:
a: Click on Arguments and set – “NSShowNonLocalizedStrings YES”
b: Click on option and check on “Localization Debugging”
3: Now its time to Track the missing string in project , after setting all this things you have to just run this project :
Then you will got warning in debug window;
I have taken the example of this:
self.navigationItem.title = “applicationnamee”.localized
I did not localized this key in string file.
so I got warning like this: