Giter Club home page Giter Club logo

jylocalized's Introduction

JYLocalized

License MIT  CocoaPods  CocoaPods  Support 

🌏 iOS localization/Internationalization and in-app switching language example.

JYLocalized

Localization/Internationalization

In-app switching language

In this example, In-app switching language typically has the following steps:

JYLocalized

Key code

JYLanguageViewController.m

// Set new language
[[NSBundle localizedBundle] setUserLanguage:key];
// send reload root viewController notification
[[NSNotificationCenter defaultCenter] postNotificationName:@"kNotifyRootViewControllerReset" object:nil];

NSBundle+JYLocalized.m

- (void)setUserLanguage:(NSString *)language {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj"];
    // construct a new bundle
    [NSBundle setLocalizedBundle:[NSBundle bundleWithPath:path]];
    // Store new language tags locally
    [defaults setObject:language forKey:kUserLanguage];
    [defaults synchronize];
}

AppDelegate.m

- (void)resetRootViewController {
    // Add animations
    @weakify(self);
    [[JYTabBarManager manager] resetTabBarController];
    [UIView transitionWithView:self.window
                      duration:0.3
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        @strongify(self);
                        // Switch the RootViewController to reload all viewController
                        [self.window setRootViewController:nil];
                        [self.window setRootViewController:[JYTabBarManager manager].tabBarController];
                        [[JYTabBarManager manager] tabBarController].selectedIndex = 0;
                        [self.window makeKeyAndVisible];
                    } completion:nil];
}

License

JYLocalized is released under the MIT license. See LICENSE file for details.


中文介绍

🌏 iOS本地化/国际化与应用内切换语言的一个范例。

本地化/国际化

应用内切换语言

在该示例中,应用中切换语言一般会经历以下几个步骤:

JYLocalized

关键代码

JYLanguageViewController.m

// 设置新的语言标示
[[NSBundle localizedBundle] setUserLanguage:key];
// 发送重新加载root viewController通知
[[NSNotificationCenter defaultCenter] postNotificationName:@"kNotifyRootViewControllerReset" object:nil];

NSBundle+JYLocalized.m

- (void)setUserLanguage:(NSString *)language {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj"];
    // 构造新的bundle
    [NSBundle setLocalizedBundle:[NSBundle bundleWithPath:path]];
    // 将新的语言标示存入本地
    [defaults setObject:language forKey:kUserLanguage];
    [defaults synchronize];
}

AppDelegate.m

- (void)resetRootViewController {
    // 添加动画
    @weakify(self);
    [[JYTabBarManager manager] resetTabBarController];
    [UIView transitionWithView:self.window
                      duration:0.3
                       options:UIViewAnimationOptionTransitionCrossDissolve
                    animations:^{
                        @strongify(self);
                         // 切换RootViewController使得构建在它上面的ViewController都重新加载
                        [self.window setRootViewController:nil];
                        [self.window setRootViewController:[JYTabBarManager manager].tabBarController];
                        [[JYTabBarManager manager] tabBarController].selectedIndex = 0;
                        [self.window makeKeyAndVisible];
                    } completion:nil];
}

许可证

JYLocalized 使用 MIT 许可证,详情见 LICENSE 文件。

jylocalized's People

Contributors

job-yang avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.