Skip to content

alobanov/ALLocalizedManager

Repository files navigation

ALLocalizedManager

Simple localized manager

Introduction

Sometimes we need to change language of application "on fly", without changing device locale. This manager very helpfull for this purpose

Overview

  1. You can simple usage ALLocalizedString(key, comment) like are NSLocalizedString(key, comment)
  2. Or use you own table
  3. Plural support with awesone TTTLocalizedPluralString

Usage

Read article at alobanov.github.io

Settings up

  1. In your AppDelegate file you configure manager
    [[ALLocalizedManager sharedInstance] setLanguages:@[ @{@"ru": @"Русский"}, @{@"en": @"English"}, @{@"pt-PT": @"Portugal"} ]];
    [[ALLocalizedManager sharedInstance] setDefaultLanguage:@"ru"];
  1. After first running this manager uses default locale of device. Then user change language information stored at NSUserDefaults

  2. You can change language by

    NSString *newLang = @"ru";
    ALLocalizationSetLanguage(newLang);
  1. You can observe language changes by
    [[ALLocalizedManager sharedInstance] addChangeLanguageBlock:^(NSString *newLang) {
        [self updateTitles:nil];
        
    } forObject:self];

Pluralization

For append plural rule to you project follow next steps

  1. Add .strings file by default named LocalizablePlural

  2. Append to file localizable plural rules like this

/* Murloc */
"%d Murloc (plural rule: one)" = "%d мурлок";
"%d Murloc (plural rule: few)" = "%d мурлока";
"%d Murloc (plural rule: many)" = "%d мурлоков";
"%d Murloc (plural rule: other)" = "%d мурлока";
  1. Use plural like this
    NSString *pluralString = ALLocalizedPluralString(@"Murloc", 4, nil);