iOS:如何在不重启应用的情况下以编程方式更改应用语言?

sg3maiej  于 2023-06-25  发布在  iOS
关注(0)|答案(5)|浏览(119)

当我在设备语言上独立更改应用程序使用的语言时,只有关闭应用程序并重新启动它才能生效。如何不需要应用程序重新启动加载所有笔尖文件和.字符串文件再次取决于所选的语言?
我使用这个在运行时更改语言:

NSArray* languages = [NSArray arrayWithObjects:@"ar", @"en", nil]; 
[[NSUserDefaults standardUserDefaults] setObject:languages forKey:@"AppleLanguages"];
eoigrqb6

eoigrqb61#

这对我很有效:Swift 4:
创建一个名为BundleExtension.swift的文件,并将以下代码添加到其中-

var bundleKey: UInt8 = 0

class AnyLanguageBundle: Bundle {

override func localizedString(forKey key: String,
                              value: String?,
                              table tableName: String?) -> String {
    
    guard let path = objc_getAssociatedObject(self, &bundleKey) as? String,
        let bundle = Bundle(path: path) else {
            
            return super.localizedString(forKey: key, value: value, table: tableName)
    }
    
    return bundle.localizedString(forKey: key, value: value, table: tableName)
  }
}

extension Bundle {
 
class func setLanguage(_ language: String) {
    
    defer {
        
        object_setClass(Bundle.main, AnyLanguageBundle.self)
    }
    
