• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Baglan/MCLocalization: Set of tools to simplify "on the fly" localizat ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

Baglan/MCLocalization

开源软件地址(OpenSource Url):

https://github.com/Baglan/MCLocalization

开源编程语言(OpenSource Language):

Objective-C 92.8%

开源软件介绍(OpenSource Introduction):

MCLocalization

Set of tools to simplify "on the fly" localization in iOS apps.

Reasons

There is the "standard" way Apple of localizing iOS apps using NSLocalizedString. It's versatile enought and you should stick to it unless you have good reasons not to.

I reuse the same localization files in an iOS app, on a web site and, possibly, in an app made for another platform. Advantage of JSON there is that libraries for handling JSON are available for just about any modern platform.

As a side advantage, it is much easier to create tools which do not rely on localizers being proficient with escaping C strings or use translation services not geared towards iOS app localization.

File format

MCLocalization uses strings files in JSON format. Internally, it is a collection of collections of strings. Here's an example from the sample project:

{
    "en": {
        "greeting": "Hello!",
        "message": "Tap on the buttons below to switch languages",
      	"glory": "Glory Glory, %name%!",
        "mustache": "mustache",
        "lovely-mustache": "What a wonderful {{mustache}} you have!"
    },
    "ru": {
        "greeting": "Привет!",
        "message": "Нажимайте на кнопки для смены языка",
      	"glory": "Славься славься, %name%!",
        "mustache": "усы",
        "lovely-mustache": "Какие замечательные у Вас {{mustache}}!"
    }
}

In case of using multiple JSON files, one for each individual language, file should contain strings for only that language:

{
    "greeting": "Hello!",
    "message": "Tap on the buttons below to switch languages",
    "glory": "Glory Glory, %name%!",
    "mustache": "mustache",
    "lovely-mustache": "What a wonderful {{mustache}} you have!"
}

Collection of strings for each language is referenced by a canonicalized IETF BCP 47 language identifier (the same identifier used in NSLocale). Strings in a collection are further identified by keys.

Installation

Add files from the 'Classes' folder to your project.

Usage

Initialize localization by loading strings:

Using a single JSON file:

[MCLocalization loadFromURL:[[NSBundle mainBundle] URLForResource:@"strings.json" withExtension:nil] defaultLanguage:@"en"];

Using multiple JSON files, one for each language:

NSDictionary * languageURLPairs = @{
    @"en":[[NSBundle mainBundle] URLForResource:@"en.json" withExtension:nil],
    @"ru":[[NSBundle mainBundle] URLForResource:@"ru.json" withExtension:nil],
};
[MCLocalization loadFromLanguageURLPairs:languageURLPairs defaultLanguage:@"en"];

Legacy way using a file path:

NSString * path = [[NSBundle mainBundle] pathForResource:@"strings.json" ofType:nil];
[MCLocalization loadFromJSONFile:path defaultLanguage:@"en"];

MCLocalization will try to determine the best mathing language based on device's language preferences, defaultLanguage prameter is a "fall-through" setting in case there is no match.

Here's how you fetch a localized string:

_label.text = [MCLocalization stringForKey:@"greeting"];

Here is how you use placeholders:

// Given the "key": "%a% {{b}} [c]", the following call will return "A B C"
[MCLocalization stringForKey:@"key" withPlaceholders:@{@"%a%":@"A", @"{{b}}":@"B"}, @"[c]":@"C"];

MCLocalization was designed to aid "instant" localization so that app's UI could update right after user updates the language setting. To update the localization language, set it like this:

[MCLocalization sharedInstance].language = @"ru";

Localization language will be set and a MCLocalizationLanguageDidChangeNotification notification will be send. This setting is stored in user defaults and next time app is launched, this setting will be used.

Recommended usage pattern

For using MCLocalization, I recommend this pattern:

Load localization strings in AppDelegate application:didFinishLaunchingWithOptions::

NSString * path = [[NSBundle mainBundle] pathForResource:@"strings.json" ofType:nil];
[MCLocalization loadFromJSONFile:path defaultLanguage:@"en"];

In UIViewController you want to localize, collect all the localization code in a dedicated function:

- (void)localize
{
    _greetingLabel.text = [MCLocalization stringForKey:@"greeting"];
    _messageLabel.text = [MCLocalization stringForKey:@"message"];
    _labelPlaceholders.text = [MCLocalization stringForKey:@"glory" withPlaceholders:@{@"%name%":@"Man United"}];
    _mustacheLabel.text = [MCLocalization stringForKey:@"lovely-mustache" withPlaceholders:@{@"{{mustache}}":[MCLocalization stringForKey:@"mustache"]}];
}

Call that function from the viewDidLoad and add view controller as an observer for the MCLocalizationLanguageDidChangeNotification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(localize) name:MCLocalizationLanguageDidChangeNotification object:nil];
[self localize];

Handling missing localization

By default, localized string for a missing key will be nil. noKeyPlaceholder text can be set instead:

[MCLocalization sharedInstance].noKeyPlaceholder = @"[No '{key}' in '{language}']";

{key} and {language} placeholders will be replaced with corresponding settings.

License

Code in this project is available under the MIT license.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap