Code Monkey home page Code Monkey logo

ios-i18n's Introduction

Smartling.i18n.framework

iOS 6+ Plurals Localization Library

Note: this library was designed to support plurals in iOS 6. As of iOS 7, Apple added support for plurals via string dictionaries. New projects should use Apple’s native plural support. This library has been deprecated.

iOS (pre-iOS 7) localization did not support plural functionality out of the box. The ios-i18n library was designed to bridge that gap and provide a means for developers to seamlessly integrate plurals into their localized apps across any number of locales.

As of iOS 7 and Mac OS X 10.9 Mavericks, Foundation has the ability to specify localized strings according to pluralization and grammar rules. You can find more about this in the Handling Noun Plurals and Units of Measurement section of the Internationalization and Localization Guide.

ios-i18n is not compatible with Apple's implementation, and does not handle new ".stringsdict" resource files.

Installation

Via CocoaPods

Update your Podfile with

pod 'Smartling.i18n', '~> 1.0'

Framework bundle

  1. Download [Smartling.i18n.framework.tar.gz] fw
  2. Unpack and drag Smartling.i18n.framework to your project's Frameworks group.
  3. In the Build Settings of your target add -ObjC to Other Linker Flags.
  4. #import <Smartling.i18n/SLLocalization.h>

Carthage

Carthage compatible

Cartfile

github "Smartling/ios-i18n"

Usage

Plurals

int number = 42;
NSString *format = SLPluralizedString(@"%d apples", number, @"Comment");
NSString *text = [NSString stringWithFormat:format, number];

SLPluralizedString...

There are four functions to retrieve pluralized string, similar to NSLocalizedString:

NSString * SLPluralizedString(NSString *key, number, NSString *comment)
NSString * SLPluralizedStringFromTable(NSString *key,
    NSString *tableName,
    number,
    NSString *comment)
NSString * SLPluralizedStringFromTableInBundle(NSString *key,
    NSString *tableName,
    NSBundle *bundle,
    number,
    NSString *comment)
NSString * SLPluralizedStringWithDefaultValue(NSString *key,
    NSString *tableName,
    NSBundle *bundle,
    number,
    NSString *defaultValue,
    NSString *comment)

number can be any primitive type (int, long, float, double), or NSNumber.

Notes

Base internationalization

When using Base internationalization, Localizable.strings files should be in language-specific folders (like en.lproj), not in Base.lproj folder.

Fallback

ios-i18n implements resource fallback for each key query. At runtime, SLPluralizedString retrieves string whose localization most closely matches the preferred language of the user.

In contrast, Apple's implementation of NSLocalizedString first selects appropriate localization table, then uses this table for all subsequent queries. For more details on this mechanism, see [Support for Internationalization] applei18n

Extended .strings format

ios-i18n extends standard .strings file format with pluralized variants.

The extended syntax for key is: KEY##{rule}. Where KEY is the original key string, and rule is one of plural rules: zero, one, two, few, many, other.

The rule portion of the full key conforms to the CLDR spec on plural forms. The ios-i18n library will load a particular translation following the same rules as defined under CLDR.

Sample resource files for key string %d songs found:

en.lproj/Localizable.strings
/* Number of songs from search results */
"%d songs found##{one}" = "One song found";
"%d songs found##{other}" = "%d songs found";
ru.lproj/Localizable.strings
/* Number of songs from search results */
"%d songs found##{one}" = "Найдена %d песня";
"%d songs found##{few}" = "Найдено %d песни";
"%d songs found##{many}" = "Найдено %d песен";
"%d songs found##{other}" = "Найдено %d песен";

Copyright and license

Copyright 2013 Smartling, Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with the License. You may obtain a copy of the License in the LICENSE file, or at:

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

ios-i18n's People

Contributors

belkevich avatar dimitrystd avatar diniska avatar dparshin avatar emilienh avatar foo4u avatar jones-smartling avatar paiv avatar sdsykes avatar vilinskiy-playdayteam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ios-i18n's Issues

Base localizations files

Your lib does not look into base localization file. For example in iOS setting region is English, but I have only Base and French files.

Use of NS_FORMAT_ARGUMENT is wrong