    objc_setAssociatedObject(Bundle.main, &bundleKey,    Bundle.main.path(forResource: language, ofType: "lproj"), .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  }
}

现在,每当你需要更改语言时,调用这个方法:

func languageButtonAction() {
    // This is done so that network calls now have the Accept-Language as "hi" (Using Alamofire) Check if you can remove these
    UserDefaults.standard.set(["hi"], forKey: "AppleLanguages")
    UserDefaults.standard.synchronize()
    
    // Update the language by swapping bundle
    Bundle.setLanguage("hi")
    
    // Done to reinstantiate the storyboards instantly
    let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
    UIApplication.shared.keyWindow?.rootViewController = storyboard.instantiateInitialViewController()
}
ekqde3dh

ekqde3dh2#

我对带有标签导航的Kiosk模式iPad应用程序有类似的要求。该应用程序不仅需要支持动态语言更改,而且必须这样做,因为该应用程序在加载新版本时每周才重新启动一次(平均),因为大多数选项卡已经从笔尖加载。
我尝试了几个建议来利用现有的Apple本地化机制,它们都有严重的缺点,包括XCode 4.2对本地化nibs的支持不稳定-我的IBoutlet连接变量似乎在IB中设置正确,但在运行时它们通常为null!?
我最终实现了一个模仿Apple NSLocalizedString类的类,但它可以处理运行时更改,并且每当用户进行语言更改时,我的类都会发布通知。需要本地化字符串(和图像)来更改的屏幕声明了一个handleLocaleChange方法,该方法在viewDidLoad时调用,并且每当LocaleChangedNotification被发布时调用。
我所有的按钮和图形都是独立于语言的,尽管标题文本和标签文本通常会随着区域设置的变化而更新。如果我必须改变图像,我想我可以在每个屏幕的handleLocaleChange方法中这样做。
这是密码。它包含了一些对nib/bundle路径的支持,而我在最终的项目中实际上并没有使用这些路径。
MyLanguage. h////MyLanguage. h// //

#import <Foundation/Foundation.h>

#define DEFAULT_DICTIONARY_FOR_STRINGS                      @""
#define ACCESSING_ALTERNATE_DICTIONARY_SETS_DEFAULT         1

#define LANGUAGE_ENGLISH_INT  0
#define LANGUAGE_SPANISH_INT  1
#define LANGUAGE_ENGLISH_SHORT_ID  @"en"
#define LANGUAGE_SPANISH_SHORT_ID  @"es"

#define LANGUAGE_CHANGED_NOTIFICATION   @"LANGUAGE_CHANGED"

@interface MyLanguage : NSObject
{
    NSString        *currentLanguage;    
    NSDictionary    *currentDictionary;
    NSBundle        *currentLanguageBundle;
}

+(void) setLanguage:(NSString *)languageName;

+(NSString *)stringFor:(NSString *)srcString forLanguage:(NSString *)languageName;
+(NSString *)stringFor:(NSString *)srcString;

+ (MyLanguage *)singleton;

@property (nonatomic, retain) NSBundle        *currentLanguageBundle;
@property (nonatomic, retain) NSString        *currentLanguage;    
@property (nonatomic, retain) NSDictionary    *currentDictionary;

@end

MyLanguage.m:/// MyLanguage.m

#import "MyLanguage.h"
#import "Valet.h"

#define GUI_STRING_FILE_POSTFIX   @"GUIStrings.plist"

@implementation MyLanguage

@synthesize currentLanguage;   
@synthesize currentDictionary;
@synthesize currentLanguageBundle;

+(NSDictionary *)getDictionaryNamed:(NSString *)languageName
{
    NSDictionary *results = nil;

    // for now, we store dictionaries in a PLIST with the same name.
    NSString *dictionaryPlistFile = [languageName stringByAppendingString:GUI_STRING_FILE_POSTFIX];

    NSString *plistBundlePath = [Valet getBundlePathForFileName:dictionaryPlistFile];

    if ( [[NSFileManager defaultManager] fileExistsAtPath:plistBundlePath] )
    {
        // read it into a dictionary
        NSDictionary *newDict = [NSDictionary dictionaryWithContentsOfFile:plistBundlePath]; 
        results = [newDict valueForKey:@"languageDictionary"];

    }// end if

    return results;
}

+(NSString *)stringFor:(NSString *)srcString forDictionary:(NSString *)languageName;
{
    MyLanguage *gsObject = [MyLanguage singleton];

    // if default dictionary matches the requested one, use it.
    if ([gsObject.currentLanguage isEqualToString:languageName])
    {
        // use default
        return [MyLanguage stringFor:srcString];
    }// end if
    else
    {
        // get the desired dictionary
        NSDictionary *newDict = [MyLanguage getDictionaryNamed:languageName];

        // default is not desired!
        if (ACCESSING_ALTERNATE_DICTIONARY_SETS_DEFAULT)
        {
            gsObject.currentDictionary = newDict;
            gsObject.currentLanguage = languageName;
            return [MyLanguage stringFor:srcString];
        }// end if
        else
        {
            // use current dictionary for translation.
            NSString *results = [gsObject.currentDictionary valueForKey:srcString];

            if (results == nil)
            {
                return srcString;
            }// end if

            return results;
        }
    }

}

+(void) setLanguage:(NSString *)languageName;
{
    MyLanguage *gsObject = [MyLanguage singleton];

    // for now, we store dictionaries in a PLIST with the same name.
    // get the desired dictionary
    NSDictionary *newDict = [MyLanguage getDictionaryNamed:languageName];

    gsObject.currentDictionary = newDict;
    gsObject.currentLanguage = languageName;   

    // now set up the bundle for nibs
    NSString *shortLanguageIdentifier = @"en";
    if ([languageName contains:@"spanish"] || [languageName contains:@"espanol"] || [languageName isEqualToString:LANGUAGE_SPANISH_SHORT_ID])
    {
        shortLanguageIdentifier = LANGUAGE_SPANISH_SHORT_ID;
    }// end if
    else
        shortLanguageIdentifier = LANGUAGE_ENGLISH_SHORT_ID;

//    NSArray *languages = [NSArray arrayWithObject:shortLanguageIdentifier];
//    [[NSUserDefaults standardUserDefaults] setObject:languages forKey:@"AppleLanguages"]; 
//    
    NSString *path= [[NSBundle mainBundle] pathForResource:shortLanguageIdentifier ofType:@"lproj"];
    NSBundle *languageBundle = [NSBundle bundleWithPath:path];
    gsObject.currentLanguageBundle = languageBundle;

    [[NSNotificationCenter defaultCenter] postNotificationName:LANGUAGE_CHANGED_NOTIFICATION object:nil];

}

+(NSString *)stringFor:(NSString *)srcString;
{
    MyLanguage *gsObject = [MyLanguage singleton];
    // default is to do nothing.
    if (gsObject.currentDictionary == nil || gsObject.currentLanguage == nil || [gsObject.currentLanguage isEqualToString:DEFAULT_DICTIONARY_FOR_STRINGS] )
    {
        return srcString;
    }// end if

    // use current dictionary for translation.
    NSString *results = [gsObject.currentDictionary valueForKey:srcString];

    if (results == nil)
    {
        return srcString;
    }// end if

    return results;
}


#pragma mark -
#pragma mark Singleton methods

static MyLanguage *mySharedSingleton = nil;

-(void) lateInit;
{

}

// PUT THIS METHOD DECLARATION INTO THE HEADER
+ (MyLanguage *)singleton;
{
    if (mySharedSingleton == nil) {
        mySharedSingleton = [[super allocWithZone:NULL] init];
        [mySharedSingleton lateInit];
    }
    return mySharedSingleton;
}

+ (id)allocWithZone:(NSZone *)zone
{    return [[self singleton] retain]; }

- (id)copyWithZone:(NSZone *)zone
{    return self; }

- (id)retain
{    return self; }

- (NSUInteger)retainCount //denotes an object that cannot be released
{    return NSUIntegerMax;  }

- (oneway void)release    //do nothing
{   }

- (id)autorelease
{     return self; }

@end
wwwo4jvm

wwwo4jvm3#

不要依赖在笔尖文件中设置的字符串。使用您的笔尖只为布局和设置的意见。任何显示给用户的字符串(按钮文本等)都需要在Localizable.strings文件中,当你加载笔尖时,你需要相应地在相应的视图/控件上设置文本。
要获取当前语言的包,请执行以下操作:

NSString *path = [[NSBundle mainBundle] pathForResource:currentLanguage ofType:@"lproj"];
if (path) {
    NSBundle *localeBundle = [NSBundle bundleWithPath:path];
}

并使用bundle获取本地化字符串:

NSLocalizedStringFromTableInBundle(stringThatNeedsToBeLocalized, nil, localeBundle, nil);

此外,对于日期格式,您可能需要查看

[NSDateFormatter dateFormatFromTemplate:@"HH:mm:ss"" options:0 locale:locale];

要使用它,您需要为您希望使用的相应语言/国家/地区创建一个NSLocale。

wecizke3

wecizke34#

这就是我所做的。我猜诀窍是使用NSLocalizedStringFromTableInBundle而不是NSLocalizedString。
对于所有字符串,使用这个

someLabel.text = NSLocalizedStringFromTableInBundle(@"Your String to be localized, %@",nil,self.localeBundle,@"some context for translators");

要更改语言,请运行以下代码

NSString * language = @"zh-Hans"; //or whatever language you want
    NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj"];
    if (path) {
        self.localeBundle = [NSBundle bundleWithPath:path];
    }
    else {
        self.localeBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"] ];
    }

在此之后,您可能需要调用任何更新代码来将字符串更新为新语言,例如。再运行一次

someLabel.text = NSLocalizedStringFromTableInBundle(@"Your String to be localized, %@",nil,self.localeBundle,@"some context for translators");

就这样。不需要重新启动应用程序。兼容系统设置以及(如果你通过iOS设置设置设置语言,它也会工作)。不需要外部图书馆。不需要越狱。它也适用于genstrings。
当然,您仍然应该执行通常的操作,以保持您的应用设置:

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"zh-Hans", nil] forKey:@"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];

(and在你的viewDidLoad或其他东西中做一个检查)

NSString * language = [[NSLocale preferredLanguages] objectAtIndex:0];
    NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj"];
    if (path) {
        self.localeBundle = [NSBundle bundleWithPath:path];
    }
    else {
        self.localeBundle = [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"en" ofType:@"lproj"] ];
    }
pes8fvy9

pes8fvy95#

您应该创建自己的宏,类似于NSLocalizedString,但它选择字符串的捆绑包基于您设置的NSUserDefaults值(即不用担心苹果语言默认值的值是多少)
当您更改语言时,您应该发出一个通知,说明哪些视图控制器、视图等应该监听和刷新自己

相关问题