Swiftpack.co - MihaelIsaev/Localizer as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by MihaelIsaev.
MihaelIsaev/Localizer 1.0.2
๐Ÿ‡ฎ๐Ÿ‡ธ๐Ÿ‡ฉ๐Ÿ‡ช๐Ÿ‡ฏ๐Ÿ‡ต๐Ÿ‡ฒ๐Ÿ‡ฝ Swift localization helper
โญ๏ธ 14
๐Ÿ•“ 3 years ago
.package(url: "https://github.com/MihaelIsaev/Localizer.git", from: "1.0.2")

MIT License Swift 4.2 Cocoapod Swift.Stream


Localizer

Install through Swift Package Manager

.package(url: "https://github.com/MihaelIsaev/Localizer.git", from: "1.0.2")

or CocoaPods

pod 'Localizer', '~> 1.0.2'

How to use

The most important part is to import it ๐Ÿš€

import Localizer

iOS

// create string relative to current language
let myString = String(
    .en("Hello"),
    .fr("Bonjour"),
    .ru("ะŸั€ะธะฒะตั‚"),
    .es("Hola"),
    .zh_Hans("ไฝ ๅฅฝ"),
    .ja("ใ“ใ‚“ใซใกใฏ"))
print(myString)

By default current language is equal to Locale.current but you can change it by setting Localizer.current = .en. Also localizer have default language in case if user's language doesn't match any in your string, and you could set it just by calling Localizer.default = .en.

Server-side

On server-side we can't detect user's locale through Locale.current cause this way we will get server's locale :)

So e.g. if we have User model on server which is have locale string variable we could use it. For that we should conform User to Localizable like this

class User: Localizable {
    // declare this as a link to locale variable in this model
    static var localeKey: LocaleKey? { return \.locale }

    var id: UUID
    var email, password: String

    /// this variable will be used for Localizer to detect language
    /// it should contain e.g. short `en` or long `en_US` value
    var locale: String?
}

then declare your localized strings with user obejct like this

let myString = String(for: user,
    .en("Hello"),
    .fr("Bonjour"),
    .ru("ะŸั€ะธะฒะตั‚"),
    .es("Hola"),
    .zh_Hans("ไฝ ๅฅฝ"),
    .ja("ใ“ใ‚“ใซใกใฏ"))
print(myString)

Easy, right? :) Would be great if you could give it a star โญ๏ธ

GitHub

link
Stars: 14
Last commit: 3 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics