Reading Localized String - ios

I have an app that I'd like to be able to get the localized string for a certain key, regardless of what my current localization on the iPhone is.
I have the following in an en.lproj localized strings file
"Black" = "Black";
In the es.lproj localized strings file I have
"Black" = "Negro";
So what I would like to do is get the Spanish string when my phone is in the US and set up accordingly
I'm using the following code:
let bpath:String = NSBundle.mainBundle().pathForResource("es", ofType: "lproj")! as String
let bundle = NSBundle(path: bpath as String)
let thisWord="Black"
let ourWord=NSLocalizedString(thisWord, bundle: bundle!, comment: "")
I'm expecting to get "Negro" in the value for ourWord, but I always get "Black"
Am I missing something obvious?

Localization settings apply based on the settings you set in your phone not the location. If your phone is setup to display an Application Language of Spanish then you will see the appropriate string. To configure Application Language settings so you can test this you need to edit your Scheme.
In Xcode 7.1+ Goto Product > Scheme > Edit Scheme
Then change the Application Language and/or Region to simulate what a user would see who has an iPhone configured for a region or language other than English in the US.
(Switch it to Spanish and then debug the app on the device again to see the updated localized strings).

Actually my code works fine. I had made the stupid mistake of not setting the "Localizable.strings" file as localized.
Feeling pretty dumb right now

Related

Is there a way to set English as a default language

I localized my app for 2 languages (EN, FR). When iPhone language set to English, app shows up in English. When iPhone language set to French, app shows up in French. But when iphone language set to another language lets say Spanish app shows up in French not in English.
I set "Localization native development region" to "en" or "United States" in my Info.plist file but didn't help.
Main.storyboard (Base) also in English.
Solution:
One way to do it is to get the English string from en.lproj if the preferred language does not match en or fr.
This is how you get the preferred language:
let preferredLanguage = Locale.preferredLanguages[0]
This is how you get the English string using your localized key:
if let enPath = Bundle.main.path(forResource: "en", ofType: "lproj") {
let enBundle = Bundle(path: enPath)
enBundle?.localizedString(forKey: "your_key_here", value: nil, table: nil)
}
Explanation of your problem:
In iOS native Settings App -> General -> Language & Region, there is a Preferred Language Order list.
Apps and websites will use the first language in this list that they
support.
For example:
Set "French" as your iPhone Language.
Open the App and everything is localized in French because your app does support French.
Set "Spanish" as your iPhone Language.
Open the App and everything is localized in French because your app does not support Spanish and it'll fall through your next preferred language which is French in this case.
Just set developer language to English
YOURPROJECT.xcodeproj -> show package content -> Editing the .pbxproj file directly using a text editor - look out for the developmentRegion property
And change the Localizations native development region property in the Info.plist accordingly en Or bellow image
once your result like this then you setup successfully :
]1

XIB localization failed in iPad

My app support Traditional Chinese and English
Now I set my device language as Japanese
Well... In iPhone everything is fine, but iPad localization failed
If I set text used code, it show English when app running
Since app doesn't support Japanese it use English as default language
theLabel.text = NSLocalizedString("theKey", tableName: nil, bundle: Bundle.main, value: "", comment: "")
But if I set text used xib localize
xib base file↓
// English String
"fKW-21-ZWh.placeholder" = "Please fill in your skype ID";
"hyL-A7-fx9.placeholder" = "Please fill in your paypal ...";
// Chinese String
"fKW-21-ZWh.placeholder" = "請填入你的skype";
"hyL-A7-fx9.placeholder" = "請填入你的paypal";
it shows Chinese text when app running
I hope it can use English as default language
do I missing something?
What I did
TARGET -> Info -> Localization native development region = United States
TARGET -> Info -> Localizations = [English, Chinese(traditional)]
PROJECT -> Localizations = [Chinese, English - Development Laanguage]
Delete DerivedData, APP
Clean Project
restart computer
Edit
Edit
I found a Apple tech QA, link
the language that app used is depend on user language preferences
Just select your storyboard/xib, go to File Inspector. In the Localization section, you can tick the language you want.

