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.
Related
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.
Here's our localization workflow:
Build stuff in interface builder
Export project for localization
Translator looks at xliff files and applies translations for new strings (Only new untranslated strings)
Import xliff into project
This works fine for building new stuff. But if the developer changes the text in a label in storyboard that has already been translated, he will have to remember to delete the translation for that label, so the translater sees that string as untranslated. If the developer forgets to delete that translation, the translation will be wrong, which is a very hard error to find.
When code is localized like this:
var testString = NSLocalizedString("Some text in english", comment: "just a test string")
The string ("Some text in english") defines the key in the xliff-file, which means that if the string is changed, then the exported xliff file will automatically have a new string that needs to be translated.
I've tried to solve the problem with xcode's "export for localization..." function, with genstrings, and with BartyCrounch, but all methods seems to use the UI Element's Object ID as a key for the .strings-files. Which means it won't react to changes in the actual string.
The only solution I've found so far, is to set every string that need translation in the storyboards in code via a IBOutlet, but this is a quite comprehensive solution.
Do you know any tools or methods that solve this problem?
I tried what you said and understood what you meant. I think the solution is straightforward.
First, I added a new button in storyboard, named it "Test". Then I translated it in Chinese, imported the translation. Changed "Test" to "GoGo" and exported the translation again. The xliff part was changed from
<trans-unit id="jso-qF-Z1t.title">
<source>Test</source>
<target>测试</target>
<note>Class = "NSButtonCell"; title = "Test"; ObjectID = "jso-qF-Z1t";</note>
</trans-unit>
to
<trans-unit id="jso-qF-Z1t.title">
<source>GoGo</source>
<target>测试</target>
<note>Class = "NSButtonCell"; title = "GoGo"; ObjectID = "jso-qF-Z1t";</note>
</trans-unit>
What to do next
I don't know if there is a ready-tool for this. But you can use Git as it can always find the diffs.
Steps:
add you translation folder to git
commit current files
export new translations from Xcode
look at diffs with git
As you can see in the picture, git already shows what changes, that is where you should translate again.
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.
This is a follow up question (and answer) on the How to localize my app with Xcode 4? question.
How do I localize my app with Xcode 5.x?
It's quite simple once you understand it.
The first thing you want to do is add a localization file to your project.
To do so, simply select your project's main group
,
then, from the toolbar, select File → New → File... (or just hold down ⌘N)
.
Under the Resource category, select Strings File ,
and name it Localizable.strings (note that it is case sensitive) .
Now that we have our localizable file, we can click on the Localize... button, in the File Inspector
.
Xcode is going to ask you if you want to localize the file, just click on Localize with Base selected
.
Now this next part is a bit tricky. We need to enter our project's Info section, to do so, click on the project file in Xcode's Navigator, then to your right you'll see a category named PROJECT, click on your project file under this category
.
Now we can add our desired language under the Localizations category. I'll add Norwegian
.
It's important that we only leave our Localizable.strings file checked in the menu that appears
.
Now we can expand our Localizable.strings file in the Navigator to see our localizable files
.
We now how our Base file (within our Localizable.strings file), which will be our app's "main language", and our previously selected language.
It's important to know that the structure of these files needs to be identical. You'll see what I mean in just a sec.
In our Base, I'll add a string named it_worked, and add it's localization
.
And in our previously selected language (In my case Norwegian), i'll add the same string it_worked (to keep the structure), but with a different localization
.
Now that we have our localized file, we can make our app read it when needed.
I added a UILabel to my app, so that we can make our app display the localized text.
[myLabel setText:NSLocalizedString(#"it_worked", nil)];
Now if I launch my app, we'll see our base language
,
and if I change the language of the simulator to Norwegian, we'll see our other language
.
You don't need to add the uilabel and change the text on code.
You can take advanced of the User Defined Runtime Attributes:
http://cupobjc.blogspot.com.es/2014/04/interfaz-builder-localization.html
First define a new category for UILabel:
#import "UILabel+Localized.h"
#implementation UILabel (Localized)
-(void) setTextLocalized:(NSString *)aText{
[self setText:NSLocalizedString(aText, nil)];
}
#end
Then in the interface builder, User Defined Runtime Attributes :
textLocalized String your string to localized
I have a questions:
I should to do a multilanguage app and I would to know a method to manage graphics elements quickly.
A method is this:
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
if ([countryCode isEqualToString:#"US"]){
//something to do
}
in this example I can set graphics elements in every viewcontroller, but there isn't a quickly solution to do all? at example in plsit file?
Yes, I think you can.
See this tutorial:
http://www.raywenderlich.com/2876/how-to-localize-an-iphone-app-tutorial
There is a paragraph where it says you can have different folders for different languages.
If that dos now work, at least you can have a singleton class where you check the "countryCode" just once, set it there and refer to it in your "if" statements.
I hope this helps.
If you are using XCode 4 just create a .strings file then in the file inspector you just hit the " +" button then you choose the language to add. In each file you will have (key, value) pairs.For example ( "firstViewTitle" = "First View"; ). the in the .m file just assign an NSLocalizedString to the title of the UI object. Befor running try to clean the target. Hope i made my self clear
You can localize every item you want in an app.
E.g. once you have imported a resource (e.g. image) inside Xcode, just select the resource in the Navigator (left side) and then in the Assistant (right side) search for the "Localization" group and click the "+" button. The first time it will create a new folder and will move the resource to that folder (default is "English"). Later you can add as many languages as you want, for each language a dedicated folder will be created by Xcode and then you will be able to change your resource locally for each language. E.g. if you have an "image button" with a text you want to localize, once you have created all these folders you will be able to change the image file locally for each. Only requirement: keep the same name of course. Then iOS will be able to fetch the right file based on the current device/simulator localization setting.
Be aware that sometime if you change stuff the bundle is not compiled correctly and to avoid headaches while debugging the easiest solution is to clean the build and remove and re-install the app from the device/simulator.