I have a questions:
I should to do a multilanguage app and I would to know a method to manage graphics elements quickly.
A method is this:
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
if ([countryCode isEqualToString:#"US"]){
//something to do
}
in this example I can set graphics elements in every viewcontroller, but there isn't a quickly solution to do all? at example in plsit file?
Yes, I think you can.
See this tutorial:
http://www.raywenderlich.com/2876/how-to-localize-an-iphone-app-tutorial
There is a paragraph where it says you can have different folders for different languages.
If that dos now work, at least you can have a singleton class where you check the "countryCode" just once, set it there and refer to it in your "if" statements.
I hope this helps.
If you are using XCode 4 just create a .strings file then in the file inspector you just hit the " +" button then you choose the language to add. In each file you will have (key, value) pairs.For example ( "firstViewTitle" = "First View"; ). the in the .m file just assign an NSLocalizedString to the title of the UI object. Befor running try to clean the target. Hope i made my self clear
You can localize every item you want in an app.
E.g. once you have imported a resource (e.g. image) inside Xcode, just select the resource in the Navigator (left side) and then in the Assistant (right side) search for the "Localization" group and click the "+" button. The first time it will create a new folder and will move the resource to that folder (default is "English"). Later you can add as many languages as you want, for each language a dedicated folder will be created by Xcode and then you will be able to change your resource locally for each language. E.g. if you have an "image button" with a text you want to localize, once you have created all these folders you will be able to change the image file locally for each. Only requirement: keep the same name of course. Then iOS will be able to fetch the right file based on the current device/simulator localization setting.
Be aware that sometime if you change stuff the bundle is not compiled correctly and to avoid headaches while debugging the easiest solution is to clean the build and remove and re-install the app from the device/simulator.
Related
I'm new to IOS and currently I'm working on Localization of Displaying Apps icon.
and I made InfoPlist.strings
and then I put the text for like this in my InfoPlist.strings(MyLanguage) file
"CFBundleDisplayName" = "sometext sometext andlasttext ";
and it literally shows every text, include   on iphone emulators home screen.
and of course either
"CFBundleDisplayName" = "sometext sometext andlasttext";
is also not working! could you help me?
This solution works for me before iOS11 consider space as double space.
But from iOS 11 Apple has stopped truncating for longer app names. I thing if app name count is greater then 12 to 14, the spaces will be removed. Otherwise, they'll still exist.
= 2 spaces
Localizing your app display name is actually a very simple task, so let’s just get right into it.
Set up Localization
First, make sure you set up localization for another language (Note, this is not all that is required to completely localize an app, just the display name). Select your project in the project navigator, go to Localizations, then select a language to localize your display name.
Create a Strings File
Next, you have to create a Strings file that will contain the localized name to be used in your project. Go to File > New > File, select your OS target, go to Resources, and select the Strings file template.
Select the template, enter InfoPlist into the name field, and create the file.
Adding Localized Strings
Once you have created your strings file, open it and select all of the languages you would like to localize for in the inspector. In your base language file, add the following:
/* Localized Bundle Display Name */
"CFBundleDisplayName" = "Your_Localized_Name_In_Native_Language";
Then, enter the same in every other language file, swapping in your translated name in the place of your native name as so:
/* Localized Bundle Display Name */
"CFBundleDisplayName" = "Localized_Name_In_Other_Language";
When you are done, each file should look like the following:
Add Localization Keys to Info.plist
The last thing you have to do is add the necessary keys to your Info.plist file to enable your app to read and swap in your localized display name when appropriate. The first key you will add is the “bundle display name” key, it will look like so:
Bundle display name = $(PRODUCT_NAME)
You also have to add the “application has localized display name” key and set it to yes, which will look like the following:
Application has localized display name = YES
These keys in Info.plist will look like the following:
Thats all there is to it! Your app name will now be displayed in all languages that you localize to.
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.
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.
This is a follow up question (and answer) on the How to localize my app with Xcode 4? question.
How do I localize my app with Xcode 5.x?
It's quite simple once you understand it.
The first thing you want to do is add a localization file to your project.
To do so, simply select your project's main group
,
then, from the toolbar, select File → New → File... (or just hold down ⌘N)
.
Under the Resource category, select Strings File ,
and name it Localizable.strings (note that it is case sensitive) .
Now that we have our localizable file, we can click on the Localize... button, in the File Inspector
.
Xcode is going to ask you if you want to localize the file, just click on Localize with Base selected
.
Now this next part is a bit tricky. We need to enter our project's Info section, to do so, click on the project file in Xcode's Navigator, then to your right you'll see a category named PROJECT, click on your project file under this category
.
Now we can add our desired language under the Localizations category. I'll add Norwegian
.
It's important that we only leave our Localizable.strings file checked in the menu that appears
.
Now we can expand our Localizable.strings file in the Navigator to see our localizable files
.
We now how our Base file (within our Localizable.strings file), which will be our app's "main language", and our previously selected language.
It's important to know that the structure of these files needs to be identical. You'll see what I mean in just a sec.
In our Base, I'll add a string named it_worked, and add it's localization
.
And in our previously selected language (In my case Norwegian), i'll add the same string it_worked (to keep the structure), but with a different localization
.
Now that we have our localized file, we can make our app read it when needed.
I added a UILabel to my app, so that we can make our app display the localized text.
[myLabel setText:NSLocalizedString(#"it_worked", nil)];
Now if I launch my app, we'll see our base language
,
and if I change the language of the simulator to Norwegian, we'll see our other language
.
You don't need to add the uilabel and change the text on code.
You can take advanced of the User Defined Runtime Attributes:
http://cupobjc.blogspot.com.es/2014/04/interfaz-builder-localization.html
First define a new category for UILabel:
#import "UILabel+Localized.h"
#implementation UILabel (Localized)
-(void) setTextLocalized:(NSString *)aText{
[self setText:NSLocalizedString(aText, nil)];
}
#end
Then in the interface builder, User Defined Runtime Attributes :
textLocalized String your string to localized
We can localize an image in the File Inspector using Localize... like this:
Then we can get this:
But now, I use Images.xcassets to manage my images in my iOS project, how should I localize these images in Images.xcassets?
If you are using an Asset Catalog:
Asset catalog elements are now localizable. In the information panel for an asset, there is a "Localization" section that lists all the languages you've set up for your project in the project settings. If you don't select any of them, your images will be deemed "universal" (i.e., they will adopt the default behavior). If you select one or more of them, you will be able to select different images for "universal" and any alternate language.
For example, if your base language is English, and you want Spanish-language alternates, select only "Spanish" and drag in the alternate versions in the Spanish wells, and leave the English versions as the Universal. Then, at run-time, if the chosen language is Spanish, then the Spanish-language image will be pulled. Otherwise, it will pull the English version. (Or, if you want specific English and Spanish versions that are both different from "everything else", also check English and drag in the corresponding English and Universal images.)
If you need to determine localized images at run time without using the Asset Catalog:
While there's (apparently) not currently a way to explicitly localize the xcassets file directly, you could instead localize the name of the asset to pull using your Localizable.strings file. For instance, say you have a graphic logo treatment for the main menu of a game that needs to be localized. You could put the localized logo treatments in the assets file with different names, add the names to your Localizable.strings file, and then fetch the appropriate image with code like this:
UIImage *img = [UIImage imageNamed:NSLocalizedString(#"MAIN_MENU_IMAGE", nil)];
That "Localize..." button is back in Xcode 11! So now you can localize your images and assets in the Asset catalog as you expect:
When Apple gives you lemons, make lemonade, or in this case, lemonade_en, lemonade_es or whatever suits your needs.
First, I create an entry for each desired language in my assets file like so:
Next, you will need to get a language code for the device. The important thing to remember here is that the user may have an unsupported language, so if that is the case, return your default (I use English).
The following extension of UIApplication will handle all of this for you:
extension UIApplication {
var languageCode: String {
let supportedLanguageCodes = ["en", "es", "fr"]
let languageCode = Locale.current.languageCode ?? "en"
return supportedLanguageCodes.contains(languageCode) ? languageCode : "en"
}
}
This extension does the following:
Creates an array of the languages my app supports
Obtains the current device's language code, or returns a default value if this is nil
Finally, it checks to see if the current code is in my supported list. If it is, it returns it, otherwise it returns my default code
Now, we combine the two to get the proper image:
let languageCode = UIApplication.shared.languageCode
let image = UIImage(named: "access_\(languageCode)")
After some search on the Internet, I think this feature is not provide by xcassets right now. Therefore, just don't use xcassets to manage your localization images.
I found another way:
add in you asset.xcassets a folder for each language (for instance: en,it,de)
set the flag "Provides Namespace" to each folder
copy all images and assets in that folder
In your code load the assets by calling
let languageCode = UIApplication.shared.languageCode
let image = UIImage(named: "\(languageCode)\assetname")
At the moment, I pull out the images that needs localization from Images.xcassets and put it in the standard project folder. I leave the non-localized images in the .xcassets. I use your method for the images that need to be localized.