Localization in Swift 2 - ios

I want to localize my application; I am using Swift 2. I followed this tutorial but I experience 2 issues.
1. Localized image disappears for both languages
I localized multiple images the same way for German and English. One set of images (the en and de versions of an image) disappear for both languages. I added them, they are in the project's folder and I can not spot any difference to other images I localized.
I tried
cleaning and running the project (no errors)
removed the images and added them again
removed and added the localization
…nothing helped. Any idea?
2. Error w/ NSLocalizedString & Localizable.strings
I created a Localizable.strings for localizing my app's strings. Here is how I make them localizable:
button.setTitle(NSLocalizedString("buttonTitle", comment: "MyButton"), forState: .Normal)
In the Localizable.strings for English I added:
"buttonTitle" = "MyButton"
…and for German:
"buttonTitle" = "MeinButton"
Now, Xcode does not show any errors in code but it says
Read failed:
The data couldn't be read because it isn't in the correct format.
Any ideas?
Thank you in advance :)
Edit
Issue #2 was resolved, I missed a ; there. Thanks to codingVoldemort!

I think you have missed the semi-colon on the Localizable.strings after each key-value pair.

In the Localizable.strings for English and German you have missed the semi-colon at the end of statement.
It should be like :
"buttonTitle" = "MyButton"; and
"buttonTitle" = "MeinButton";
You can refer Apple docs for this.

I'm a bit late, but if you're still searching for a solution for task #1:
You could use the solutions presented in that tutorial; there's a Internationalization of Images section.
"After selecting [your image] in Project navigator, click Localize button under File Inspector. This should provide you with the option to Localize the file in English and German."
That being said, I'm unsure whether this would work on Swift 2 (if you're still on there); but that should be possible with an up-to-date Xcode/IDE.

Related

XLIFF doesn't contain all forms of plurals when trying to localize a project in Xcode

I'm trying to localize a small Xcode project (Xcode 9 and Swift 4). In that project there are places where I use plurals, so I need to localize them, too. To do that, I use stringsdictfile. In code I use localizedStringWithFormat(_:,fromat,:argument) static method on a String. Here how my code for a plural looks (I just print the words for testing):
let localizedString = NSLocalizedString("%d apple(s)", comment: "The number of apples")
print(String.localizedStringWithFormat(localizedString, 0))
print (String.localizedStringWithFormat(localizedString, 1))
print(String.localizedStringWithFormat(localizedString, 2))
print(String.localizedStringWithFormat(localizedString, 10))
Then I create a stringsdict file. It looks like this:
In the video from WWDC 2017 (session 401) about Localization it is said, that when we use stringsdict file for localizing plurals into other languages (in this case, I want to localize it into Russian) we just need to give values for the cases of our development language (English), and when exporting for localization, Xcode will automatically create cases in an XLIFF file for the language into which we want to localize. So I've given values for zero, one and other (in the demo from the session values are given only to keys one and other, however, I don't thing that that's the reason of the problem).
Now, when I create an XLIFF file it looks like this (only the part of apples):
As you can see, Xcode doesn't generate cases for a particular language automatically (for Russian it should have 4). I'm using stringsdict files as well as trying to localize plurals for the first time, so I can't figure out what I'm doing wrong. If you know where is the issue, or have any suggestions, I would appreciate your help.
There is little you can do in Xcode. XLIFF just holds the segments that it's been told to hold.
A better approach would be to rephrase the strings so that plurals are less an issue, like:
not "%d apple(s)"
but "number of apples: %d"
You should file a bug at apple about that.
Unless you have a requirement to work with the XLIFF from Xcode, I suggest you don't and instead rely on the string files.

NSLocalizedString in Spritekit SKLabelNode

I've been searching all over the internet and there doesn't seem to be a clear explanation on how to localize strings using SpriteKit. Only seeing tutorials for people using the interface builder, but all I really want is, imagine this:
I have an SKLabelNode called label. And I define the text like:
labl.text = NSLocalizedString("titleOfTheScreen",nil)
So basically what I think I have to do is add the new language in the Project settings. Then, I add a new Strings file called Localized, and add it to the new folder.
But what happens to my English language? There's no file for the original one
First you have to add a Strings File:
Then open the project settings and add a new language:
Mark your added strings file as target:
Find the newly added localising file. (English is automatically added)
From your screenshots I can see that you have "File.strings" file. You should have created "Localizable.strings" file.
Also, I can see that you have the (Base), (English) and (German) strings version. Why do you think English is not there?
In each of the files you should put strings like that:
"titleOfTheScreen" = "blah-blah";
Replace "blah-blah" with the proper translation in each of the strings files. It's important to note that the semi-colon at the end of the lines in strings files are mandatory, otherwise Xcode would issue some really funny error messages. This is easy to overlook if you're programming in Swift and trailing semi-colons are not mandatory.

SpriteKit Localization Hook-ups

