Swiftpack.co - dankinsoid/SwiftLocalize as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by dankinsoid.
dankinsoid/SwiftLocalize 2.6.1
Native Swift localization
⭐️ 2
🕓 47 weeks ago
.package(url: "https://github.com/dankinsoid/SwiftLocalize.git", from: "2.6.1")

SwiftLocalize

CI Status Version License

Description

Library for native Swift localization of your projects.

Example

import Foundation
import SwiftLocalize

public extension String {

   @Localized public var ok: String {
     [.ru: "Да",
      .en: "Ok"]
   }
   @Localized public var cancel: String {
     [.ru: "Отмена",
      .en: "Cancel"]
   }
   @Localized public var never: String {
     [.ru: "Никогда",
      .en: "Never"]
   }
   @Localized public var later: String {
     [.ru: "Позже",
      .en: "Later"]
   }

   public static func coins(for count: Int) -> String {
      Localized([
        .ru: [
          .cases(NumberCase.accusative): "монеты",
          .cases(NumberCase.singular): "монета",
          .cases(NumberCase.genitive): "монет"
        ]
     ]).string(.cases(NumberCase(for: count)))
   }
	
   public static let errors: Localized.Dictionary = [
      "unknown": [.ru: "Неизвестная ошибка", .en: "Unknown error"],
      "server": [.ru: "Ошибка сервера", .en: "Server error"]
   ]
}

Usage

To get a localized string create Localized object:

let word = Localized(formsDictionary)

where string: String - default value, formsDictionary: [Language: Localized.Forms] - dictionary of forms

To get a string for current language use word.localized To get for a custom language or form call

word.string(language, form)

where language: Language - language, default value is Language.current, form: FormType - word form (OptionSet)

Supported forms: none, singular, plural, masculine, feminine, neuter, common and any combination of them.

You can create your own form type (for language cases as example) via LanguageCaseProtocol and use it:

let formType = Localized.FormType.cases(customFormEnum)

The repo contains one custom LanguageCaseProtocol type NumberCase for Russian language as example of usage.

Examples of word with several forms:

let manWord = Localized([
	.ru: [.singular: "человек", .plural: "люди"],
	.en: [
		[.singular, .masculine]: "man", 
		[.plural, .masculine]: "men",
		[.singular, .feminine]: "woman", 
		[.plural, .feminine]: "women"
	     ],
	 .ja: "人"
])

You can combine words to get phrases:

let tree = Localized([
    .ru: [
        [.neuter, .singular]: "дерево",
        .plural: "деревья"
    ]
])
       
let beautiful = Localized([
    .ru: [
        .plural: "красивые",
        .singular: [.masculine: "красивый", .feminine: "красивая", .neuter: "красивое"]
    ]
])
       
let phrase = beautiful + " " + tree

print(phrase.string(language: .ru, .plural))
    //prints "красивые деревья"
print(phrase.string(language: .ru, .singular))
    //prints "красивое дерево"

Installation

  1. Swift Package Manager

Create a Package.swift file.

// swift-tools-version:5.0
import PackageDescription

let package = Package(
  name: "SomeProject",
  dependencies: [
    .package(url: "https://github.com/dankinsoid/SwiftLocalize.git", from: "1.9.0")
  ],
  targets: [
    .target(name: "SomeProject", dependencies: ["SwiftLocalize"])
  ]
)
$ swift build
  1. CocoaPods

Add the following line to your Podfile:

pod 'SwiftLocalize'

and run pod update from the podfile directory first.

Author

Voidilov, [email protected]

License

SwiftLocalize is available under the MIT license. See the LICENSE file for more info.

GitHub

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

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