简体   繁体   English

如何使用iphone SDK中的Two Button在运行时更改应用程序语言?

[英]how to change Application language on run time using Two Button in iphone SDK?

i am building up a project in ios platform and want to use two language in this application and also want . 我正在ios平台上构建一个项目,并希望在这个应用程序中使用两种语言并且也想要。 the user can change the language on Runtime suppose we can take two language button button 1 for english and button 2 for german and the user can change the application language at any time by using these button . 用户可以在运行时更改语言,假设我们可以将两个语言按钮1用于英语,按钮2用于德语,用户可以使用这些按钮随时更改应用程序语言。 any help or tutorials 任何帮助或教程

Thanks in advance 提前致谢

Assuming that you have created localizations for English and German, your app will use the language selected in Settings (this is the preferred option). 假设您已经为英语和德语创建了本地化,您的应用将使用在“设置”中选择的语言(这是首选选项)。

If you want to set the language directly from your application: 如果要直接从应用程序设置语言:

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

will set the language for your application only to English (for German, I assume the key is "de"). 将您的应用程序的语言设置为仅英语(对于德语,我假设键是“de”)。 You should also: 你还应该:

[[NSUserDefaults standardUserDefaults] synchronize];

This will not take effect until your application restarts. 在您的应用程序重新启动之前,这不会生效。

Have a class "LanguageUtils" with that method : 使用该方法有一个“LanguageUtils”类:

- (NSString *) localizedString:(NSString *)key
{
    if (self.currentLanguage == nil) {
        self.currentLanguage = @"en";
    }

    NSString* path = [[NSBundle mainBundle] pathForResource:[self.currentLanguage lowercaseString] ofType:@"lproj"];
    NSBundle* languageBundle = [NSBundle bundleWithPath:path];
    return [languageBundle localizedStringForKey:key value:@"" table:nil];
}

And the property NSString currentLanguage. 和属性NSString currentLanguage。

Then in your .pch do : 然后在你的.pch做:

#undef NSLocalizedString
#define NSLocalizedString(key, _comment) [[Language sharedInstance] localizedString:key]

Works like a charm and you can redefine the current language at runtime. 像魅力一样工作,您可以在运行时重新定义当前语言。

Just keep in mind that your view should be refreshed by yourself. 请记住,您的观点应该由您自己刷新。 It won't automatically do it for you. 它不会自动为你做。

I came up with a solution that allows you to use NSLocalizedString . 我想出了一个允许你使用NSLocalizedString的解决方案。 I create a category of NSBundle call NSBundle+RunTimeLanguage . 我创建了一个NSBundle类的NSBundle+RunTimeLanguage The interface is like this. 界面是这样的。

// NSBundle+RunTimeLanguage.h
#import <Foundation/Foundation.h>
@interface NSBundle (RunTimeLanguage)
#define NSLocalizedString(key, comment) [[NSBundle mainBundle] runTimeLocalizedStringForKey:(key) value:@"" table:nil]
- (NSString *)runTimeLocalizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName;
@end

The implementation is like this. 实现是这样的。

// NSBundle+RunTimeLanguage.m
#import "NSBundle+RunTimeLanguage.h"
#import "AppDelegate.h"

@implementation NSBundle (RunTimeLanguage)

- (NSString *)runTimeLocalizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)tableName
{
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    NSString *path= [[NSBundle mainBundle] pathForResource:[appDelegate languageCode] ofType:@"lproj"];
    NSBundle *languageBundle = [NSBundle bundleWithPath:path];
    NSString *localizedString=[languageBundle localizedStringForKey:key value:key table:nil];
    return localizedString;
}
@end

Than just add import NSBundle+RunTimeLanguage.h into the files that use NSLocalizedString . 不只是将导入NSBundle+RunTimeLanguage.h添加到使用NSLocalizedString的文件中。

As you can see I store my languageCode in a property of AppDelegate . 如您所见,我将languageCode存储在AppDelegate的属性中。 This could be stored anywhere you'd like. 这可以存储在您喜欢的任何地方。

