I want to localise application for two languages one is LTR & Another is RTL without restarting application.
class func changeLanguageForceFully(currLan : String){
let langCultureCode: String = currLan
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject([langCultureCode], forKey: "AppleLanguages")
defaults.synchronize()
}
Above function is working fine but it takes effect after restarting the application.
Are you supporting Arabic internationalisation in your project info?
Also are you setting constraints using leading/trailing?
Related
I have an iOS app with a default language (English).
It also has a French localization and works as expected in both languages.
Here is the problem I am having:
When setting the device to a language other than English(default) or a language for which localization is provided (for example setting the device to Japanese); the app does not fall as expected in the default language. But it keeps the last language used for the app.
Why is that? And how can I fix it?
I have found a few post with a similar issue, but the solutions proposed did not work for me. For example this one, where the problem is similar to mine.
I am using Xcode version 11.1 and iOS version 12.4.2.
iOS will have a list of preferred language order based on the previously selected languages.
So, if you changed from English then to an unmapped language (e.g ko), the application will have as AppleLanguages the array:
[ko-(ZoneCode), en-(ZoneCode)].
You can avoid this procedure using this code below:
let defaultCultureCode: String = "en"
let defaults = UserDefaults.standard
let currentAppleLanguages = defaults.stringArray(forKey: "AppleLanguages")
if let currentLanguages = currentAppleLanguages {
if(!currentLanguages.isEmpty && !(currentLanguages.first?.contains(["en", "fr"]))!) {
defaults.removeObject(forKey: "AppleLanguages")
defaults.set([defaultCultureCode], forKey: "AppleLanguages")
defaults.synchronize()
}
}
I used this string extension to check if the current languages contain the available language:
extension String {
public func contains(_ elements: [String]) -> Bool {
var haveElementOnString = false
if elements.count == 0 { return false }
else { elements.forEach{ element in haveElementOnString = haveElementOnString || self.contains(element)} }
return haveElementOnString
}
}
The next step it's up to you to define the best strategy.
In my application context, it will always be defined as English by default because it is the only .strings file available. All the other supported strings will be downloaded and then a message will be displayed to the user warning that new languages are available and will be applied next time.
There's a lot of information on StackOverflow about bundle.localizedStringForKey or NSLocalizedString that could help you to found the best solution for what you need.
Also, this is only the validation and update for the current locale and the default language. this doesn't include the Region section (e.g. ko-KR (KR)).
I found an interesting text on this topic that could be useful
How not to do localization.
I hope it solves your problem!
I want my app language to change on button click..
My code is:
#IBAction func convertlang(_ sender: Any) {
if L102Language.currentAppleLanguage() == "en" {
L102Language.setAppleLAnguageTo(lang: "ar")
UIView.appearance().semanticContentAttribute = .forceRightToLeft
} else {
L102Language.setAppleLAnguageTo(lang: "en")
UIView.appearance().semanticContentAttribute = .forceLeftToRight
}
}
let APPLE_LANGUAGE_KEY = "AppleLanguages"
class L102Language {
class func currentAppleLanguage() -> String{
let userdef = UserDefaults.standard
let langArray = userdef.object(forKey: APPLE_LANGUAGE_KEY) as! NSArray
let current = langArray.firstObject as! String
return current
}
class func setAppleLAnguageTo(lang: String) {
let userdef = UserDefaults.standard
userdef.set([lang,currentAppleLanguage()], forKey: APPLE_LANGUAGE_KEY)
userdef.synchronize()
}}
and this works fine and convert the language when i click on the button..
the problem is i need to restart the app to see the language change..
i searched for this but most of them were for objective-c and tried some for swift but didn't work..
how to do this?
You can't change the language at runtime with normal button click. If you really need that, you need to use a custom localization system instead.
To change the app language during runtime, I have done this manually by creating two different storyboards. I saved the language preference in NSUserDefaults but NOT with the key AppleLanguages and then called AppDelegate's didFinishLaunchingWithOptions method to select storyboard.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainEnglish" bundle:nil];
UINavigationController *navigationcontroller=[[UINavigationController alloc]init];
RegistorViewController *registor=[storyboard instantiateViewControllerWithIdentifier:#"Registor"];
[self.window setRootViewController:navigationcontroller];
[self.window makeKeyAndVisible];
[navigationvontroller pushViewController:registor animated:NO];
Though this is not what apple wants a developer to do in runtime.
Someone even asked an Apple engineer and this is the response:
In general, you should not change the iOS system language (via use of the AppleLanguages pref key) from within your application. This goes against the basic iOS user model for switching languages in the Settings app, and also uses a preference key that is not documented, meaning that at some point in the future, the key name could change, which would break your application.
If you want to switch languages in your application, you can do so via manually loading resource files in your bundle. You can use NSBundle:pathForResource:ofType:inDirectory:forLocalization: for this purpose, but keep in mind that your application would be responsible for all loading of localized data.
I had faced the same problem in my project recently. You are using the correct code to change app language but it change only string file data not the storyboard. For complete localisation you have to restart the app again, because there is no any another way to do this. You can show a popup to user "Please restart your app to change language completely". Because we can kill the app programatically, but after doing this there is no way to restart the app again.
This might not be the most iOS-typical way of doing things, but I tend to treat static content like any other data in the app, especially when I'm using VIPER (although other patterns would work) and a unidirectional data flow.
With that in mind, for this I would have a service accessing a store of static content keyed of the user's language preference. When that changes I would trigger a refresh of the content.
This has worked well for projects when I also have server data as well as local data that needs to be localised.
To be fair, I've never used the standard tools much. And I suspect this may not be ideal for right-to-left language types.
Did anyone face an issue with Shared User Defaults via App Groups on iOS 11? I am saving a value in one of the extensions but I am not able to fetch same value via another extension.
In the first extension :
let defaults = UserDefaults.init(suiteName: Constants.commonSuite)
defaults.set("Sample", forKey: "SampleKey")
defaults.synchronize()
In the second extension :
let defaults = UserDefaults.init(suiteName: Constants.commonSuite)
let sampleString = defaults.object(forKey: "SampleKey")
print(sampleString)
Interestingly this thing works fine on iOS 10. Breaks on iOS 11
Had the same issue.
Suitename needs to be the same value as your app group name, not some arbitrary value.
Hope that helps!
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.
In iOS, in the native Emoji keyboard, you can see your most recently used Emojis. I would like to know if it is possible to get the data on those Emojis (which is app-independent) from inside my app.
My goal is to display the most used emoji, given a user, in my app.
If you just want an Emoji selector you could use/modify libraries like SYEmojiPopover or AGEmojiKeyboard which allows you to have full control on the output without messing with the iOS internals (albeit the "recents" list will be app-specific).
On iOS 9 the preferences are stored in the com.apple.EmojiPreferences suite, which you could extract the list of most recently used emoji by:
// swift 3:
let prefs = UserDefaults(suiteName: "com.apple.EmojiPreferences")!
let defaults = prefs.dictionary(forKey: "EmojiDefaultsKey")!["EmojiRecentsDefaultsKey"]! as! [String: Any]
let recents = defaults["RecentsKey"]! as! [String]
print(recents)
// swift 2:
let prefs = NSUserDefaults(suiteName: "com.apple.EmojiPreferences")!
let recents = prefs.dictionaryForKey("EmojiDefaultsKey")!["EmojiRecentsDefaultsKey"]!["RecentsKey"]! as! [String]
print(recents)
// prints e.g. ["π", "π³", "πΏ", "β", "πΆ", "πΏπ¦", "βͺ", "π", "π
"]
Note that this is UNDOCUMENTED, and I have only checked it works on iOS 9 when deployed via Xcode. There is no guarantee that the App Store reviewers will allow this usage, nor there is any guarantee that it will work in the past or future versions.