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

前端 未结 7 1296
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-17 06:30

i am building up a project in ios platform and want to use two language in this application and also want . the user can change the language on Runtime suppose we can ta

相关标签:
7条回答
  • 2020-12-17 06:53

    SWIFT 2.0 version We can do this on run time without restarting the app in Swift 2.0 as follows

    Make a function within a class, lets say LanguageManager is the name of the class

    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
    Spanish - 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

    or

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

    0 讨论(0)
  • 2020-12-17 06:54

    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 . You can refer following tutorial for this : http://www.raywenderlich.com/2876/how-to-localize-an-iphone-app-tutorial

    0 讨论(0)
  • 2020-12-17 06:57

    Here is demo tutorial

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

    Just remove

     [self dealloc];
    

    from

     -(IBAction) languageSelection:(id)sender
    

    so it wont be crash!!! enjoy!

    0 讨论(0)
  • 2020-12-17 06:58

    I came up with a solution that allows you to use NSLocalizedString. I create a category of NSBundle call 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.

    As you can see I store my languageCode in a property of 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. Perhaps someone could help me fix this part.

    0 讨论(0)
  • 2020-12-17 07:06

    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"). You should also:

    [[NSUserDefaults standardUserDefaults] synchronize];
    

    This will not take effect until your application restarts.

    0 讨论(0)
  • 2020-12-17 07:06

    Have a class "LanguageUtils" with that method :

    - (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.

    Then in your .pch do :

    #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.

    0 讨论(0)
提交回复
热议问题