From 146ffc31abd8789a01f2b164a920cccc23bd3062 Mon Sep 17 00:00:00 2001 From: Miroslav Kovac Date: Mon, 28 Aug 2017 22:23:15 +0200 Subject: [PATCH] API improvements, updated README.md --- README.md | 30 +++++-------------- .../Lingo/DataSources/FileDataSource.swift | 2 +- .../DataSources/LocalizationDataSource.swift | 2 +- Sources/Lingo/Lingo.swift | 2 +- 4 files changed, 11 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index d76d93b..4c86ac1 100644 --- a/README.md +++ b/README.md @@ -46,30 +46,14 @@ Add the dependency: ```swift dependencies: [ ..., - .Package(url: "https://github.com/miroslavkovac/Lingo.git", majorVersion: 2) + .Package(url: "https://github.com/miroslavkovac/Lingo.git", majorVersion: 3) ] ``` -Optionally, if you are using Xcode, you can generate Xcode project by running: - -```swift -swift package generate-xcodeproj -``` - Create an instance of `Lingo` object passing the root directory path where the localization files are located: ```swift -import Vapor -import Lingo - -let config = try Config() - -// Create Lingo -let lingo = Lingo(rootPath: config.workDir.appending("Localizations"), defaultLocale: "en") - -let droplet = try Droplet(config) -try drop.run() - +let lingo = try Lingo(rootPath: "path/to/localizations", defaultLocale: "en") ``` ## Usage @@ -130,9 +114,11 @@ print(unread1) // Will print: "You have an unread message." print(unread24) // Will print: "You have 24 unread messages." ``` -Each language contains custom pluralization rules. Lingo currently implements rules for the following languages: +Each language contains custom pluralization rules that define which plural category should be used for which numeric value. Lingo currently implements rules for the following languages: > ak, am, ar, az, be, bg, bh, bm, bn, bo, br, bs, by, ca, cs, cy, da, de\_AT, de\_CH, de\_DE, de, dz, el, en\_AU, en\_CA, en\_GB, en\_IN, en\_NZ, en, eo, es\_419, es\_AR, es\_CL, es\_CO, es\_CR, es\_EC, es\_ES, es\_MX, es\_NI, es\_PA, es\_PE, es\_US, es\_VE, es, et, eu, fa, ff, fi, fil, fr\_CA, fr\_CH, fr\_FR, fr, ga, gd, gl, guw, gv, he, hi\_IN, hi, hr, hsb, hu, id, ig, ii, it\_CH, it, iu, ja, jv, ka, kab, kde, kea, km, kn, ko, ksh, kw, lag, ln, lo, lt, lv, mg, mk, ml, mn, mo, mr\_IN, ms, mt, my, naq, nb, ne, nl, nn, nso, or, pa, pl, pt, ro, root, ru, sah, se, ses, sg, sh, shi, sk, sl, sma, smi, smj, smn, sms, sr, sv\_SE, sv, sw, th, ti, tl, to, tr, tzm, uk, ur, vi, wa, wo, yo, zh\_CN, zh\_HK, zh\_TW, zh\_YUE, zh +The origial seed of pluralization rules was translated from [Rails i18n](https://github.com/svenfuchs/rails-i18n/tree/master/rails/pluralization) into Swift. + ## Performance In tests with a set of 1000 localization keys including plural forms, the library was able to handle: @@ -151,8 +137,8 @@ To implement a custom data source, all you need is to have an object that confor ```swift public protocol LocalizationDataSource { - func availableLocales() -> [LocaleIdentifier] - func localizations(`for` locale: LocaleIdentifier) -> [LocalizationKey: Localization] + func availableLocales() throws -> [LocaleIdentifier] + func localizations(forLocale: LocaleIdentifier) throws -> [LocalizationKey: Localization] } ``` @@ -161,7 +147,7 @@ So, let's say you are using MongoDB to store your localizations, all you need to ```swift let mongoDataSource = MongoLocalizationDataSource(...) -let lingo = Lingo(dataSource: mongoDataSource, defaultLocale: "en") +let lingo = try Lingo(dataSource: mongoDataSource, defaultLocale: "en") ``` Lingo already includes `FileDataSource` conforming to this protocol, which, as you might guess, is wired up to the Longo's convenience initializer with `rootPath`. diff --git a/Sources/Lingo/DataSources/FileDataSource.swift b/Sources/Lingo/DataSources/FileDataSource.swift index 1bca9f7..31e0239 100644 --- a/Sources/Lingo/DataSources/FileDataSource.swift +++ b/Sources/Lingo/DataSources/FileDataSource.swift @@ -23,7 +23,7 @@ public final class FileDataSource: LocalizationDataSource { } } - public func localizations(for locale: LocaleIdentifier) throws -> [LocalizationKey : Localization] { + public func localizations(forLocale locale: LocaleIdentifier) throws -> [LocalizationKey : Localization] { let jsonFilePath = "\(self.rootPath)/\(locale).json" var localizations = [LocalizationKey: Localization]() diff --git a/Sources/Lingo/DataSources/LocalizationDataSource.swift b/Sources/Lingo/DataSources/LocalizationDataSource.swift index 90dd685..a36dbf9 100644 --- a/Sources/Lingo/DataSources/LocalizationDataSource.swift +++ b/Sources/Lingo/DataSources/LocalizationDataSource.swift @@ -10,6 +10,6 @@ public protocol LocalizationDataSource { func availableLocales() throws -> [LocaleIdentifier] /// Return localizations for a given locale. - func localizations(`for` locale: LocaleIdentifier) throws -> [LocalizationKey: Localization] + func localizations(forLocale locale: LocaleIdentifier) throws -> [LocalizationKey: Localization] } diff --git a/Sources/Lingo/Lingo.swift b/Sources/Lingo/Lingo.swift index 713bc66..b4a8a9e 100644 --- a/Sources/Lingo/Lingo.swift +++ b/Sources/Lingo/Lingo.swift @@ -34,7 +34,7 @@ public final class Lingo { print("WARNING: Invalid locale identifier: \(locale)") } - let localizations = try dataSource.localizations(for: locale) + let localizations = try dataSource.localizations(forLocale: locale) self.model.addLocalizations(localizations, for: locale) } }