Latest 1.0.1
Homepage https://github.com/digitrick/LocalizedStringSwift
License MIT
Platforms ios 9.0, requires ARC

Using NSLocalizedString more simply.


Carthage compatible
Swift Package Manager compatible
Twitter
[License]()

Requirements

  • iOS 9.0+
  • Xcode 8.1+
  • Swift 3+

Installation

Cocoapods

Podfile

pod 'LocalizedStringSwift', '~> 1.0'

Carthage

Cartfile

github "digitrick/LocalizedStringSwift" ~> 1.0

Swift Package Manager

Package.swift

.Package( url: "https://github.com/digitrick/LocalizedStringSwift.git", majorVersion: 1)

Usage

Localizations Setting & Localized files (*.strings) to Xcode.

String Extension method

func localized(
        key: String, 
        language: String = default, 
        tableName: String? = default, 
        bundle: Bundle = default, 
        comment: String = default
) -> String
  • Default lauguage is system language
  • Default table name is "Localizable"
  • Default bundle is Bundle.main
  • Default Comment is ""

Priority

Localized languguage file > System language file > Base file > Default string

If "KEY" doesn’t exist in Localized languguage file, it uses System language file.
If "KEY" doesn’t exist in System language file, it uses Base file.
If "KEY" doen’t exist in Base file, it uses "DefaultString"

"DefaultString".localized(key: "KEY")
"DefaultString".localized(key: "KEY", comment: "localized string")
Sample

2 strings files in Xcode.

  • Localizable.strings

    // (English)
    "localizedKey" = "text";
    
    // (Japanese)
    "localizedKey" = "テキスト";
    
    // (Base)
    "localizedKey" = "base text";
  • Test.strings

    // (English)
    "testTitle" = "Title";
    
    // (Japanese)
    "testTable" = "Table";
    
    // (Base)
    "testText" = "Text";

If System language is English,

// return "text"
"DefaultText".localized(key: "localizedKey")
"DefaultText".localized(key: "localizedKey", comment: "English")

// return "テキスト"
"DefaultText".localized(key: "localizedKey", language: "ja")

// return "base text"
"DefaultText".localized(key: "localizedKey", language: "Base", comment: "Base")

// return "DefaultText"
"DefaultText".localized(key: "anotherKey", comment: "Another")

// return "Title"
"DefaultText".localized(key: "testTitle", tableName: "Test")

// return "DefaultText"
"DefaultText".localized(key: "testTitle", language: "ja", tableName: "Test")

// return "DefaultText"
"DefaultText".localized(key: "testTable", tableName: "Test")

// return "Table"
"DefaultText".localized(key: "testTable", language: "ja", tableName: "Test")

// return "Text"
"DefaultText".localized(key: "testText", language: "ja", tableName: "Test", comment: "Base")

// return "DefaultText"
"DefaultText".localized(key: "anotherTestTable", language: "ja", tableName: "Test", comment: "Another")

LocalizedStringSwift Framework

import LocalizedStringSwift
  • Localizations Setting contains English and Japanese
  • System language is English

Localizable languages in Your App

// Identifiers (getter only)
// return ["Base", "en", "ja"]
LocalizedString.language.localizableIdentifiers

// DisplayNames (getter only)
// return ["English", "日本語"]
LocalizedString.language.localizableDisplayNames

Default Language

System Language

// Identifier (getter only)
// return "en"
LocalizedString.language.defaultIdentifier

// DisplayName (getter only)
// return "English"
LocalizedString.language.defaultDisplayName

Current Language

// Identifier (getter/setter)
// return "en"
LocalizedString.language.currentIdentifier

LocalizedString.language.currentIdentifier = "ja"
// "ja"
print(LocalizedString.language.currentIdentifier)

// DisplayName (getter only)
// return "English"
LocalizedString.language.currentDisplayName

LocalizedString.language.currentIdentifier = "ja"
// "日本語"
print(LocalizedString.language.currentDisplayName)

Identifier to DisplayName

/// return "English"
LocalizedString.language.displayName(byId: "en")

/// return "日本語"
LocalizedString.language.displayName(byId: "ja")

Reset to default language

LocalizedString.language.resetLanguage()

Notification

LocalizedLanguageChanged notification is emit when currentIdentifier is set.

NotificationCenter.default.addObserver(self, selector: #selector(languageChanged), name: NSNotification.Name(.localizedLanguageChanged), object: nil)

func languageChanged() {}

Latest podspec

{
    "name": "LocalizedStringSwift",
    "version": "1.0.1",
    "summary": "Simple Localization in Swift",
    "description": "- Using NSLocalizedString more simply.",
    "homepage": "https://github.com/digitrick/LocalizedStringSwift",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": "digitrick",
    "social_media_url": "http://twitter.com/digitrick",
    "platforms": {
        "ios": "9.0"
    },
    "source": {
        "git": "https://github.com/digitrick/LocalizedStringSwift.git",
        "tag": "1.0.1"
    },
    "source_files": "LocalizedStringSwift/**/*.swift",
    "requires_arc": true,
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This