iOS swift如何更改英语以外的基本语言

时间:2017-04-20 04:39:02

标签: ios swift localization

我正在使用Localizable文件,目前我有两种语言英语 - 开发语言和其他是波斯语(fa-ir) 所以我的基本语言现在是英语,如果我的设备有西班牙语,如果用英语执行。

现在我想将我的基础语言英语改为波斯语。

2 个答案:

答案 0 :(得分:0)

您可以更改字符串路径,如下所示

目标C

    NSString *path = [[NSBundle mainBundle ] pathForResource:@"en"  ofType:@"lproj"]; // in place of "en" you can change put your locale code.
    NSBundle *bundle = [NSBundle bundleWithPath:path] ;
    NSString *string = [bundle localizedStringForKey:key value:@"" table:nil];

Swift 3

        let bundle = Bundle.main
        bundle.path(forResource: "en" as String, ofType: "lproj")// in place of "en" you can change put your locale code.
        let string = bundle.localizedString(forKey: key, value: "", table: nil)

答案 1 :(得分:0)

它将在您的任何代码执行之前调用

转到AppDelegate并移除@UIApplicationMain

创建新的Swift文件main.swift

添加以下代码

import Foundation
import UIKit

let langCultureCode: String = LanguageCode.dutch
let defaults = UserDefaults.standard
defaults.set([langCultureCode], forKey: appleLanguages)
defaults.synchronize()

UIApplicationMain(
CommandLine.argc,
UnsafeMutableRawPointer(CommandLine.unsafeArgv)
    .bindMemory(
        to: UnsafeMutablePointer<Int8>.self,
        capacity: Int(CommandLine.argc)),
nil,
NSStringFromClass(AppDelegate.self)
)

哪里

    let appleLanguages      =   "AppleLanguages"

static let dutch            =   "nl-US"