localisation in iOS application in two language - ios

I have localized my iOS application in French and English now
I want to add German language in my project.
For NSlocalized string I have created a localizable file for French, how can I create another localizable file for German languages,i am using this code for localizing strings
NSLocalizedString("ReJoin Now", tableName: nil, bundle: Bundle.main, value: "", comment: "")

Related

NSLocalizedString not getting value for the key

I have a 2 localized string files, one for Romanian and one for English. In both files I have this line:
English:
"invalidSum" = "Invalid amount"
Romanian:
"invalidSum" = "Suma invalida"
I use this message in a alert like this:
let titleAlert = NSLocalizedString("invalidSum", comment: "")
let sumAlert = UIAlertController(title: titleAlert, message: nil, preferredStyle: .alert)
sumAlert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(sumAlert, animated: true, completion: nil)
}
However, whenever the alert shows, I get the message "invalidSum", not "Invalid amount" for english localization and "Suma invalida" for romanian localization. What am I doing wrong?
Rename your Localized.string file to Localizable.strings. That's it.
Solved it
My issue was that I created those new strings into Main.strings file (The files that Xcode automatically creates for you when you select new languages in Project Info.
I had to create 2 new Strings file called "Localizable.strings" and Localize it one for english, one for romanian. I copied the strings I've written into Main.strings to Localizable.strings and now it works like charm.
The name for the localizable file should be "Localizable.strings".
In my case, the problem was in an incorrect format.
The problem did not arise immediately, but only after the next build cleanup (or tms like that).
So, I found that lines from the end of the file are not translated, but from begin are.
And I turned out that one of the lines looked like
= "some value";
That is, there simply was no key. And the build was going without errors. But all the lines below were not translated.

iOS - only one localization works

I try to localize my project. I have clicked "Use Base Internalization" and added 2 languages: English and French. The English is marked as "Development language" - not sure, maybe I set it, but in the storyboard I use another language (not English). And second difference between these 2 localizations is that English (Development language) has 2 files localized while French has only 1 file localized (screenshot) I checked the app's folder. In the en.lproj
there is only 1 file Main.strings with all my english translations. In the fr.lproj the same thing - only Main.strings but with french translations.
So in the folders everything is correct, but:
1) When I run app with English language - everything works correct. But when I set French language to simulator (or my real device, after deleting the app) nothing works - it shows me my standard storyboard (not English).
2) English localization has 2 localized and I can't understand what is the second file.
The second point is not crucial for me now. I just need to make French localization work
When you add Localization than these file will show
Localizable.strings (English). “Hello ” = “Hello”; (Ex you can write hello )
Localizable.strings (Frech). “Hello ” = “Bonjour”;
Or main string you can change object into freach
Main.strings(English). 3ZR-e4-Gil.text" = "Hello";
Main.strings(Frech). 3ZR-e4-Gil.text" = "Bonjour";
swift
import Foundation
extension String {
func localized(lang:String) -> String {
let path = Bundle.main.path(forResource: lang, ofType: "lproj")
let bundle = Bundle(path: path!)
return NSLocalizedString(self, tableName: nil, bundle: bundle!, value: "", comment: "")
}
}
let str = "Hello".localized(lang: self.language! )
change language via simulator
Or Programmatically change language via app
UserDefaults.standard.set(["en","fr"], forKey: "AppleLanguages")
UserDefaults.standard.synchronize()
SOLVED IT!!! The French localization wasn't used because there was a mistake in the Main.strings (for French) - I missed only one ("). But as in the Main.strings there is no catcher for such a mistakes I didn't get it.
So, it was: "XXX-6r-aEC.text" = "Confirmer;
and I changed it to:"XXX-6r-aEC.text" = "Confirmer**"**;

Swift cannot read madarin from JSON api?

