Xcode Localization: Keep translation in sync with Storyboard - ios

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.

Related

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.

iOS Storyboard Localization: Manually editing generated files

I'm in the process of localizing an iOS application. I've run into a few issues though - the main one being storyboard localization. I need to add textual context to each string so that the translator can understand how the string is used. For example, in my storyboard localization for English, I will change:
/* Class = "UILabel"; text = "Label"; ObjectID = "1Sf-fE-WR8"; */
"1Sf-fE-WR8.text" = "Label";
to:
/* Context: this label is the header of the settings screen */
"1Sf-fE-WR8.text" = "Label";
Firstly - is this a bad idea? The problem is that if I ever have to re-generate the localization for the storyboard, all edits I've done on the file will disappear. How then, in the future, if I add a new element to the storyboard, will I get that element into the .strings file without regenerating it?
Note:
a) I have tried "Export For Localization", but this always results in an error "Localization failed to read a strings file"
b) The project is set up in a strange way: there is a parent project which has existing localizations, and we've added a sub-target which has it's own localized files, but the overall localization export mechanism applies to the project as a whole, and I'm worried that fiddling with it will corrupt the existing work.
Any help or thoughts are appreciated!
There's a "Comment for the localizer" text field that you can use to provide the label's usage context.
Not sure if it's still relevant to you or if it was present back in the days of Xcode 7.

XLIFF file, keys and long strings with NSLocalizedString

I want to move to xliff instead of translating Localizable.strings and Main.strings files but I found out that I'm using NSLocalizedString in an improper way (and I did it for 5 years actually...).
I don't like to have the translations directly inside my code, so I use a generic key and I do not write any comments:
NSLocalizedString("general.error", comment: "")
Then I include the string into the Localizable.strings
"general.error" = "An error occured";
So far so good (maybe). Now when I export xliff files I see that the source is just my generic key and obviously a translator cannot guess what to write as target for that key :/
So my question is: Is the only solution to move all the translations directly inside the NSLocalizedString?
NSLocalizedString("An error occured", comment: "")
or inside the comment... (I really don't like this solution)
And what if the string is really long? it seems so strange to put a string of 3 rows directly into the code :/
Any other interesting solution out there?
EDIT
I've already tried to use constants, but it seems that this solution doesn't work in swift. I've created a String.swift file where I've added constants:
let thisIsMyLonStringID = "An here I can put the long translation";
And I can use it in this way:
NSLocalizedString(thisIsMyLonStringID, comment: "")
When I export to XLIFF this string is not available in the xliff files though :(
I put here an answer with my temporary solution. It seems to work pretty well actually.
Instead of using a Base language for the Localizable.strings file I've used English, so I've just deselected Base from the file inspector -> Localizations area in Xcode and I've been prompt with a question like "which language would you like to use as base"... I've selected english.
Now I can continue using NSLocalizedString using a generic key and putting the translations in Localizable.strings when I export to xliff automatically the source is filled with the right translation and not with the key.
You need to make the call to NSLocalizedString directly in your Strings.swift file. For example:
// Strings.swift
let myString = NSLocalizedString("some very long string", comment: "")
// Usage
print(myString)
That way, the string export process will be able to determine the string literal that is passed in to NSLocalizedString.
Yes there is very elegant solution to your problem. Problem is very long string's are getting part of our code, making it harder to read and messy.
Solution:
Create LocalizationKeys.h
#ifndef Project_LocalizationKeys_h
#define Project_LocalizationKeys_h
static NSString *const LocalizationKeyForVeryLongString =
#"Your Very Very Long String";
#endif
In your code: ViewController.m
NSLocalizedString(LocalizationKeyForVeryLongString, "");
So above solution separated very long messy strings and replaced them with elegant and readable Key Strings, moreover now we have separate file for key strings, so whenever someone is needed to change or to lookup, directly refer to LocalizationKeys.h

Specify base localization for Localized formatted APNS strings

My base localizations are in storyboards, but also in a struct like this:
struct Strings {
struct Restaurant {
static let makeCall = NSLocalizedString("Restaurant-makeCall", value: "Call %#", comment: "Restaurant-makeCall: title for button that allows you to call a place")
}
//...many more
}
This works pretty well because it keeps them in one place, defines a key that is separate from the base translation value, and gives me autocomplete when I'm using them: Strings.Restaurant.makeCall
To enter translations, I use the xliff import/export process: editor > export for localization. I found genstrings had trouble with the longhand form of NSLocalizedString.
This all works well until I get to Localized APNS Messages. Given an loc-key and optional loc-args, They search for a matching localization key.
For the translations, this works, because there's a Localizable.strings built for each translation when I import the translated xliff file.
There is no Localizable.strings file for my base translation. I attempted to make one, but the xliff export does not notice those fields to add to other translations, so I think that's not the right way to do it.
How do I add base translations in a way that will work for APNS?
One functional, but duplicative approach is to define them in both places:
Define keys (and unused values) in the Strings struct. The xliff export notices these and this allows other languages to provide translations
Define keys and values in a Localizable.strings file you create for your base translation. This allows APNS to find the values when you're in the base language, but xliff export can't see these.
This works, but if somebody ever changes one without keeping the other file up-to-date, differences in translation will occur that will be hard to detect, so I don't like it very much.

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.

Resources