I've tried reading online tutorials on how to localize strings. I have a game that uses SpriteKit as an engine and has user-readable text in .m files.
My guess on localizing these strings is to swap where I have a readable string (... .text = #"some string") with a call to NSLocalizedString().
Say, I have a label that says "play" when the game starts up in English. In Spanish, it would say "jugar." In addition, I have an options menu, where in English, it says "options", and in Spanish, it says "opciones."
If I'm correct, I'd add two .strings files to the project: en.string and es.string.
In the code, I would add:
playLabel.text = NSLocalizedString(#"play", nil)
optionsLabel.text = NSLocalizedString(#"options", nil)
And, in the en.strings file, it would say,
"play" = "play";
"options" = "options";
In the es.strings file, it would say,
"play" = "jugar";
"options" = "opciones";
How would I make it so these .strings files were hooked up to the GameScene.m file?
I found the issue and how to fix it. I read Ray Wanderlich's tutorial on this, available here.
How to fix this error, for those trying to figure this out in the future:
Create a new strings file named Localizable.strings.
Open the Utilities tab (the one on the right) if it's not already open.
Copy the text from en.strings and paste it into the Localizable.strings file.
Localize it into every language you'll be localizing it into (including Base) by clicking the button in the File Inspector labeled "Localize". It's hard to miss. (Note: You'll now notice a pull-down for a folder. Open it, and you'll find the Localizable.strings copied into every language you selected. However, it's not translated.)
Delete en.strings. It's no longer needed.
Copy and paste the text from es.strings into the Localizable.strings's Spanish file, and delete es.strings afterwards. Repeat for every language you have it localized in.

NslocalizedString return the key instead of the value

I create tow Localizable.strings files one for english and the second for arabic :
/* The number 1 */
"LABEL_ONE" = "label number one";
I am using this code to get the string value:
[self.Lable1 setText:NSLocalizedString(#"LABEL_ONE", #"The number 1")];
but the app show "LABEL_ONE" instead of "label number one" ?
What's the problem ?
Thanks
Your string files name have to be Localizable.strings.
In addition to the top answer, the localization file has to be named Localizable.strings and not anything else.
Your code is correct, so the problem is elsewhere.
Here are some possibilities:
The Localized.strings file is not copied into the application bundle. To verify this focus on the Localized.strings file using the 'Project Navigator' and make sure your target is checked on the right pane under 'Target Membership'
There is some syntax issues within the Localized.strings file, i.e. missing ;
The code you quoted does not get executed. To verify put a breakpoint and see if it's reached or use some debug logs...
The string appears in the Localized.strings for Arabic but is missing for English.
Your problem is very simple:
The app doesn't find the file with translations.
So you must be sure that you create the localizable files with the correct name (for example en for english) and also that in your device (or simulator) you have set 1 of the languages prepared from you in these files.
Otherwise, the file is not found and is returned the key instead of the value.
try this answer it is work for me
I was having problems with this on the iOS Simulator. I ended up deleting the Localization.strings file in the simulator directory
( /Users/(me)/Library/Application Support/iPhone Simulator/5.0/Applications/(etc)/(project)/(application.app)
copied from here
https://stackoverflow.com/a/11657945
In my case cleaning the project after initially introducing i18n to it solved it.
Check whether you added any text/character by mistake after the ; in Localizable.strings file.
It doesn't show the error while running the app but XCode ignore all the key-value pair after this character, which leads to returning the key instead of values.
I put a + by mistake in string file and wasted 2 hours for this.
Make sure that the Target Membership is set to include your App and any other places you may need it. In my case, the membership was only set to the test folder
[Answer from August, 2017. Compiler may have changed]
Search for a syntax error in your .strings File
I had the following line:
"Changed your mind?" = "¿Cambiaste de opinión?";,
And somehow that made the compilation succeed, even the command plutil -lint Localizable.strings said it was okay. After I took the same comma down from each file in that line, now the compiler is able to detect there are errors.
The problem before was the compilation succeeded, but because of the comma the .strings file was actually wrong so nothing after the comma was read. I don't know if the comma has any use in .strings files.
Note:
plutil -lint Localizable.strings is used on the Terminal tu debug .strings files as Xcode cannot tell where the .strings compilation wen wrong.

Localizing strings from the Settings.bundle using InAppSettingsKit

I am attempting to use InAppSettingsKit to manage my settings. This uses the Settings.bundle with a .plist file and the .strings files for each of the languages being translated.
I can confirm that the translation of my strings is working properly outside of my application, using the Setting application. But when I am in my application, the translation is not occurring.
I think it comes down to code like this, from the InAppSettingsKit class IASKSettingsReader, with a couple logging statements that I thought my be helpful:
- (NSString*)titleForStringId:(NSString*)stringId {
NSLog(#"%#",[_bundle localizedStringForKey:stringId value:stringId table:self.localizationTable]);
NSLog(#"%#",[_bundle localizedInfoDictionary]);
return [_bundle localizedStringForKey:stringId value:stringId table:self.localizationTable];
}
If I understand correctly, this should be using a table with the name self.localizationTable as the source of the translation. This value is simply "Root". It's not a path to the Root.strings file in the selected language, so I am guessing that the method localizedStringForKey:value:table: must be using some global system reference that points to the correct path.
I have confirmed that the strings file name is "Root.strings" all around, with a capital R, including in the Root.plist file.
[_bundle localizedInfoDictionary] returns (null); It is doing this for two language settings of English and French.
I'm not sure how to debug this. Thanks for any help you can give.
I'm using InAppSettingsKit with localized text with no problems. Two things I can think of that you could check: are your Root.strings files located in the correct subdirectories of Settings.bundle (en.lproj and fr.lproj for English and French?
Is there a "Strings Filename" entry in your Root.plist? It should simply contain a string with value "Root"
It has been quite some time since I resolved this, and at the time, I didn't fully understand it. But, in the interest of closing out this question, I'll point to the following documentation for future reference:
NSBundle Class Reference
which refers to the following:
Resource Programming Guide
In the second document, refer to the section "String REsources -> Loading String Resources Into Your Code"
The solution contains a properly configured Root.strings file, which shows up in the file list like this:

Resources