Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mihaelisaev/localizer
๐ฎ๐ธ๐ฉ๐ช๐ฏ๐ต๐ฒ๐ฝ Swift localization helper
https://github.com/mihaelisaev/localizer
Last synced: 4 months ago
JSON representation
๐ฎ๐ธ๐ฉ๐ช๐ฏ๐ต๐ฒ๐ฝ Swift localization helper
- Host: GitHub
- URL: https://github.com/mihaelisaev/localizer
- Owner: MihaelIsaev
- License: mit
- Created: 2019-08-10T16:12:30.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-06-07T20:52:17.000Z (over 4 years ago)
- Last Synced: 2024-05-23T10:32:12.905Z (9 months ago)
- Language: Swift
- Homepage:
- Size: 38.1 KB
- Stars: 14
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Localizer
Install through Swift Package Manager
```swift
.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 ๐
```swift
import Localizer
```### iOS
```swift
// 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
```swift
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
```swift
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 โญ๏ธ