I'm trying to read from the API's i've created using Kimono. However, when i print it in my console it seems like it can't read Mandarin Chinese in my xcode?
Am I missing something here?
Here's my code:
let myContentsToBeScanned = NSURL(string: "https://www.kimonolabs.com/api/7ant5tkm?apikey=jlZUlWROWVJjPFrVbCwdmmcIaTWO6ISI")
let myDataToBeRead = NSData(contentsOfURL: myContentsToBeScanned!)
do {
let myContentsFromJSON = try NSJSONSerialization.JSONObjectWithData(myDataToBeRead!, options: [])
print(myContentsFromJSON)
} catch let err as NSError {
print("nil")
}
I am getting the output:
results = {
collection1 = (
{
articleSum = "\U611f\U53d7\U842c\U8056\U7bc0\U8a6d\U8b4e\U602a\U8a95\U53c8\U6b61\U6a02\U7121\U6bd4\U7684\U6c23\U6c1b\Uff0c\U5c31\U4f86 Garena \U96fb\U7af6\U9928\U5427\Uff01";
articleTitles = "\U3010\U6d3b\U52d5\U3011\U5e7d\U6ba4\U65b0\U5a18\U904a\U8569\U4e2d\Uff0c\U842c\U8056\U7bc0\U4f86\U96fb\U7af6\U9928\U300c\U9b3c\U6df7\U300d\U5427\Uff01";
Thanks for your help
It's simply that the default Xcode font, Menlo, does not support Chinese character. The fact that the Unicode code points are print means your characters are there.
You can verify this by opening the Font Book app, search Menlo, then Cmd + I to see the Font Info. Menlo supports the following languages:
Language: Afrikaans, Albanian, Azerbaijani, Basque, Belarusian, Bulgarian, Catalan, Cornish, Croatian, Czech, Danish, Dutch, English, Esperanto, Estonian, Faroese, Finnish, French, Galician, German, Greek, Hausa, Hawaiian, Hungarian, Icelandic, Indonesian, Irish, Italian, Kalaallisut, Kazakh, Latvian, Lithuanian, Macedonian, Malay, Maltese, Manx, Norwegian Bokmål, Norwegian Nynorsk, Oromo, Polish, Portuguese, Romanian, Russian, Serbian, Slovak, Slovenian, Somali, Spanish, Swahili, Swedish, Swiss German, Turkish, Ukrainian, Uzbek, Welsh, Zulu
You can change Xcode to use a monospace font that does support Chinese, like MingLiU or SimHei. The easiest way to find out which font is available on your system is to create a Smart Collection:
File > New Smart Collection
Design Style = Monospaced
Languages include Chinese

Localizable.strings is not working

I added a Localizable.strings to my project, but its not working... Here is a sample:
NSLog(#"Welcome Text: %#", NSLocalizedString(#"WelcomeKey", #""));
The Localizable.strings:
Localizable.strings // DE
"WelcomeKey" = "Willkommen!";
and
Localizable.strings // EN
"WelcomeKey" = "Welcome!";
The Localization native development region is en
The NSLog in console is:
2013-05-11 04:45:49.552 App[13752:907] Welcome Text: WelcomeKey
Any ideas what's wrong ?
The localization from the Storyboard is working.
Codierung of both files are UTF-16
Have you added a strings file to store the localized text. Click File > New > New File.
This link will be helpful to you. Please check this localizable strings.
I found this answer maybe helpful to you:
Select the localized storyboard eg. Chinese (Simplified)
In File Inspector, toggle from “Localizable Strings” to “Interface Builder Cocoa Touch Storyboard”. This will retain the strings you already had, so you don’t have to worry.
Now, change it back to “Localizable Strings”, and things should be updated!
Here to view original tutorial

Create localizable.strings from Code in iOS

I'm currently localizing my iOS-Application. This works pretty decent so far.
I've already created localizable.strings and several xib-files and so about 80% of the app is already translated.
But the App also loads data from a WebService which passes me a key, e.g.: "TITEL 1" as well as all supported languages (values) attached to it e.g: "Titel One" "Titel Eins" "Titolo Uno".
And now i would like to store those values in the according localizable.strings files.
For example:
Localizable.strings (English) should then contain:
"TITLE 1" = "Title One";
Localizable.strings (German) should then contain:
"TITLE 1" = "Titel Eins";
Localizable.strings (Italian) should then contain:
"TITLE 1" = "Titolo Uno";
How can I do this?
You will not be able to insert them in the localization strings at run time, if the webserver is managed by you, i suggest that you send wich language is the user using,
Please consider this example
//Get the language code
NSString* languageCode = [[NSLocale preferredLanguages] objectAtIndex:0];
//Now send the request to the server with the language you want
NSString *str = [NSString stringWithFormat:#"www.yourserve.com/yourfunctiom?lang=%#", languageCode];
//Request it
Now basing on the language that you receive in your server you will return the appropriate string
This may be stretching the frameworks a bit, but if you want to use Apple's string loading functions you could try to build an NSBundle in your document directory (a bundle is just a directory, after all), fill it with files from the server and then use NSLocalizedStringFromTableInBundle to get to your strings.
Not sure it is worth it compared to the approach in Omar Abdelhafith's answer, though.

Resources