如何在IOS中运行时切换到语言后获取本地化的故事板字符串? [英] How to get localised storyboard strings after switching to language at runtime in IOS?

查看:87
本文介绍了如何在IOS中运行时切换到语言后获取本地化的故事板字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码来切换语言运行时:

I have following code to switch language runtime:

-(void) switchToLanguage:(NSString *)lang{
    self.language = lang;
    [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:self.language, nil]
                                        forKey:@"AppleLanguages"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

我有一个Helper函数可以检索本地化的字符串:

And I have a Helper function that retrieves localised strings:

+(NSString *) getLocalizedString:(NSString *)key{
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];   
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Localizable"
                                                 ofType:@"strings"
                                            inDirectory:nil
                                        forLocalization:appDelegate.language];

    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
    return [dict objectForKey:key];
}

这是有效的。我的故事板也是本地化的,但是当我切换到另一种语言时它们没有改变。

This is working. My storyboards are also localised, but they are not changing when I switch to another language.

如何获取故事板字符串的本地化值?

How can I get localised values for the storyboard strings?

推荐答案

在运行时更改语言有点棘手。

Changing the Language at run time is a bit tricky.

这是我以前在这个小类的帮助下做到的最佳方式:

This is the best way I've used to do so with the help of this tiny class:

Language.m:

#import "Language.h"

@implementation Language

static NSBundle *bundle = nil;

+(void)initialize {
    NSUserDefaults* defs = [NSUserDefaults standardUserDefaults];
    NSArray* languages = [defs objectForKey:@"AppleLanguages"];
    NSString *current = [languages objectAtIndex:0];
    [self setLanguage:current];

}

+(void)setLanguage:(NSString *)l
{
    NSString *path = [[ NSBundle mainBundle ] pathForResource:l ofType:@"lproj" ];
    bundle = [NSBundle bundleWithPath:path];
}

+(NSString *)get:(NSString *)key alter:(NSString *)alternate
{
    return [bundle localizedStringForKey:key value:alternate table:nil];
}

@end

Language.h :

import <Foundation/Foundation.h>

@interface Language : NSObject
+(void)setLanguage:(NSString *)l;
+(NSString *)get:(NSString *)key alter:(NSString *)alternate;

@end






当您想要更改语言时:


When you want to change the Language:

[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:@"en", @"de", nil] forKey:@"AppleLanguages"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    [Language setLanguage:@"en"];

    [(AppDelegate *)[[UIApplication sharedApplication] delegate] window].rootViewController = [self.storyboard instantiateInitialViewController];

当你想设置字符串时:

[self.someButton setTitle:[Language get:@"Some Button Text" alter:nil] forState:UIControlStateNormal];

这篇关于如何在IOS中运行时切换到语言后获取本地化的故事板字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