Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some more changes #13

Merged
merged 3 commits into from
Jul 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0820"
LastUpgradeVersion = "0830"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
8 changes: 4 additions & 4 deletions Example/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// Set your file name
localize.update(fileName: "lang")
// Set your default languaje.
localize.update(defaultLanguage: .french)
localize.update(defaultLanguage: "fr")
// If you want change a user language, different to default in phone use this method.
localize.update(language: .english)
localize.update(language: "en")
// If you want remove storaged languaje use
localize.resetLanguage()
// The used language
Expand All @@ -34,8 +34,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// Or you can use static methods for all.

Localize.update(fileName: "lang")
Localize.update(defaultLanguage: .french)
Localize.update(language: .english)
Localize.update(defaultLanguage: "fr")
Localize.update(language: "en")

return true
}
Expand Down
10 changes: 1 addition & 9 deletions Localize.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
objects = {

/* Begin PBXBuildFile section */
9F050EF81E2B486C009804A5 /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 9F989F421E2B34E700A19F67 /* Info.plist */; };
9F1677FA1E51DE270022F3F8 /* Strings.strings in Resources */ = {isa = PBXBuildFile; fileRef = 9F1677FC1E51DE270022F3F8 /* Strings.strings */; };
9F1678011E51F33E0022F3F8 /* StringBaseTestInSpanish.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F1678001E51F33E0022F3F8 /* StringBaseTestInSpanish.swift */; };
9F19DFC71E4D2B1D00F953E6 /* StringBaseTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F19DFC61E4D2B1D00F953E6 /* StringBaseTest.swift */; };
Expand All @@ -18,12 +17,10 @@
9F4570221E3173A4008C37D1 /* lang-es.json in Resources */ = {isa = PBXBuildFile; fileRef = 9F4570211E3173A4008C37D1 /* lang-es.json */; };
9F761AD71E305B9B00904B4B /* LocalizeStatic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F761AD61E305B9B00904B4B /* LocalizeStatic.swift */; };
9F761AEC1E3063E400904B4B /* lang-en.json in Resources */ = {isa = PBXBuildFile; fileRef = 9F761AEB1E3063E400904B4B /* lang-en.json */; };
9F93453E1E29192300C400F7 /* Languages.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F9345391E29192300C400F7 /* Languages.swift */; };
9F93453F1E29192300C400F7 /* LocalizeExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F93453A1E29192300C400F7 /* LocalizeExtensions.swift */; };
9F9345401E29192300C400F7 /* LocalizeUIElements.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F93453B1E29192300C400F7 /* LocalizeUIElements.swift */; };
9F9345411E29192300C400F7 /* Localize.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F93453C1E29192300C400F7 /* Localize.swift */; };
9F9345421E29192300C400F7 /* LocalizeString.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F93453D1E29192300C400F7 /* LocalizeString.swift */; };
9F93454A1E291A2700C400F7 /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 9F9345481E291A2700C400F7 /* Info.plist */; };
9F93454B1E291AF800C400F7 /* Localize.h in Headers */ = {isa = PBXBuildFile; fileRef = 9F9345471E291A2700C400F7 /* Localize.h */; settings = {ATTRIBUTES = (Public, ); }; };
9F9345581E291D4E00C400F7 /* Localize.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 69F1B5741D955990000A2B87 /* Localize.framework */; };
9F989F4C1E2B3B8200A19F67 /* BaseTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F989F411E2B34E700A19F67 /* BaseTest.swift */; };
Expand Down Expand Up @@ -62,7 +59,6 @@
9F4570211E3173A4008C37D1 /* lang-es.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "lang-es.json"; sourceTree = "<group>"; };
9F761AD61E305B9B00904B4B /* LocalizeStatic.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LocalizeStatic.swift; sourceTree = "<group>"; };
9F761AEB1E3063E400904B4B /* lang-en.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "lang-en.json"; sourceTree = "<group>"; };
9F9345391E29192300C400F7 /* Languages.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Languages.swift; sourceTree = "<group>"; };
9F93453A1E29192300C400F7 /* LocalizeExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LocalizeExtensions.swift; sourceTree = "<group>"; };
9F93453B1E29192300C400F7 /* LocalizeUIElements.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LocalizeUIElements.swift; sourceTree = "<group>"; };
9F93453C1E29192300C400F7 /* Localize.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Localize.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -125,7 +121,6 @@
9F9345381E29192300C400F7 /* Source */ = {
isa = PBXGroup;
children = (
9F9345391E29192300C400F7 /* Languages.swift */,
9F93453C1E29192300C400F7 /* Localize.swift */,
9F761AD61E305B9B00904B4B /* LocalizeStatic.swift */,
9F93453D1E29192300C400F7 /* LocalizeString.swift */,
Expand Down Expand Up @@ -238,7 +233,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0820;
LastUpgradeCheck = 0820;
LastUpgradeCheck = 0830;
ORGANIZATIONNAME = Mobility;
TargetAttributes = {
69F1B5731D955990000A2B87 = {
Expand Down Expand Up @@ -279,7 +274,6 @@
9FA3BC4F1E318BED0054CA92 /* file-de.json in Resources */,
9FCD8EA71E53319900B5909C /* other-es.json in Resources */,
9F761AEC1E3063E400904B4B /* lang-en.json in Resources */,
9F93454A1E291A2700C400F7 /* Info.plist in Resources */,
9F4570221E3173A4008C37D1 /* lang-es.json in Resources */,
9F1677FA1E51DE270022F3F8 /* Strings.strings in Resources */,
9FA3BC4D1E318BDC0054CA92 /* some-it.json in Resources */,
Expand All @@ -292,7 +286,6 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
9F050EF81E2B486C009804A5 /* Info.plist in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -311,7 +304,6 @@
9F9345411E29192300C400F7 /* Localize.swift in Sources */,
9F9345421E29192300C400F7 /* LocalizeString.swift in Sources */,
9F761AD71E305B9B00904B4B /* LocalizeStatic.swift in Sources */,
9F93453E1E29192300C400F7 /* Languages.swift in Sources */,
9F2FC5651E4275A300C15610 /* LocalizeProtocol.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0820"
LastUpgradeVersion = "0830"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0820"
LastUpgradeVersion = "0830"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
72 changes: 0 additions & 72 deletions Source/Languages.swift

This file was deleted.

11 changes: 3 additions & 8 deletions Source/Localize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,11 @@ public class Localize: NSObject {
return self.provider.language()
}

/// Update default languaje, this store a language key and retrive the next time
public func update(language:Languages) -> Void {
/// Update default language, this stores a language key which can be retrieved the next time
public func update(language: String) -> Void {
self.provider.update(language: language)
}

/// Update default languaje, this store a language key and retrive the next time
public func update(language string:String) -> Void {
self.provider.update(language: string)
}

/// Update base file name, searched in path.
public func update(fileName:String) {
self.provider.update(fileName: fileName)
Expand All @@ -127,7 +122,7 @@ public class Localize: NSObject {
}

/// Update default language
public func update(defaultLanguage: Languages) {
public func update(defaultLanguage: String) {
self.provider.update(defaultLanguage: defaultLanguage)
}

Expand Down
30 changes: 10 additions & 20 deletions Source/LocalizeCommonProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ class LocalizeCommonProtocol: NSObject {

/// Default language, if this can't find a key in your current language
/// Try read key in default language
public var defaultLanguage: Languages = .english
public var defaultLanguage: String = "en"

/// Determinate if your interface localization is based on LocalizeInterface
public var localizableInterface: LocalizeInterface = .boot

/// Storaged language or default language in device
public var currentLanguage: Languages {
public var currentLanguage: String {
get {
return Languages(rawValue: self.language())!
return self.language()
}
}

Expand Down Expand Up @@ -65,24 +65,16 @@ class LocalizeCommonProtocol: NSObject {
// MARK: Public methods


/// Update default languaje, this store a language key and retrive the next time
public func update(language:Languages) -> Void {
/// Update default language, this stores a language key which can be retrieved the next time
public func update(language: String) -> Void {
let defaults = UserDefaults.standard
defaults.setValue(language.rawValue, forKey: LocalizeStorageKey)
defaults.setValue(language, forKey: LocalizeStorageKey)
defaults.synchronize()
NotificationCenter.default.post(name: Notification.Name(rawValue: LocalizeChangeNotification), object: nil)
}

/// Update default languaje, this store a language key and retrive the next time
public func update(language string:String) -> Void {
guard let language = Languages(rawValue: string) else {
return
}
return self.update(language: language)
}

/// Update default language
public func update(defaultLanguage: Languages) {
public func update(defaultLanguage: String) {
self.defaultLanguage = defaultLanguage
}

Expand All @@ -97,7 +89,7 @@ class LocalizeCommonProtocol: NSObject {
///
/// - return: String form language code in current user language
public func displayNameForLanguage(_ language: String) -> String {
let locale : NSLocale = NSLocale(localeIdentifier: self.currentLanguage.rawValue)
let locale : NSLocale = NSLocale(localeIdentifier: self.currentLanguage)
if let name = locale.displayName(forKey: NSLocale.Key.identifier, value: language) {
return name.capitalized
}
Expand All @@ -118,7 +110,7 @@ class LocalizeCommonProtocol: NSObject {
if let lang = defaults.string(forKey: LocalizeStorageKey) {
return lang
}
return Locale.preferredLanguages[0].components(separatedBy: "-")[0]
return Locale.preferredLanguages[0]
}

/// Update base file name, searched in path.
Expand Down Expand Up @@ -151,9 +143,7 @@ class LocalizeCommonProtocol: NSObject {
/// - returns: localized key or same text
public func localize(key:String, replace:String, tableName:String? = nil) -> String {
let string = self.localize(key: key, tableName:tableName)
if string == key {
return key
}

return string.replacingOccurrences(of: "%", with: replace)
}

Expand Down
15 changes: 8 additions & 7 deletions Source/LocalizeJson.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class LocalizeJson: LocalizeCommonProtocol, LocalizeProtocol {
/// As long as the default language is the same as the current one.
private func readJSON(tableName:String? = nil) -> NSDictionary? {
let tableName = tableName ?? self.fileName
let lang = self.currentLanguage.rawValue
let lang = self.currentLanguage
var json = self.readJSON(named: "\(tableName)-\(lang)")

if json == nil && lang != self.defaultLanguage.rawValue {
if json == nil && lang != self.defaultLanguage {
json = self.readDefaultJSON()
}

Expand All @@ -37,7 +37,7 @@ class LocalizeJson: LocalizeCommonProtocol, LocalizeProtocol {
/// - returns: json or nil value.
private func readDefaultJSON(tableName:String? = nil) -> NSDictionary? {
let tableName = tableName ?? self.fileName
return self.readJSON(named: "\(tableName)-\(self.defaultLanguage.rawValue)")
return self.readJSON(named: "\(tableName)-\(self.defaultLanguage)")
}

/// This method has path where file is
Expand Down Expand Up @@ -124,16 +124,17 @@ class LocalizeJson: LocalizeCommonProtocol, LocalizeProtocol {
// MARK: Config methods


/// Show all aviable languajes whit criteria name
/// Show all aviable languages with criteria name
///
/// - returns: list with storaged languages code
public func availableLanguages() -> [String] {
var languages : [String] = []
for language in iterateEnum(Languages.self) {
let name = "\(self.fileName)-\(language.rawValue)"

for localeId in NSLocale.availableLocaleIdentifiers {
let name = "\(self.fileName)-\(localeId)"
let path = self.bundle().path(forResource: name, ofType: "json")
if path != nil {
languages.append(language.rawValue)
languages.append(localeId)
}
}
return languages
Expand Down
11 changes: 4 additions & 7 deletions Source/LocalizeProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protocol LocalizeProtocol {

/// Default language, if this can't find a key in your current language
/// Try read key in default language
var defaultLanguage: Languages {get set}
var defaultLanguage: String {get set}


/// MARK: Localize Methos
Expand Down Expand Up @@ -61,11 +61,8 @@ protocol LocalizeProtocol {
/// - returns: current used language
func language() -> String

/// Update default languaje, this store a language key and retrive the next time
func update(language:Languages) -> Void

/// Update default languaje, this store a language key and retrive the next time
func update(language string:String) -> Void
/// Update default language, this stores a language key which can be retrieved the next time
func update(language: String) -> Void

/// Update base file name, searched in path.
func update(fileName:String)
Expand All @@ -74,7 +71,7 @@ protocol LocalizeProtocol {
func update(bundle: Bundle)

/// Update default language
func update(defaultLanguage: Languages)
func update(defaultLanguage: String)

/// This remove the language key storaged.
func resetLanguage() -> Void
Expand Down
11 changes: 3 additions & 8 deletions Source/LocalizeStatic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,11 @@ extension Localize {
return Localize.shared.language()
}

/// Update default languaje, this store a language key and retrive the next time
public static func update(language:Languages) {
/// Update default language, this stores a language key which can be retrieved the next time
public static func update(language: String) {
return Localize.shared.update(language: language)
}

/// Update default languaje, this store a language key and retrive the next time
public static func update(language string:String) {
return Localize.shared.update(language: string)
}

/// Update base file name, searched in path.
public static func update(fileName:String) {
return Localize.shared.update(fileName: fileName)
Expand All @@ -77,7 +72,7 @@ extension Localize {
}

/// Update default language
public static func update(defaultLanguage: Languages) {
public static func update(defaultLanguage: String) {
return Localize.shared.update(defaultLanguage: defaultLanguage)
}

Expand Down
Loading