SLLocalization.h declares -pluralizedStringWithKey:defaultValue:table:pluralValue: with an "NS_FORMAT_ARGUMENT(1)". However, that is wrong because that macro is only used for variadic functions where arguments are used as arguments to a format string, which is not the case here.

cachedLocales issue

For Cocoapod v. 1.0.5
cahcedLocales seems to have different behaviour between iOS6 and 7.

Method is:

- (NSArray *)cachedLocales
{
    static const NSString *kSLBundleCachedLocales = @"kSLBundleCachedLocales";
    NSMutableArray *locales = objc_getAssociatedObject(self, (__bridge const void *)(kSLBundleCachedLocales));
    if (!locales) {
        locales = [NSMutableArray arrayWithArray:self.preferredLocalizations];
        if (self.developmentLocalization && ![[locales lastObject] isEqualToString:self.developmentLocalization]) {
            [locales addObject:self.developmentLocalization];
        }
        objc_setAssociatedObject(self, (__bridge const void *)(kSLBundleCachedLocales), locales, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return locales;
}

In

NSMutableArray *locales = objc_getAssociatedObject(self, (__bridge const void *)(kSLBundleCachedLocales)); 

locales can sometimes be nil in iOS7 and an initialized but otherwise empty dictionary in iOS6.
iOS7 behavior seems to be correct.

exc_bad_access on iOS 9

Hi, I'v start using your project, installed via CocoaPods, created sample project and run an app. But simple function [NSString stringWithFormat:SLPluralizedString(@"%@ apples", number, nil), number] terminates app with EXC_BAD_ACCESS

Demo project works well in simulator, but failing compile cause

ld: ../Frameworks/Smartling.i18n.framework/Smartling.i18n(NSBundle+Smartling_i18n.o)' does not contain bitcode. You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. for architecture arm64

special plural message for zero in English

http://cldr.unicode.org/index/cldr-spec/plural-rules

States
The minimal pairs are those that are required for correct grammar. So because 0 and 1 don't have to form a minimal pair (it is ok—even though often not optimal—to say "0 people") , 0 doesn't establish a separate category. However, implementations are encouraged to provide the ability to have special plural messages for 0 in particular, so that more natural language can be used:

None of your friends are online.
rather than
You have 0 friends online.

Is this possible with the Smartling library and if so how?

Incompatibility with Cocoapods >= 1.0

Hello,

Some of my private pods use Smartling. And them stop working after update of cocoapods to 1.0.1. I'm unable to push podspec. It didn't pass the validation. The error is:

xcodebuild:  Smartling.i18n/Smartling.i18n/SmartlingLib.h:32:9: fatal error: 'SmartlingLib/SLLocalization.h' file not found

So, I've tried to validate Smartling podspec with cocoapods 1.0.1. And it fails.

pod spec lint https://raw.githubusercontent.com/Smartling/ios-i18n/master/Smartling.i18n.podspec

 -> Smartling.i18n (1.0.11)
    - ERROR | xcodebuild: Returned an unsuccessful exit code. You can use `--verbose` for more information.
    - NOTE  | xcodebuild:  /Users/alex/Library/Developer/Xcode/DerivedData/App-diyqsuoeckugulbyqokvsklkqffn/Build/Products/Release-iphonesimulator/Smartling.i18n/Smartling_i18n.framework/Headers/SmartlingLib.h:32:9: fatal error: 'SmartlingLib/SLLocalization.h' file not found
    - NOTE  | xcodebuild:  /var/folders/t0/tq3q_d5x2874hby2xtwswzjw0000gn/T/CocoaPods/Lint/App/main.m:3:9: fatal error: could not build module 'Smartling_i18n'
    - NOTE  | [OSX] xcodebuild:  /Users/alex/Library/Developer/Xcode/DerivedData/App-diyqsuoeckugulbyqokvsklkqffn/Build/Products/Release/Smartling.i18n/Smartling_i18n.framework/Headers/SmartlingLib.h:21:9: fatal error: 'UIKit/UIKit.h' file not found

Analyzed 1 podspec.

[!] The spec did not pass validation, due to 1 error.

I think it's related to 'dot' in library name. But I'm not sure.

tar.gz file contains paths including Users/pasha/Documents/

$ tar tf Smartling.i18n.framework.tar
Users/pasha/Documents/Smartling/ios-i18n/Smartling.i18n/../Frameworks/Smartling.i18n.framework/
Users/pasha/Documents/Smartling/ios-i18n/Smartling.i18n/../Frameworks/Smartling.i18n.framework/Headers
Users/pasha/Documents/Smartling/ios-i18n/Smartling.i18n/../Frameworks/Smartling.i18n.framework/Smartling.i18n
Users/pasha/Documents/Smartling/ios-i18n/Smartling.i18n/../Frameworks/Smartling.i18n.framework/Versions/
Users/pasha/Documents/Smartling/ios-i18n/Smartling.i18n/../Frameworks/Smartling.i18n.framework/Versions/A/
Users/pasha/Documents/Smartling/ios-i18n/Smartling.i18n/../Frameworks/Smartling.i18n.framework/Versions/Current
Users/pasha/Documents/Smartling/ios-i18n/Smartling.i18n/../Frameworks/Smartling.i18n.framework/Versions/A/Headers/
Users/pasha/Documents/Smartling/ios-i18n/Smartling.i18n/../Frameworks/Smartling.i18n.framework/Versions/A/Smartling.i18n
Users/pasha/Documents/Smartling/ios-i18n/Smartling.i18n/../Frameworks/Smartling.i18n.framework/Versions/A/Headers/SLLocalization.h

Request: genstrings-like tool to extract strings that works with this library

Apple provides a command-line tool called "genstrings" that searches for occurrences of the NSLocalizedString() macro (as well as the NSLocalizedStringFromTable(), NSLocalizedStringFromTableInBundle(), and NSLocalizedStringWithDefaultValue() macros) in given source code files, and outputs a .strings file for the string literals within. This is very useful.

However, when using this library's SLPluralizedString() macro, genstrings will ignore it since it is not one of the NSLocalizedString family of macros. Therefore, once we use this library, we cannot use genstrings to automatically generate the .strings files anymore, or we have to manually add the SLPluralizedString() entries to the .strings files after generating.

Can you provide a tool that performs the same functionality as "genstrings", but also includes strings from SLPluralizedString()?

Library does not work with table names

The function pluralizedStringWithKey:defaultValue:table:pluralValue: expects the parameter "table" to be a path to file with translations. Instead, it should behave as with Localizable.string file (default case) - the right file path is chosen depending on the locale.

zero is not permitted as pluralized form for English

We require the ability to add a {zero} pluralized form for English, which is distinct from {other}. Smartling's library only allows the required forms to be specified ({one} and {other}, for English).

The CLDR encourages use of such optional forms, for more natural language:

The minimal pairs are those that are required for correct grammar. So because 0 and 1 don't have to form a minimal pair (it is ok—even though often not optimal—to say "0 people") , 0 doesn't establish a separate category. However, implementations are encouraged to provide the ability to have special plural messages for 0 in particular, so that more natural language can be used:

  • None of your friends are online.
    rather than
  • You have 0 friends online.

http://cldr.unicode.org/index/cldr-spec/plural-rules

But Smartling's library does not support this CLDR recommendation.

By not supporting this, apps are forced to either not use the Smartling library, or special-case the zero form in code, which in turn will cause the zero form to break in languages where zero is required.

I noticed this issue was discussed a year and a half ago here, and would like to see this issue revisited.

Base internationalization isn't supported

I am using Base i18n as described here:

https://developer.apple.com/library/ios/documentation/MacOSX/Conceptual/BPInternational/InternationalizingYourUserInterface/InternationalizingYourUserInterface.html

When I look up a string for my Base locale ("en"), it doesn't try looking it up in Base.lproj/table.strings. If I pass "Base" as a locale to pluralizedStringWithKey:defaultValue:table:pluralValue:forLocalization: then it doesn't know the plural rules to apply.

I think I would also want Base to be in the fallback list for other locales but I haven't gotten that far yet.

I can work around now by using pluralformf and looking it up myself. Since this is only a problem for my Base language, I don't mind losing your fallback mechanism.

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.