This only thing I don't like about it is a Warning that NSLocalizedString marco redefined. 我唯一不喜欢的是NSLocalizedString marco重新定义的警告。 Perhaps someone could help me fix this part. 也许有人可以帮我解决这个问题。

This is very good and the language is changed within the app and also when device language changes. 非常好,并且在应用程序内以及设备语言更改时也会更改语言。 I used this in many apps. 我在很多应用程序中使用过它。

For localization we generally use 对于我们通常使用的本地化

 NSLocalizedString(@"hello",@"Hello World"); 

in the custom implementation they have something similar like this 在自定义实现中,他们有类似的东西

 AMLocalizedString(@"hello",@"Hello World");

Here is demo tutorial 这是演示教程

https://github.com/object2dot0/Advance-Localization-in-ios-apps https://github.com/object2dot0/Advance-Localization-in-ios-apps

Just remove 只需删除

 [self dealloc];

from

 -(IBAction) languageSelection:(id)sender

so it wont be crash!!! 所以它不会崩溃! enjoy! 请享用!

You have to use localization for this . 你必须使用本地化。 Just store the key and value in side localizable.string and load the appropriate .string file as per your requirement . 只需将键和值存储在localizable.string中,然后根据您的要求加载相应的.string文件。 You can refer following tutorial for this : http://www.raywenderlich.com/2876/how-to-localize-an-iphone-app-tutorial 您可以参考以下教程: http//www.raywenderlich.com/2876/how-to-localize-an-iphone-app-tutorial

SWIFT 2.0 version We can do this on run time without restarting the app in Swift 2.0 as follows SWIFT 2.0版本我们可以在运行时执行此操作,而无需在Swift 2.0中重新启动应用程序,如下所示

Make a function within a class, lets say LanguageManager is the name of the class 在类中创建一个函数,假设LanguageManager是类的名称

class LanguageManager
{

    static let sharedInstance =  LanguageManager()

    //Make a function for Localization. Language Code is the one which we will be deciding on button click.
    func LocalizedLanguage(key:String,languageCode:String)->String{

        //Make sure you have Localizable bundles for specific languages.
        var path = NSBundle.mainBundle().pathForResource(languageCode, ofType: "lproj")

        //Check if the path is nil, make it as "" (empty path)
        path = (path != nil ? path:"")

        let languageBundle:NSBundle!

        if(NSFileManager.defaultManager().fileExistsAtPath(path!)){
            languageBundle = NSBundle(path: path!)
            return languageBundle!.localizedStringForKey(key, value: "", table: nil)
        }else{
            // If path not found, make English as default
            path = NSBundle.mainBundle().pathForResource("en", ofType: "lproj")
            languageBundle = NSBundle(path: path!)
           return languageBundle!.localizedStringForKey(key, value: "", table: nil)
        }
    }
}

How to Use this. 如何使用它。
Now Assume you have two Languages for the application (English and Spanish) 现在假设您有两种语言的应用程序(英语和西班牙语)
English - en 英语 - en
Spanish - es 西班牙语 - es
在此输入图像描述

Suppose you have to place something for a label 假设您必须为标签放置一些东西

//LOGIN_LABEL_KEY is the one that you will define in Localizable.strings for Login label text
 logInLabel.text = LanguageManager.sharedInstance.LocalizedLanguage("LOGIN_LABEL_KEY", languageCode: "es")

//This will use the Spanish version of the text. 
//"es" is the language code which I have hardcoded here. We can use a variable or some stored value in UserDefaults which will change on Button Click and will be passed in loginLabel

For more info on language codes see this link below 有关语言代码的更多信息,请参阅以下链接
https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/LanguageandLocaleIDs/LanguageandLocaleIDs.html https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/LanguageandLocaleIDs/LanguageandLocaleIDs.html

or 要么

http://www.ibabbleon.com/iOS-Language-Codes-ISO-639.html http://www.ibabbleon.com/iOS-Language-Codes-ISO-639.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM