forked from zhiphe/Potatso-iOS
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Localized.swift
129 lines (111 loc) · 4.27 KB
/
Localized.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//
// Strings.swift
// Potatso
//
// Created by LEI on 1/23/16.
// Copyright © 2016 TouchingApp. All rights reserved.
//
import Foundation
/// Internal current language key
let LCLCurrentLanguageKey = "LCLCurrentLanguageKey"
/// Default language. English. If English is unavailable defaults to base localization.
let LCLDefaultLanguage = "en"
/// Name for language change notification
public let LCLLanguageChangeNotification = "LCLLanguageChangeNotification"
public extension String {
/**
Swift 2 friendly localization syntax, replaces NSLocalizedString
- Returns: The localized string.
*/
public func localized() -> String {
if let path = NSBundle.mainBundle().pathForResource(Localize.currentLanguage(), ofType: "lproj"), bundle = NSBundle(path: path) {
return bundle.localizedStringForKey(self, value: nil, table: nil)
}else if let path = NSBundle.mainBundle().pathForResource("Base", ofType: "lproj"), bundle = NSBundle(path: path) {
return bundle.localizedStringForKey(self, value: nil, table: nil)
}
return self
}
/**
Swift 2 friendly localization syntax with format arguments, replaces String(format:NSLocalizedString)
- Returns: The formatted localized string with arguments.
*/
public func localizedFormat(arguments: CVarArgType...) -> String {
return String(format: localized(), arguments: arguments)
}
/**
Swift 2 friendly plural localization syntax with a format argument
- parameter argument: Argument to determine pluralisation
- returns: Pluralized localized string.
*/
public func localizedPlural(argument: CVarArgType) -> String {
return NSString.localizedStringWithFormat(localized(), argument) as String
}
}
// MARK: Language Setting Functions
public class Localize: NSObject {
/**
List available languages
- Returns: Array of available languages.
*/
public class func availableLanguages() -> [String] {
return NSBundle.mainBundle().localizations
}
/**
Current language
- Returns: The current language. String.
*/
public class func currentLanguage() -> String {
if let currentLanguage = NSUserDefaults.standardUserDefaults().objectForKey(LCLCurrentLanguageKey) as? String {
return currentLanguage
}
return defaultLanguage()
}
/**
Change the current language
- Parameter language: Desired language.
*/
public class func setCurrentLanguage(language: String) {
let selectedLanguage = availableLanguages().contains(language) ? language : defaultLanguage()
if (selectedLanguage != currentLanguage()){
NSUserDefaults.standardUserDefaults().setObject(selectedLanguage, forKey: LCLCurrentLanguageKey)
NSUserDefaults.standardUserDefaults().synchronize()
NSNotificationCenter.defaultCenter().postNotificationName(LCLLanguageChangeNotification, object: nil)
}
}
/**
Default language
- Returns: The app's default language. String.
*/
public class func defaultLanguage() -> String {
var defaultLanguage: String = String()
guard let preferredLanguage = NSBundle.mainBundle().preferredLocalizations.first else {
return LCLDefaultLanguage
}
let availableLanguages: [String] = self.availableLanguages()
if (availableLanguages.contains(preferredLanguage)) {
defaultLanguage = preferredLanguage
}
else {
defaultLanguage = LCLDefaultLanguage
}
return defaultLanguage
}
/**
Resets the current language to the default
*/
public class func resetCurrentLanguageToDefault() {
setCurrentLanguage(self.defaultLanguage())
}
/**
Get the current language's display name for a language.
- Parameter language: Desired language.
- Returns: The localized string.
*/
public class func displayNameForLanguage(language: String) -> String {
let locale : NSLocale = NSLocale(localeIdentifier: currentLanguage())
if let displayName = locale.displayNameForKey(NSLocaleLanguageCode, value: language) {
return displayName
}
return String()
}
}