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.
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.
This question already has an answer here:
Get list of folder and files within Assets.xcassets
(1 answer)
Closed 5 years ago.
I am new to xCode. I want to create an array of UIImage from the folder in Assets Catalogue - see screenshot ("Brands" folder).
To load it in a UITableView like this:
private func loadLocalBrands() {
// Load images names from assets and add them to array
let names : [String] = ["Avon","Dove","Faberlic","Grand","GreenLight","Loreal",
"Loreal", "MaxFactor", "Nivea", "Olay", "Oriflame",
"Rexona","Schwarzkopf","Spaquatoria","Wella"]
for name in names{
brands.append(Brand.init(name: name, photo:
UIImage(named: name), rating: Int(arc4random_uniform(5) + 1))!)
}
}
and it works properly - see screenshot
But if I will add sooner more images, I will need to expand "names" array too.
So I want to get list of files that located in "Brands" folder within the Assets catalogue.
You are over-engineering this, its best to maintain a list of your assets that you want to display to the user. You will have to add assets to the project and repackage which is a good time to add new items to the list.
Also this is why things like this are manged from server in case you didn't think of it.
Another issue is that the asset catalogue is a product of Xcode whereas its not available when the IPA file of the app is created in the same format (in my understanding). So this approach will fail even if you were to work around and read the file list somehow.
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.
I need OpenSans in my app and so I imported the whole OpenSans bunch which includes Bold, ExtraBold, Italic, Regular, Light and more. I added them to Fontbook (to check the exact name I need to address them by when using them) on my Mac, to my project in the file structure and added them in Build phases to my project as well.
Now the weird thing; when I use them with UIFont, ONLY OpenSans-Bold works. The rest doesn't work. If I use OpenSans-Bold (exactly the identifier given by Fontbook), everything works fine. If I, however, change it to OpenSans-Regular or OpenSans-Light or something else in that family, I get the systemfont with UIFont and I get nothing when drawing with Quartz.
All fonts are in all lists and I checked all the identifiers from Fontbook (which was why I couldn't get OpenSans-Bold working), but now i'm at a loss. Any idea what I could be doing wrong?
OpenSans Regular is just OpenSans, semi-bold is OpenSans-Semibold, etc.
In Font Book.app select the font and choose Command + I which brings up the font info. In the "PostScript name" in the top you can see what the font is called and how you can call it in your code.
Also, just make sure the font has actually been added in your bundle and your plist :)
You Can check the fonts provided by the ttf file With the following lines of code..
this method is provided by the UIFont class.
(NSArray *)fontNamesForFamilyName:(NSString *)familyName
You need to add the files in the right locations, then in the plist file, then enumerate them all and print them to get the PDF names so you can access them. Most likely you're using the wrong names.
Fonts have three kinds of names:
The file name
The font name in Font Book
The PDF name - this is the one you use to access the font in code
See this answer:
Adding custom fonts to iOS app finding their real names
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.