Localizable.strings is not working - ios

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

Related

Localization in Xcode 10

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.

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.

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.

NSLocalizedString only retrieves the key, not the value in Localizable.strings (IOS)

I've made a strings file named "Localizable.strings" and added two languages to it, like so:
"CONNECTIONERROR" = "Check that you have a working internet connection.";
"CONNECTIONERRORTITLE" = "Network error";
I have also converted the files to Unicode UTF-8
However, when I create a UIAlertView like this:
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(#"CONNECTIONERRORITLE",nil)
message:NSLocalizedString(#"CONNECTIONERROR",nil)
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
the alert view only shows the key text, not the value. It works if I, for example, set a UITextviews text to NSLocalizedString(#"CONNECTIONERROR",nil), but the alert view only displays the key. Anyone know what's wrong?
In my case it was because I had mistakenly named the file "Localization.strings" and hadn't noticed (it has to be named Localizable.strings). As explained previously the symptom is because the compiler cannot find the string. Otherwise the cause could be any number of things but usually it's a missing semi colon or quotation mark. These are hard to find when you're doing a lot of localizations at once. The lesson learned is to start building your localization file early on in your development process and build it as you go, so that they are easier to spot.
Same problem, solved using the filename: Localizable.strings
Change your .strings file name to Localizable.strings, it worked for me.
Double check that the Localizable.strings file is being added to
Targets -> BuildPhases -> Copy Bundle Resources
It hadn't been added automatically for me.
Edit 2021: with XCode 12 the Localizable.strings have to be added to
Targets -> Build Phases -> Compile resources
I have been searching the solution for 5 hours, I tried everything I could to make my app localization working.
The problem was that one of the Pods of my project had a Localizable.strings file (actually it was Parse pod that had not renamed it). Therefore my Localizable.strings file was not recognized by my app.
I fixed the issue by changing the name of the file to "MyappnameLocalizable.strings" and using NSLocalizedString this way:
NSLocalizedString("key", tableName: "MyappnameLocalizable", comment: "comment")
Tested the app on an actual device and it worked
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)
cd to there and delete all copies of Localization.strings found there.
For some reason the usual rubber chicken voodoo of build clean, quit iOS Simulator, quit XCode, etc wasn't working, but this did. At least for me, today.
This is happening when the runtime can't find the specified key, for whatever reason. In your case, it's most likely due to a typo: CONNECTIONERRORITLE is missing a T for TITLE. Also pay attention to any warnings/error when compiling regarding the Localizable.strings file: if there are unbalanced " or missing ; the file cannot be compiled/read correctly.
Change the name of the file to Localizable.strings, make sure the target in the file inspector is set.
To avoid syntax errors right click on Localizable.strings file->open as->ASCII property list
Also, cleaning the project and building again helped in my case.
NSLocalizedString usage means you need the EXACT case and spelling of your key in order to get the content from it. You'll notice that this one says
NSLocalizedString(#"CONNECTIONERRORITLE",nil)
when it should be
NSLocalizedString(#"CONNECTIONERRORTITLE",nil)
If you look at the last part of the first one, it says 'ITLE', not 'TITLE'
Rename the InfoPlist.strings file to Localizable.strings (double clic) and then you will get the correct string for that key.
If you wrote double semicolons at the end of a line, NSLocalization does not working.
You should check Localizable.strings file if there is ';;'
When you are developing an SDK. You need some extra operation.
1) create Localizable.strings as usual in YourLocalizeDemoSDK.
2) create the same Localizable.strings in YourLocalizeDemo.
3) find your Bundle Path of YourLocalizeDemoSDK.
Swift4:
// if you use NSLocalizeString in NSObject, you can use it like this
let value = NSLocalizedString("key", tableName: nil, bundle: Bundle(for: type(of: self)), value: "", comment: "")
Bundle(for: type(of: self)) helps you to find the bundle in YourLocalizeDemoSDK. If you use Bundle.main instead, you will get a wrong value(in fact it will be the same string with the key).
But if you want to use the String extension mentioned by dr OX. You need to do some more. The origin extension looks like this.
extension String {
var localized: String {
return NSLocalizedString(self, tableName: nil, bundle: Bundle.main, value: "", comment: "")
}
}
As we know, we are developing an SDK, Bundle.main will get the bundle of YourLocalizeDemo's bundle. That's not what we want. We need the bundle in YourLocalizeDemoSDK. This is a trick to find it quickly.
Run the code below in a NSObject instance in YourLocalizeDemoSDK. And you will get the URL of YourLocalizeDemoSDK.
let bundleURLOfSDK = Bundle(for: type(of: self)).bundleURL
let mainBundleURL = Bundle.main.bundleURL
Print both of the two url, you will find that we can build bundleURLofSDK base on mainBundleURL. In this case, it will be:
let bundle = Bundle(url: Bundle.main.bundleURL.appendingPathComponent("Frameworks").appendingPathComponent("YourLocalizeDemoSDK.framework")) ?? Bundle.main
And the String extension will be:
extension String {
var localized: String {
let bundle = Bundle(url: Bundle.main.bundleURL.appendingPathComponent("Frameworks").appendingPathComponent("YourLocalizeDemoSDK.framework")) ?? Bundle.main
return NSLocalizedString(self, tableName: nil, bundle: bundle, value: "", comment: "")
}
}
Hope it helps.
To find out if the Localizable.strings file is being found, check the content of your .app build and you can also do this in code:
//en is for English for example so specify yours here
NSString *path = [[NSBundle mainBundle] pathForResource:#"en" ofType:#"lproj"];
If path is NULL, it means file not found.
If you have any extra semicolon in your strings file, it doesnt localise.
I faced a similar problem, suddenly my localizable strings didn't work at all. Then I used file-compare with the old .strings copy, and at-last I found I have accidentally deleted a key in it.
So Definitely if the format is wrong Xcode will not read the strings for you.
This is the format it expects "Key" = "Value";
Because I stumbled upon this when looking for the answer to a similar problem, I'm leaving my solution here:
If your key has "\u2028" in it instead of "\n", it will always return just the key and not the value. Seems to be a bug with localization.
We were dealing with an issue in which some keys were not getting their values despite all of them existing in Localizable.strings.
Turns out that the developer who built the file added two semi-colons at one line, and that caused the parsing of this file to be quite erratic.
After I found the two semi-colons one next to the other and got rid of one of them, the app wouldn't compile anymore. At this point I was able to find some lines that didn't have semi-colons. After fixing those and the app compiled, all my strings worked fine.
So having two semi-colons in the same line caused the file to compile despite it missing semi-colons in other lines.
It looks like Apple's parser for .strings file is very primitive.
Unfortunately, the plist linter was useless in our case. To find and fix the issue, I copied and pasted the entire contents of our Localizable.strings file (which is over 2000 lines long), and started copying it back in 20 line chunks and compiling each time. It was the only way to find an issue that the linter would not catch.
Put an ; at end of the lines in the Localizable.strings files.
Resetting the simulator settings worked for me.
iOS Simulator > Reset Content and Settings...
In my case, I tried clean project and delete derived data still not work. I remove several line breaks in the strings file and then Xcode find the strings again.
None of the suggested solutions worked for me, but I did solve the issue by deleting the .app file in Products
I had the issue when one language was working properly and other language worked half of the time and other time I was getting the key, instead of localized version of that key.
Problem in my case was that after manually merging version control conflict, there was extra line of '>>>>>' left, project didn't complain (not like when you miss the semicolon, it complains) and was building properly, so every key before that '>>>>>' was being translated and every key after, not.
If more simpler solutions fails, you might have to go through every line and look for extra characters (not only '>>>>>', other extra characters too), or if you are using the version control get older, stable version of Localizable.strings file and manually go through changes and add later commits.
The first line of the localization file must contain a mapping, otherwise the SDK won't read the file.
Be sure to not put '#' in front of your key or value like so:
#"key" = #"value";
It should be just:
"key" = "value";
The '#' only goes in front of the key when accessing it:
NSWebLocalizedString(#"key", #"label for coder");
I had everything working when suddenly localization just stopped translating strings. This means the file is somehow unreadable to Xcode.
This had happened to me because I had pasted a bad character in the Localized.strings file. When I deleted the few lines with the offending character (a non unicode character? bad quote signs? Couldn't tell) everything went back to normal.
To find the offending lines I made a breakpoint, and manually translated the strings in my file in the debugger, until I hit the first one that won't translate.
Did you tried cleaning and rebuilding the project?
For xcode 9.2 removing "Localizable.strings" files from the simulators for the project using console solved it for me. Here is the commands for the lazy ones like me ;)
Do not forget to replace YOUR_APP_NAME_HERE with your project name
Shell script:
cd
cd Library/Developer/CoreSimulator/Devices/
find . -name "Localizable.strings" | grep "YOUR_APP_NAME_HERE.app" | xargs rm
If you are experiencing the problem with Unit Tests it will work in simulator ;)
Swift 4:
If you use more than one bundle such as you use it in an external framework:
var currentBundle = Bundle.main
NSLocalizedString("CONNECTIONERRORITLE", tableName: nil, bundle: currentBundle, value: "", comment: "")
I resolved it using this approach.
The localize file was created with the name main.Strings. Then, I open the main.Strings files (for each language added) and I renamed them manually with the name localize.strings and add them one by one to my project, also I deleted the main.strings.
The second thing to evaluate is: check . your file, all the keys have to end with ; and be sure all the quotes are properly opened and closed.
And you can use in swift 4 :
myButon.setTitle(NSLocalizedString("forgotpassword.key", comment: ""), for: UIControlState.normal)

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