How to use localisation with image asset in Xcode? - ios

I need to localize my image in image asset folder in the xcode. Can anybody help me out to do it.

Here's an example:
Add 2 image sets to your Asset Catalog, one in English and one in Dutch, and postfix the name with the language code:
myImage-en
myImage-nl
Create string table entries (Localizable.strings) for each language as follows:
English file:
"myImage" = "myImage-en"
Dutch file:
"myImage" = "myImage-nl"
Now in your code you can use
let image = UIImage(named: NSLocalizedString(“myImage", comment:”language independent imageID"))
The iOS Localisation will now convert the language independent imageID “myImage” into a language specific imageID that is well known in the Asset Catalog.
I wrote a blog post about exactly this topic.

You can use Qordoba SDK to manage the localization of the images, layout and strings.

Related

NSLocalizedString in Spritekit SKLabelNode

I've been searching all over the internet and there doesn't seem to be a clear explanation on how to localize strings using SpriteKit. Only seeing tutorials for people using the interface builder, but all I really want is, imagine this:
I have an SKLabelNode called label. And I define the text like:
labl.text = NSLocalizedString("titleOfTheScreen",nil)
So basically what I think I have to do is add the new language in the Project settings. Then, I add a new Strings file called Localized, and add it to the new folder.
But what happens to my English language? There's no file for the original one
First you have to add a Strings File:
Then open the project settings and add a new language:
Mark your added strings file as target:
Find the newly added localising file. (English is automatically added)
From your screenshots I can see that you have "File.strings" file. You should have created "Localizable.strings" file.
Also, I can see that you have the (Base), (English) and (German) strings version. Why do you think English is not there?
In each of the files you should put strings like that:
"titleOfTheScreen" = "blah-blah";
Replace "blah-blah" with the proper translation in each of the strings files. It's important to note that the semi-colon at the end of the lines in strings files are mandatory, otherwise Xcode would issue some really funny error messages. This is easy to overlook if you're programming in Swift and trailing semi-colons are not mandatory.

How to name images in XCode Asset Catalog in the right manner?

I try to organise the images in XCode Asset Catalog, but I am kind of confused:
(1) I try to create new folders for different group of images. However, I can access an image without specifying the folder name, such as:
let image = UIImage(named: "btn_tabsetting")
Should I give each image a different name even I already have put them in different folders?
(2) As shown below, in test folder, I create an image btn_tabsetting which has the same name as the one in the other folder. Why does XCode allow me to do this? When I call
let image = UIImage(named: "btn_tabsetting")
it actually loads the the one in Tab Setting View folder instead of the one in test folder, why is it so? What is the rule here?
I think maybe I misunderstand some important concepts in using the Asset Catalog, please help me to correct them.
According to docs
For any target in an Xcode project, the fully qualified name of an asset must be unique across all the asset catalogs and across all asset types. For example, it is an error to have an image set folder in one asset catalog called Llama.imageset and an image set with the same name in the same catalog or in a different catalog that is part of the same target. Similarly, it is an error to have both an image set folder called Llama.imageset and an app icon folder called Llama.appiconset in the same catalog or in a different catalog that is part of the same target.
So asset names should be unique. What I recommend is organize assets in logical groups and than prefixing each asset inside group with group name.
For an example:
- Icons (group)
- icon_gear
- icon_heart
- icon_edit
- TabBar (group)
- icon_tabbar_profile
- icon_tabbar_settings
- Settings (group)
- icon_settings_age
- icon_settings_name
- Streachables (group)
- streachable_profile_background
- streachable_product_background
Xcode Automatically set different image name, if you have placed same name of images in Asset Catlog.
Suppose i have drop one image as xyz.png and then i again drop xyz.png in it, the xcode will rename it with xyz_1.png

Localization in Swift 2

I want to localize my application; I am using Swift 2. I followed this tutorial but I experience 2 issues.
1. Localized image disappears for both languages
I localized multiple images the same way for German and English. One set of images (the en and de versions of an image) disappear for both languages. I added them, they are in the project's folder and I can not spot any difference to other images I localized.
I tried
cleaning and running the project (no errors)
removed the images and added them again
removed and added the localization
…nothing helped. Any idea?
2. Error w/ NSLocalizedString & Localizable.strings
I created a Localizable.strings for localizing my app's strings. Here is how I make them localizable:
button.setTitle(NSLocalizedString("buttonTitle", comment: "MyButton"), forState: .Normal)
In the Localizable.strings for English I added:
"buttonTitle" = "MyButton"
…and for German:
"buttonTitle" = "MeinButton"
Now, Xcode does not show any errors in code but it says
Read failed:
The data couldn't be read because it isn't in the correct format.
Any ideas?
Thank you in advance :)
Edit
Issue #2 was resolved, I missed a ; there. Thanks to codingVoldemort!
I think you have missed the semi-colon on the Localizable.strings after each key-value pair.
In the Localizable.strings for English and German you have missed the semi-colon at the end of statement.
It should be like :
"buttonTitle" = "MyButton"; and
"buttonTitle" = "MeinButton";
You can refer Apple docs for this.
I'm a bit late, but if you're still searching for a solution for task #1:
You could use the solutions presented in that tutorial; there's a Internationalization of Images section.
"After selecting [your image] in Project navigator, click Localize button under File Inspector. This should provide you with the option to Localize the file in English and German."
That being said, I'm unsure whether this would work on Swift 2 (if you're still on there); but that should be possible with an up-to-date Xcode/IDE.

How to localize the images in Images.xcassets?

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.

Best way to utilize strings in iOS

I am new to coding in Objective-C, but am coming from the Java world of mobile development. In Android, we use a strings resource, and then point to those in the Java class. Is there an analogous process in iOS that we should be using, or do we "hard-code" the strings into the implementation files? I haven't yet found a good tutorial using the localizable string (and .strings) file.
You can use the NSLocalizedString macro from Foundation to retrieve a localized string. Read here:
http://www.icanlocalize.com/site/tutorials/iphone-applications-localization-guide/
Example: build a bilingual (English-French) app.
Whenever you encounter a string to be displayed to the user, use NSLocalizedString instead of the actual string constant. For example:
instead
self.title = #"Welcome";
use
self.title = NSLocalizedString(#"Welcome");
Create two directories inside your app bundle as follows:
MyApp.app
English.lproj
Localizable.strings
French.lproj
Localizable.strings
In the first Localizable.strings file you can write key-value pairs like this:
"Welcome" = "Welcome";
In the second one:
"Welcome" = "Bienvenus";
When you add these files and folders to your application, an recompile it using the NSLocalizedString macro, you'll be able to use your app in French also when the system language is French.
Sure, there's a way to do that.
Take a look at the Apple doc here - http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/LoadingResources/Strings/Strings.html
There's not much more to it than that really. Although I tend to just hard code a lot of strings but it's definitely worth taking the time to learn about string resources because it's the basis of internationalisation.

Resources