I am happy to annouce my new framework written in Swift I built this week, Yay! As mentioned in previous post about Test Driven Development I was working this week on framework that solves problem of app localization and plural strings support in smarter way - And there it is - Swifternalization.
It simplifies a lot process of localizing strings depending on numbers, like e.g. “1 car”, “2 cars”, etc. This is easy in English but it might be tricky in Polish. I don’t want to copy-paste entire framework description from github page - feel free to go there for implementation details - I’ll cover it a bit here.
Instead of creating localized strings like this below and coding all the logic in all places where you want to display proper localized string you can do it smarter…
Logic for Polish localization:
- 0, (5 - 21), (25 - 31), … - “few-seconds”
- 1 - “one-second”
- (2 - 4), (22-24), (32-34), (42, 44), …, (162-164), … - “many-seconds”
By smarter I meant using expressions. Like these:
Or like this for tricky Polish ordering:
Expressions may be inequalities or regular expressions. To help you organize
it better there is functionality called “shared expressions”. When you read
detail implementation on the github repo’s page you’ll learn that
Localizable.strings files could looks like this:
Nice, ha? And what is the best, all the logic is contained in Swifternalization framework. You only have to pass some expressions. It works really nice with my app that I am working on recently.
To get localized string do simple call:
Swifternalization has also built-in expressions like: one, >one, two, other (for every language) and few and more for Polish which handles this tricky validation like in examples above. Framework is easy to extend and I hope there will be more built-in expressions for other countries too.
Swifternalization is ready for Swift 2, just checkout swift2 branch.
I hope you find it useful as I did ;)