My app changes the language at runtime and advises the user to restart the app. I do it like this:
我的应用程序在运行时更改语言,并建议用户重新启动应用程序。我是这样做的:
typealias LanguageLocaleType = (language: String?, locale: String?, title: String)
let elements: [LanguageLocaleType] = [
(nil, nil, "System Default"),
("en", "en_US", "English"),
("ar", "ar_SA", "Arabic"),
]
//...func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)...
let element = elements[indexPath.row]
guard let language = element.language else {
// How to really reset app back to OS language/locale???
UserDefaults.standard.removeObject(forKey: "AppleLanguages")
UserDefaults.standard.removeObject(forKey: "AppleLocale")
return
}
UserDefaults.standard.set([language], forKey: "AppleLanguages")
if let locale = element.locale, !locale.isEmpty,
Locale.current.identifier != locale,
!Locale.current.identifier.hasPrefix("\(language)_") {
UserDefaults.standard.set(locale, forKey: "AppleLocale")
}
I want to offer to set languages in a list with the right one selected, one of which being offered it to set language back to System Default
. However, there's no way to find the OS-level language and locale that I could find. Since after I mess around with setting the UserDefaults
, Bundle.main.preferredLocalizations
is not reliable and do not match the system default (event when I remove the key from the User Default
).
我想提供在列表中设置语言并选择正确的语言,其中一个用于将语言设置回系统默认值。但是,没有办法找到我能找到的操作系统级语言和语言环境。因为在我设置UserDefaults后,Bundle.main.preferredLocalizations不可靠并且与系统默认值不匹配(当我从User Default中删除密钥时的事件)。
Is there a way to get the OS-level language and locale instead of the app-level?
有没有办法获得操作系统级别的语言和区域设置而不是应用程序级别?
4 个解决方案
#1
4
You should be able to get currently selected OS language like this.
您应该能够获得当前选择的操作系统语言。
let language = NSLocale.preferredLanguages[0]
if this is not what you are looking for, refer to this answer https://*.com/a/30750120/809821
如果这不是您想要的,请参阅此答案https://*.com/a/30750120/809821
#2
1
I will recommend you to use this library from Cocoa pods.
我建议你使用Cocoa pods中的这个库。
In your Podfile add this line:
在你的Podfile中添加以下行:
pod 'Localize-Swift', '~> 1.7'
An then install your pods using this command:
然后使用以下命令安装您的pod:
pod install
Then if you don't have Localizable.strings, for example Localizable.strings (English) file add this lines.
然后,如果您没有Localizable.strings,例如Localizable.strings(英文)文件添加此行。
"LD" = "Loading ...";
"CAMP" = "Campus";
"MAP" = "MAP";
The first word is like your TAG and the next word is your value .
第一个词就像你的TAG,下一个词就是你的价值。
Then in your ViewController you can use your string like this.
然后在您的ViewController中,您可以像这样使用您的字符串。
Example of use:
使用示例:
let progressHUD = ProgressHUD(text: "LD".localized())
let progressHUD = ProgressHUD(text: "CAMP".localized())
let progressHUD = ProgressHUD(text: "MAP".localized())
And that is all.
这就是全部。
I hope this help.
我希望这有帮助。
#3
1
I found an answer which seems to do something similar, by playing with the Bundle language files of your app. As in setting the proper one.
通过使用应用程序的Bundle语言文件,我找到了一个类似的答案。正如设置正确的一样。
While you state that playing with Bundle properties seems to fiddle with your use case, I'd look into saving the setted language with Core Data, inside an app file, or inside and XML (.plist). You could set up a checking method in didFinishWithOptions which checks the presence of this variable then runs the language prompt if absent or leaves as is if present (adding a check to make sure the right language is set would be wise). Since this does not alter the Settings-set language, you'd always have a quick reference point outside of your app through NSLocale to switch back to it with ease if the user so chooses.
虽然您声明使用Bundle属性似乎与您的用例有关,但我会考虑使用Core Data,app文件或内部以及XML(.plist)保存设置的语言。你可以在didFinishWithOptions中设置一个检查方法,它检查这个变量是否存在,然后如果不存在则运行语言提示,或者如果存在则离开(添加一个检查以确保设置正确的语言是明智的)。由于这不会改变设置设置语言,因此如果用户选择的话,您通过NSLocale总是在应用程序外部有一个快速参考点,以便轻松切换回它。
Here's the post
这是帖子
Disclaimer 1 : i haven't tested this, but it seems like a plausible, and Apple-Tolerated thing to do. Disclaimer 2 : as the OP in the link states, and as some have mentioned here, changing app language is not recommended by Apple. Notably due to NSLocale keys, and how frequently Swift is updated which could lead to your code being unuseable or requiring massive overhaul.. As i mentioned in a comment above, i'd reconsider this use-case sadly :(
免责声明1:我没有对此进行过测试,但这似乎是一个看似合理且容易受到Apple限制的事情。免责声明2:正如链接中的OP所述,并且正如一些人在此处提到的那样,Apple不建议更改应用程序语言。特别是由于NSLocale密钥,以及Swift更新的频率,这可能导致您的代码无法使用或需要进行大规模检修。正如我在上面的评论中提到的,我会重新考虑这个用例:#
#4
1
In reference to your question above you can either as stated above create your own bundle or use a prebuilt class like the following:https://github.com/mumensh/iOS-Localization
关于上面的问题,您可以按照上面的说明创建自己的包或使用如下所示的预构建类:https://github.com/mumensh/iOS-Localization
#1
4
You should be able to get currently selected OS language like this.
您应该能够获得当前选择的操作系统语言。
let language = NSLocale.preferredLanguages[0]
if this is not what you are looking for, refer to this answer https://*.com/a/30750120/809821
如果这不是您想要的,请参阅此答案https://*.com/a/30750120/809821
#2
1
I will recommend you to use this library from Cocoa pods.
我建议你使用Cocoa pods中的这个库。
In your Podfile add this line:
在你的Podfile中添加以下行:
pod 'Localize-Swift', '~> 1.7'
An then install your pods using this command:
然后使用以下命令安装您的pod:
pod install
Then if you don't have Localizable.strings, for example Localizable.strings (English) file add this lines.
然后,如果您没有Localizable.strings,例如Localizable.strings(英文)文件添加此行。
"LD" = "Loading ...";
"CAMP" = "Campus";
"MAP" = "MAP";
The first word is like your TAG and the next word is your value .
第一个词就像你的TAG,下一个词就是你的价值。
Then in your ViewController you can use your string like this.
然后在您的ViewController中,您可以像这样使用您的字符串。
Example of use:
使用示例:
let progressHUD = ProgressHUD(text: "LD".localized())
let progressHUD = ProgressHUD(text: "CAMP".localized())
let progressHUD = ProgressHUD(text: "MAP".localized())
And that is all.
这就是全部。
I hope this help.
我希望这有帮助。
#3
1
I found an answer which seems to do something similar, by playing with the Bundle language files of your app. As in setting the proper one.
通过使用应用程序的Bundle语言文件,我找到了一个类似的答案。正如设置正确的一样。
While you state that playing with Bundle properties seems to fiddle with your use case, I'd look into saving the setted language with Core Data, inside an app file, or inside and XML (.plist). You could set up a checking method in didFinishWithOptions which checks the presence of this variable then runs the language prompt if absent or leaves as is if present (adding a check to make sure the right language is set would be wise). Since this does not alter the Settings-set language, you'd always have a quick reference point outside of your app through NSLocale to switch back to it with ease if the user so chooses.
虽然您声明使用Bundle属性似乎与您的用例有关,但我会考虑使用Core Data,app文件或内部以及XML(.plist)保存设置的语言。你可以在didFinishWithOptions中设置一个检查方法,它检查这个变量是否存在,然后如果不存在则运行语言提示,或者如果存在则离开(添加一个检查以确保设置正确的语言是明智的)。由于这不会改变设置设置语言,因此如果用户选择的话,您通过NSLocale总是在应用程序外部有一个快速参考点,以便轻松切换回它。
Here's the post
这是帖子
Disclaimer 1 : i haven't tested this, but it seems like a plausible, and Apple-Tolerated thing to do. Disclaimer 2 : as the OP in the link states, and as some have mentioned here, changing app language is not recommended by Apple. Notably due to NSLocale keys, and how frequently Swift is updated which could lead to your code being unuseable or requiring massive overhaul.. As i mentioned in a comment above, i'd reconsider this use-case sadly :(
免责声明1:我没有对此进行过测试,但这似乎是一个看似合理且容易受到Apple限制的事情。免责声明2:正如链接中的OP所述,并且正如一些人在此处提到的那样,Apple不建议更改应用程序语言。特别是由于NSLocale密钥,以及Swift更新的频率,这可能导致您的代码无法使用或需要进行大规模检修。正如我在上面的评论中提到的,我会重新考虑这个用例:#
#4
1
In reference to your question above you can either as stated above create your own bundle or use a prebuilt class like the following:https://github.com/mumensh/iOS-Localization
关于上面的问题,您可以按照上面的说明创建自己的包或使用如下所示的预构建类:https://github.com/mumensh/iOS-Localization