.lproj and Localizable.strings : project with localized text? - objective-c

i was reading the doc about localization and internationalization , but it's not really clear for me : can you explain me the link between the .lproj files, and/or the Localizable.strings file (if those are connected) ?
Or by any chance, if you know where i could see/download an example of an app using localized text?
I've seen the Localizable.strings file with the sample code 'NavBar' (from the doc), but nothing yet about a sample code using .lproj files.
Thanks for your help

It's very simple: create a folder for each localization, with the localization name followed by ".lproj", there you put any localizable file (you can also localize images) and your Localizable.strings (which contains the text returned by the obj-c API for localization). Don't forget to add the files in your resources, in Xcode.
For exemple, if you have 3 localizations, you could have this structure:
PROJECT_DIR/
- en.lproj/
Localizable.strings
- it.lproj/
Localizable.strings
- pt.lproj/
Localizable.strings
en.lproj/Localizable.strings have the english strings, and so on. Add the three Localizable.strings on your Xcode (it will be smart enough to group then) and it's done.
To use the localized strings just call NSLocalizedString(NSString *key, NSString *comment), it will automatically look for the key in your Localizable.strings file for the current locale and return the value associated with the key. For more information about NSLocalizedString see NSLocalizedString.

Related

NSLocalizedString doesn't work

