开源软件名称(OpenSource Name):tomkowz/Swifternalization开源软件地址(OpenSource Url):https://github.com/tomkowz/Swifternalization开源编程语言(OpenSource Language):Swift 98.4%开源软件介绍(OpenSource Introduction):SwifternalizationSwift library that helps in localizing apps in a different, better, simpler, more powerful way than system localization does. It uses json files instead of strings files. Swift 3Swift 3 compatible version is available on Features
Table of Contents
IntroductionSwifternalization helps in localizing apps in a smarter way. It has been created because of necessity to solve Polish language internalization problems but it is universal and works with every language very well. It uses JSON files and expressions that avoid writing code to handle some cases that you have to write when not using this framework. It makes localizing process simpler. Practical Usage ExampleDescription of practical usage example will use things that are covered later in the document so keep reading it to the end and then read about details/features presented here. ProblemLet's assume the app supports English and Polish languages. Naturally app contains two Localizable.strings files. One is for Base localization which contains English translation and one is Polish language. App displays label with information which says when object from the backend has been updated for the last time, e.g. "2 minutes ago", "3 hours ago", "1 minute ago", etc. AnalysisThe label displays number and a hour/minute/second word in singular or plural forms with "ago" suffix. Different languages handles pluralization/cardinal numbering in slight different ways. Here we need to support English and Polish languages. In English there are just two cases to cover per hour/minute/second word:
In Polish it is more tricky because the cardinal numbers are more complicated:
Following chapters will present solution without and with Swifternalization framework. Each solution describes Base (English) and Polish localizations. Here is a table with Language Plural Rules which covers cardinal forms of numbers in many languages - Many language handle plurality in their own way. Solution without Swifternalization
There are 6 cases in English and 9 cases in Polish. Notice that without additional logic we're not able to detect which version of a string for hour/minute/second the app should display. The logic differs among different languages. We would have to add some lines of code that handle the logic for all the languages we're using in the app. What if there are more than 2 languages? Don't even think about it - this might be not so easy. The logic is already implemented in Swifternalization framework and it fits to every language. Solution with SwifternalizationThis is how localizable files will look:
As mentioned the logic is implemented into framework so if you want to get one of a localized string you have to make a simple call.
or with
The key and intValue parameters are validated by loaded expressions and proper version of a string is returned - covered below. FeaturesPluralizationSwifternalization drops necessity of using stringdicts files like following one to support pluralization in localized strings. Instead of that you can simply define expressions that cover such cases.
No more stringsdict files! Length VariationsiOS 9 provides new way to select proper localized string variation depending on a screen width. It uses stringsdict file with NSStringVariableWidthRuleType key. Swifternalization drops necessity of using such file and it is not necessary to use this new key to use the feature. With Swifternalization this length variations feature is available since iOS 8.0 because the framework has its own implementation of length variations. To use length variations feature your translation file should has entries like this:
The number after To get the second localized string the call looks like below:
You can mix expressions with length variations. Following example shows it:
ExpressionsThere are few expression types. Every expression type has their own parser and matcher but they work internally so you don't need to worry about them. There are 3 types of expressions:
Inequality ExpressionsIt is composed of several elements:
Example:
Inequality Extended ExpressionsThis is extended version of inequality expression. It is composed of 2 values, one value "marker" and two inequality signs.
Expample:
Regex ExpressionsThis is the most powerful type of expression. It takes regular expression ;)
Example: (police cars in Polish language)
Powerful stuff, isn't it? :> PS. There is built-in solution for Polish language so you can use it with doing just this:
This is called "Shared Built-In Expression" and is covered below. Shared ExpressionsShared expressions are expressions available among all the localization files. They are declared in expressions.json file divided by language and you can use them in localization files. The functionality allows developer to observance of DRY principle and to avoid mistakes that exist because of reapeating the code in many places. Normally you declare expression like this:
If you want to use the same expression in multiple files there is no necessity to repeat the expression elsewhere. This is even problematic when you decide to improve/change expression to handle another cases you forget about - you would have to change expression in multiple places. Because of that there are Shared Expression. These feature allows you to create expression just in one place and use identifier of it in multiple places where you normally should put this expression. What you need to do is to create expressions.json file with following structure:
Now in pl.json, en.json and so on you have to use it as below:
Before you decide to create your own expression take a look if there is no built-in one with the same name or whether there is such expression but named differently. Maybe you don't need to do this at all and just use it. Built-in expressionsBuilt-in expressions as name suggest are shared expressions built into framework and available to use with zero configuration. They are separated by country and not all country have its own built-in expressions. For now there are e.g. Base built-in expressions and Polish built-in expressions. Base expressions are available in every country and there are very generic to match all countries pluralization/cardinal numbering logic. List of supported built-in shared expressions:
As you can see polish has no "one", ">one", etc. because it inherits from Base by default. Getting StartedThis chapter shows you how to start using Swifternalization and how to intergrate it with your code. DocumentationDocumentation covers 100% of the code, Yay! There are two types of documentation. First covers only public API which is for those who only want to use the framework without looking inside. The second one covers all the API - public, internal and private. You can find Public API and Full documentation with docset here in docs directory. It is also hosted on my blog: Docsets: InstallationIt works with iOS 8.0 and newer. With CocoaPods:
If you are not using CocoaPods just import files from Swifternalization/Swifternalization directory to your project. Swifternalization also supports Carthage. Configuration - OptionalBefore you get a first localized string you have to configure Swifternalization by passing to it the bundle where localized json files are placed.
If you do not call It will get languages from bundle which was used to configure it. So, if it does not translate you string, please make sure that you added Localizations in Project > Info > Localizations section - the same place like for regular localizations.
Creating file with Shared ExpressionsShared Expressions must be placed in expressions.json. Syntax of a file looks like below:
In pseudo-language:
Expressions from the files may be used inside localizable files. All the shared expressions for different languages are placed in the same file because there will be just few expressions for every language. Mostly the expression will be defined in base variant because if expression is in base it is also available in every other language too. So, "ten" is available in "pl", but "three" is not available in "base". Creating file with localization per countryLocalizable file contains translations for specific language. The files might look like below:
Name of a file should be the same like country code. e.g. for English it is en.json, for Polish it is pl.json, for base localization it is base.json, etc. There are few things that you can place in such files. More complex file will look like below:
In pseudo-language:
Getting localized stringSwifternalization allows you to work with its one class method which exposes all the methods you need to localize an app. These methods have many optional paramters and you can omit them if you want. There are few common parameters:
First method called Examples:
Next method
The last method
ContributionSwifternalization is open sourced so everyone may contribute if want to. If you want to develop it just fork the repo, do your work and create pull request. If you have some idea or question feel free to create issue and add proper tag for it. There is no guide for contributors but if you added new functionality you must write unit tests for it. Things to do in future releases
LICENSESwifternalization is released under the MIT license. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论