Localization in Xcode 10 - ios

I have an app where all the user facing strings use NSLocalizableString(). I've never localized an app so I watched WWDC 2018 video New Localization Workflows in Xcode 10. From there I created a new project, a Single View App. Base localization is on by default. On the storyboard I added one UILabel and constrained it in the center of the view. I added an outlet to it in the view controller:
#IBOutlet weak var label: UILabel!
And I set its text in viewDidLoad:
label.text = NSLocalizedString("Hello World", comment: "")
Build and run and it works fine.
Now I add a localization. I select the project and add Spanish.
Then I Export For Localization... on the Editor menu. I open the file through this path:
es.xcloc -> LocalizedContents -> es.xliff
In there I find the "Hello World" key and I change the value to "Hola Mundo" and save the file.
...
<file original="MyProject/en.lproj/Localizable.strings" datatype="plaintext" source-language="en" target-language="es">
<header>
<tool tool-id="com.apple.dt.xcode" tool-name="Xcode" tool-version="10.1" build-num="10B61"/>
</header>
<body>
<trans-unit id="Hello World">
<source>Hola Mundo</source>
<note>No comment provided by engineer.</note>
</trans-unit>
</body>
</file>
...
Then back to Xcode and Import Localizations. Here's the first problem:
Why is this a Mismatched Translation?
I go ahead and import anyway. I edit the scheme as follows:
When I run I get "HELLO WORLD". It's uppercase because that's the localization debugging indicating there's no translation.
What am I missing?

We have a few problems here. First, when you do NSLocalizedString("Hello World", comment: "") what is actually happening is that you try to fetch a localized string with key Hello World and since Xcode can't find that key, it returns the key itself, which you set for the label. So, for best practices, consider using a key that doesn't contain a white space on it. Maybe something like hello-world-label. Then, it would be:
NSLocalizedString("hello-world-label", comment: "")
This will generate the following after you export for localization:
<trans-unit id="hello-world-label">
<source>hello-world-label</source>
<note>No comment provided by engineer.</note>
</trans-unit>
And now you have to add <target> tag to specify the actual translation. Therefore:
<trans-unit id="hello-world-label">
<source>hello-world-label</source>
<target>Hola Mundo</target>
<note>No comment provided by engineer.</note>
</trans-unit>
Then just import back the localizations and you should be fine.
EDIT
After doing that, a Localizable.strings file should have been generated. Click on it and take a look into the File Inspector on the right side of Xcode under the Localization section. You will that the file is only localized for Spanish. So go ahead and select English checkbox. Then, your Localizable.strings will be editable for both English and Spanish, as you can see on the figure below:
Now you edit the English version of that file for having both languages in your app.

Related

How to use a string defined in project into UILabel defined in Storyboard?

Basically in android studio there is a string.xml file in which you can define the strings that you will be using in your project.
Below I am giving the Android examples of how this is done:
String defined in string.xml:
<string name="lbl_login_banner">Find Professionals</string>
In the layout.xml (which would be equivalent of .storyboard, .xib or .nib):
android:text="#string/lbl_login_banner"
Is it possible to replicate this in Xcode version 9.2 or Swift 4.
In iOS you have to create Localizable.strings file, and inside that file you can define your strings.
"lbl_login_banner" = "Find Professionals";
How you can use it -
In Storyboard you can use the constant in label value like -
In Code -
myLabel.text = NSLocalizedString(lbl_login_banner, comment: "")
You can read documentation on how to use NSLocalizedString.
You can follow this tutorial on how to create and use Localizable.strings file.

Xcode 6.1.1 import localisations error "XLIFF id attribute missing"

I recently exported XLIFF files using Xcode's Export for localisation tool. The files were translated and I then tried to import them but I keep getting the error "XLIFF id attribute missing". I thought maybe one of the translators messed up the file so as a test I re-exported the XLIFF file and tried to import them without making any changes to them and still got the error "XLIFF id attribute missing".
Any ideas whats going on here?
For me, this was related to this question: Xcode 6 localization. export .xliff file will cut the string when the string is like “this is sample \”text\“”
My string file looked like this:
"some english \"text\"" = "some english \"text\""
Exported in the XLIFF, it looked like this:
<trans-unit id="">
<target>some english \</target>
<source>some english \</target>
<note></note>
</trans-unit>
My translators faithfully translated it: (simulated)
<trans-unit id="">
<target>some english \</target>
<source>xyz abcdefg \</source>
<note></note>
</trans-unit>
Upon attempting to import the translated XLIFF, I received the XLIFF id attribute missing error.
To fix this, I went into my strings file and changed the entry to this:
"some english 'text'" = "some english 'text'"
and the xliff...
<trans-unit id="some english 'text'">
<target>some english 'text'</target>
<source>xyz abcdefg</source>
<note></note>
</trans-unit>
...and then imported, and it worked. Obviously at this point the translation for that one string is wrong and needs to be redone, but at least the rest of the text imported.
Edit:
I've been looking for a solution to this. I found that using curly quotes does export correctly, so the string: some english “text” produces the xliff:
<trans-unit id="some english “text”">
<source>some english “text”</source>
</trans-unit>
That's the only way I know of to do this, currently.

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

NSLocalizedString is it really working?

I misunderstand NSLocalizedString.
I have a project with French and English .strings files.
In project target - Localizations I have 6 files in French and English.
In english.strings file:
"hello" = "world";
In french.strings file:
"hello" = "salut";
When I write a simple line of code to change label text:
exerciseDescription.text = NSLocalizedString(#"hello", #"no comment");
Output is: hello
I changed in iOS simulator : settings - general - international - language - French/English - Done
And output is again: hello
I thought it should be world or salut...
You're using it in correct way but not added properly,
Localized filename should always be named Localizable.strings, and that file is within the particular language folder, for English, en.lproj and for French fr.lproj like wise.
You are using it right, but as #Hemang mentioned you should change your filename to Localizable.strings as this is the default.
Furthermore I would like to suggest cleaning the build folder, and removing the application from the simulator. That helped for me in one case.

Why is my app not picking up the text in the Localizable.strings file?

I'm trying to localise my iPhone app, and I've tried to Google for help and look on here but I can't find anything that I am doing incorrectly.
I have created a Localizable.strings file, which currently only contains:
/*
Localizable.strings
AppName
Created by Joe on 25/10/2011.
Copyright (c) 2011 xxx. All rights reserved.
*/
"DestinationHeader" = "I am going to";
I then went to the Localization tab on the right in XCode, and added "English" to the file. Now the file appears in my en.lproj folder in the bundle.
Then, in the view controller, I've added the line:
sectionTitle = NSLocalizedString(#"destinationHeader", nil);
But all that seems to be happening is the text "destinationHeader" is appearing instead of "I am going to". I'm obviously doing something wrong, does anyone have any ideas?
Thanks!
:-Joe
Try this:
sectionTitle = NSLocalizedString(#"DestinationHeader", nil);
You should enter the same value of key in localizable.strings and it is case sensitive.

Resources