I want to use NSLocalizedString in my app but it always failed. What i do is:
Define 3 Localizations in Project Properties (See screenshot bellow)
Create a new file: Resource Strings File
Check in the app bundle if file.strings is there
Then I use NSLocalizedStrings as follow but it doesn't work!
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
NSString *v1 = NSLocalizedString(#"MyWindow", nil);
//NSString *v1 = [[NSBundle mainBundle] localizedStringForKey:(#"MyWindow") value:#"" table:nil];
[label setStringValue:v1];
}
In my 3 .strings files I define the below key/value:
"MyWindow" = "Ma Fenetre";
Normally, my label should display "Ma Fenetre" and not "MyWindows"
You can download an example project here and tell me where is the issue.
Bellow the content of Resources folder in my app bundle :
DerivedData om$ find test/Build/Products/Debug/test.app/Contents/Resources/
test/Build/Products/Debug/test.app/Contents/Resources/
test/Build/Products/Debug/test.app/Contents/Resources//de.lproj
test/Build/Products/Debug/test.app/Contents/Resources//de.lproj/File.strings
test/Build/Products/Debug/test.app/Contents/Resources//en.lproj
test/Build/Products/Debug/test.app/Contents/Resources//en.lproj/File.strings
test/Build/Products/Debug/test.app/Contents/Resources//fr.lproj
test/Build/Products/Debug/test.app/Contents/Resources//fr.lproj/File.strings
test/Build/Products/Debug/test.app/Contents/Resources//MainMenu.nib
Thanks
Elfoiros
NSLocalizedString uses Localizable.strings file by default. Change your File.strings name and try again.
For every time you make changes in .Strings file you need to clean your project and remove application from device and simulator. This is the only way to develop Localised application.
Have a happy coding.!
As Adam stated, NSLocalizedString uses Localizable.strings for a lookup table. To specify a custom table, use:
NSLocalizedStringFromTable(#"MyWindow", #"File");
Documentation
Need to take care over case sensitive file names: Localizable.strings not localizable.strings. Simply rename in Finder, delete reference in Xcode project and add Localizable string back to the project. Then make sure the appropriate Localization boxes are selected in the file inspector. Don't know if there's a better slicker process.
I had a similar issue as described here but the solution was different. Somehow I managed to mangle how the Localization.strings file was being referenced in the project.
Removing the Localizable.strings file and readding it fixed the problem for me.

Multilanguage application and .string file

I'm looking at some example of using NSLocalizedString and there is a .string file in project where i can find:
"Loading" = "WczytujÄ™...";
And then in a app code i can find:
return NSLocalizedString(#"Loading", #"");
How can i create multilanguage version of my app using that? Or there is a other way?
I want to have button to change language.
That's exactly how you localize an iOS app. Everywhere where you want to show some localized text you write NSLocalizedString(#"Something", #"Optional Description"). Then you add a file called Localized.strings to your app's resources and translate the text there:
"Something" = "Something in other language";
You can add new languages by selecting the .strings file and on the right side of XCode add a new language:
You can also have additional, separate .strings files in your project. Then you have to specify where to look for a translation: NSLocalizedStringFromTable(string, filename, comment)

My localized strings are not detected

I'm trying to localize my strings in Xcode 4, but the only thing I get are the keys, not the values.
I've localized the strings using NSLocalizedString(key, comment).
I've generated the file Localizable.strings using genstrings from the command line.
I've dragged and dropped Localizable.strings from Finder to Xcode (into the Resources folder).
I've localized Localizable.strings and added the appropriate countries.
I've made sure the encoding is UTF-16.
I've translated the different Localizable.strings for each country.
I've rechecked that the encoding is still UTF-16.
I've checked that each statement in Localizable.strings ends with a semi colon.
Still not working.
Any idea of what I've missed?
edit
Here a part of my Localizable.strings
/* Text displayed on the comment button if there are more than one comment. */
"CommentButtonTextPlural" = "comments";
/* Text displayed on the comment button if there is exacly one comment. */
"CommentButtonTextSingular" = "comment";
/* Text displayed on the post button. */
"CommentViewPostButtonLabel" = "Post";
/* Default text displayed in the area where the user types in a comment. */
"CommentViewWriteAComment" = "Write a comment...";
/* Error message explaining that a document isn't viewable on the user's device. */
"DocumentRendererStatusDetailedErrorText" = "This document is not viewable on this device.";
/* Message explaining that the document is downloading. */
"DocumentRendererStatusDownloadingText" = "Downloading...";
You may be putting the Localizable.strings file in the wrong location so it's using the default translations.
When you add a new language, Xcode creates a new folder in your App Directory which ends in .lproj (so for English it would be en.lproj) and that is where your Localizable.strings file for English should be.
If you want a French one, add the French Language and Xcode will generate a fr.lproj directory and that is where the French Localizable.strings file will be located.
From your description, putting it in the Resources Folder is incorrect.
Your Localization should look like the following:
and on your desktop:
I managed to get it working.
What I did was remove the Localizable.strings (from the harddrive, I haven't touched the files located in the xx.lproj folders) file which genstrings created for me and removed the app-file on my device.

Objective-C - document header and not code file?

I would like to start documenting my code properly, but am unsure where the best place is to put it for HeaderDoc to read. I read the following phrase in the HeaderDoc documentation which makes me think the intended location (from Apple's point of view) is in the header files?
Instead of specifying a single input file (as above), you can specify an input directory if you wish. HeaderDoc will process every .h file in the input directory (and all of its subdirectories), generating an output directory of HTML files for each header that contains HeaderDoc comments.
Is this right? Does it make a different where I put my documentation?

How to rename infoplist.strings file? [duplicate]

This question already has an answer here:
XCode - Multiple targets, Multiple *internationalized* names?
(1 answer)
Closed 2 years ago.
Is it possible to use other names for infoplist.strings file?
My problem: I want to create more than one target with different localized app names.
You just create localized version of the same file. And in there you set the name for each language for CFBundleDisplayName.
For example:
In the english file:
CFBundleDisplayName = "English name of the app"
In the French file:
CFBundleDisplayName = "French name of the app"
iOS will load the proper localization file. And if a language is missing it will use the default language in the info.plist. In my case, it en.
I've had what sounds like the same issue. I'm building two different apps from the same files, just with two different XCode projects, but the InfoPlist.strings files need to be different as they contain the name of the app. A simple solution I found is to use two separate folders inside of which your localized infoplist.strings are. For example:
+Version_A
+en.lproj
InfoPlist.strings
+fr.lproj
InfoPlist.strings
+Version_B
+en.lproj
InfoPlist.strings
+fr.lproj
InfoPlist.strings
Then in each XCode project you simply include the one relevant location (Version_A or Version_B) and not the other. If you do that too, check that the Copy Bundle Resources under your Build Phases pick the correct version (you can click Show in Finder from there).
You could use
NSLocalizedStringFromTable(#"key", #"myStringsFile", #"comment");
where you can specify the name of the .strings file which you can then make dependent on the target.