Localized Strings are always in English in UITests (Snapshot)

I'm pretty new to Fastlane and love the idea of Snapshot, but I got a little problem.
When I'm trying to create a new set of screenshots I'm facing the problem that the UITests don't use the correct localized Strings to fetch the UI elements.
My current state is based on this Stack entries: XCode 7 UITests with localized UI
The localization method:
func localizedString(_ key: String) -> String {
let uiTestBundle = Bundle(for: MyUITests.self)
return NSLocalizedString(key, bundle: uiTestBundle, comment: "")
}
Trying to perform a tap that way:
app.navigationBars[localizedString("key_1")].buttons[localizedString("key_2")].tap()
The error I get is the following:
No matches found for "Rolling stone" NavigationBar
Rolling stone is the Base/English Localization of the key, but there should be a German one. So for any reason the UITest always picks the English Localization.
Does anybody have an idea how to troubleshoot this? I checked the SnapshotHelper and it passes a "de-DE" as the language, so that's not the point.
But I just can't find the Bug :(
Localization
This link here should be sufficient enough to solve your problem. You simple pick the correct language option during the test tab under edit scheme option.

String localization for framework

I'm building a framework that includes UI components. I'm retrieving localized strings with:
var bundle = Bundle(identifier: "com.my.identifier")
bundle.localizedString(forKey: "myKey", value: nil, table: nil)
The problem I'm seeing is whatever parent application integrates with my framework, must also have localization enabled for my same localizations (e.g. if I have localizations for es-ES, the parent application must also turn on localization for es-ES in project settings:
Otherwise my localized strings always default to english.
I have found a workaround by manually grabbing a language bundle based on the device's preferred language:
var bundle = Bundle(identifier: "com.my.identifier")
guard let deviceLanguage = Locale.preferredLanguages.first else {
return notFound
}
guard let languagePath = bundle.path(forResource: deviceLanguage, ofType: "lproj") else {
return notFound
}
guard let languageBundle = Bundle(path: languagePath) else {
return notFound
}
return NSLocalizedString(self, tableName: "Localizable", bundle: languageBundle, value:"KEY_NOT_FOUND", comment: "")
But the issue with this workaround solution is that I lose the built-in iOS language fallback (e.g. if the device preferred language is es-MX it will auto-choose the es-ES string over the en string I have).
For my framework, is there a way to avoid forcing parent applications to turn on localization for all my localized strings, yet still keep iOS language fallback logic?
Why do you want your framework to use a localization not supported by the app? This is a terrible user experience.
Imagine app that uses your framework only supports English and Spanish. Now imagine that a user of the app have their device setup to use German first, and English second.
Since the app doesn't support German, the app shows English. The entire app should show English. If your app framework manages to show German in this case, the app's user experience is now very confusing because much of the app is in English but the parts supported by your framework appear in German.
tl;dr - Don't do anything. Let the default behavior work as-is. It makes for a much better user experience.

How to get the user "Name" using Swift?

I'm trying to create an app that when the user open the first view it will provide with a nice greeting.
For example depending on the time of the day it would say "Good Morning/Afternoon/Night - User Name".
How do I get the name that the User has on his iPhone? I need the name that is on Settings > General > About _ NAME?
UIDevice.current.name gives back device name, so you should parse the string trying to distinguish between device type (iPhone, iPad and so on...) and the name.
But is bit tricky: suppose You have "My iPhone" , "Mary's iPhone" or "IPhone of John" or similar.
Soem people try to use regular expressions, but result is not so good, specially for non-english names.
see at:
http://stackoverflow.com/questions/8261961/better-way-to-get-the-users-name-from-device
Swift 2
UIDevice.currentDevice().name
Swift 3
UIDevice.current.name
let benutzerGeraeteName = UIDevice.current.name
print("Hallo, \(benutzerGeraeteName)")

Resources