Multilanguage application and .string file - objective-c

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)

Related

how to localize iOS 8 today extensions widget and also the widget name

I can't understand how I can localize today extensions, I want to create the Localize.strings file like a normal xcode project that for example have this string:
.en
"Apple" = "Apple"
.it
"Apple" = "Mela"
and I also want to know how I can localize the iOS 8 Today Extensions widget name.
I have tried to create a Strings File in Xcode, and I have tried to insert this:
"CFBundleDisplayName" = "App Name";
but the name doesn't change...
Add LSHasLocalizedDisplayName = YES to Info.plist file and add your localizations to InfoPlist.strings which you have to place under the language.proj folder per languages, for example de.lproj for German versions.
Before testing:
- Clean the project
- Remove the application from the simulator.
- Add language.proj folder references to extension's Supporting files part - to include them inside Copy bundle resources section in Build Phases of your extension target.
For strings in your containing app and Today Widget can share the same localization files. You can simply select Localizable.string, and in the File inspector check your today widget target in Target Membership. Then all your NSLocalizedString in the today widget share the same one with the containing localization strings.
For the today widget application name, then just add an InfoPlist.strings file, and
"CFBundleDisplayName" = "Translated Name";
"CFBundleName" = "Translated Name";
Just run your containing app for testing.

How do I get the XCode Project URL programmatically?

I'm curious if there is a way to programmatically get the location of the .xcodeproj package within an Objective-C (or Swift) class contained within that package. I'd like to make a simple utility that puts files directly into the containing folder based on various app events, but I would rather avoid hard coding the path.
Essentially I want to create a target (and a reusable class) that builds swift files for NSManagedObject subclasses based on the Core Data model present in the app.
I found out the trick here is to add an item to your plist file that contains value ${PROJECT_DIR}, then you can get the location in your code with
var projectPath = NSBundle.mainBundle().infoDictionary.objectForKey("com.myapp.project_dir") as String
This assumes the plist key is "com.myapp.project_dir", of course.

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.

.lproj and Localizable.strings : project with localized text?

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.

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.