Skip to content

Commit

Permalink
Updated to swift 5, Improve tests, and added fuction to check if key …
Browse files Browse the repository at this point in the history
…exist
  • Loading branch information
andresilvagomez committed Mar 28, 2019
1 parent 3b7b5a6 commit 790ca83
Show file tree
Hide file tree
Showing 24 changed files with 178 additions and 148 deletions.
2 changes: 1 addition & 1 deletion Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
};
buildConfigurationList = 9F8D41481E29359A00D7CE9A /* Build configuration list for PBXProject "Example" */;
compatibilityVersion = "Xcode 8.0";
developmentRegion = English;
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Expand Down
2 changes: 0 additions & 2 deletions Example/Example/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Localize

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(
Expand Down Expand Up @@ -62,5 +61,4 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func applicationWillTerminate(_ application: UIApplication) {

}

}
1 change: 0 additions & 1 deletion Example/Example/CustomLocalize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Foundation
import Localize

class CustomLocalize: LocalizeCommonProtocol {

/// Custom init
override init() {
super.init()
Expand Down
1 change: 0 additions & 1 deletion Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import UIKit
import Localize

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
localize()
Expand Down
22 changes: 11 additions & 11 deletions Localize.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
9FA3BC511E318C080054CA92 /* ReadingOtherFiles.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FA3BC501E318C080054CA92 /* ReadingOtherFiles.swift */; };
9FCD8EA51E52AD3E00B5909C /* StringsChanginDefaultFileName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FCD8EA41E52AD3E00B5909C /* StringsChanginDefaultFileName.swift */; };
9FCD8EA91E53327D00B5909C /* JsonChanginFileName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FCD8EA81E53327D00B5909C /* JsonChanginFileName.swift */; };
9FE9F0C921FACAA800034A34 /* LocalizeConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9FE9F0C821FACAA800034A34 /* LocalizeConfig.swift */; };
B6EECE2A215AD8CE007D8A59 /* StringFallbackTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6EECE29215AD8CE007D8A59 /* StringFallbackTest.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -107,6 +108,7 @@
9FCD8EA41E52AD3E00B5909C /* StringsChanginDefaultFileName.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringsChanginDefaultFileName.swift; sourceTree = "<group>"; };
9FCD8EA61E53319900B5909C /* other-es.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "other-es.json"; sourceTree = "<group>"; };
9FCD8EA81E53327D00B5909C /* JsonChanginFileName.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JsonChanginFileName.swift; sourceTree = "<group>"; };
9FE9F0C821FACAA800034A34 /* LocalizeConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalizeConfig.swift; sourceTree = "<group>"; };
9FEDA663215C655000FD3BA0 /* es-CO */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "es-CO"; path = "es-CO.lproj/Other.strings"; sourceTree = "<group>"; };
B6EECE29215AD8CE007D8A59 /* StringFallbackTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringFallbackTest.swift; sourceTree = "<group>"; };
B6EECE2B215ADA55007D8A59 /* es-CO */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "es-CO"; path = "es-CO.lproj/Strings.strings"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -161,6 +163,7 @@
9F93453D1E29192300C400F7 /* LocalizeString.swift */,
9F2FC5681E427AFD00C15610 /* LocalizeStrings.swift */,
2B717464207D9AFE0070DC5F /* LocalizeUI.swift */,
9FE9F0C821FACAA800034A34 /* LocalizeConfig.swift */,
9F9345461E291A1800C400F7 /* Suporting Files */,
);
path = Source;
Expand Down Expand Up @@ -283,19 +286,19 @@
TargetAttributes = {
69F1B5731D955990000A2B87 = {
CreatedOnToolsVersion = 8.0;
LastSwiftMigration = 0900;
LastSwiftMigration = 1020;
ProvisioningStyle = Manual;
};
9F9345521E291D4E00C400F7 = {
CreatedOnToolsVersion = 8.2.1;
LastSwiftMigration = 0900;
LastSwiftMigration = 1020;
ProvisioningStyle = Manual;
};
};
};
buildConfigurationList = 69F1B56E1D955990000A2B87 /* Build configuration list for PBXProject "Localize" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
developmentRegion = en;
hasScannedForEncodings = 0;
knownRegions = (
en,
Expand Down Expand Up @@ -373,6 +376,7 @@
files = (
9F2FC5691E427AFD00C15610 /* LocalizeStrings.swift in Sources */,
9F262D911E525565002DD75A /* LocalizeCommonProtocol.swift in Sources */,
9FE9F0C921FACAA800034A34 /* LocalizeConfig.swift in Sources */,
9F93453F1E29192300C400F7 /* LocalizeExtensions.swift in Sources */,
9F2FC5671E42793B00C15610 /* LocalizeJson.swift in Sources */,
9F9345411E29192300C400F7 /* Localize.swift in Sources */,
Expand Down Expand Up @@ -589,8 +593,7 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -612,8 +615,7 @@
PRODUCT_BUNDLE_IDENTIFIER = com.andresilvagomez.localize;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand All @@ -628,8 +630,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.kekiiwaa.localize-test";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -644,8 +645,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.kekiiwaa.localize-test";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.0;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand Down
11 changes: 11 additions & 0 deletions Source/Localize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ public class Localize: NSObject {
return provider.displayNameForLanguage(language)
}

/// Determines whether a localized string exists for given key
///
/// - parameter key: localization key
/// - returns: boolean value determining whether a localized string exists for give key
public func localizeExists(forKey key: String, table: String? = nil) -> Bool {
guard let table = table else {
return key.localized != key
}
return key.localize(tableName: table) != key
}

// MARK: Config providers

/// Update provider to localize your app.
Expand Down
29 changes: 29 additions & 0 deletions Source/LocalizeConfig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
//
// LocalizeSwift.swift
// Localize
//
// Copyright © 2017 @andresilvagomez.
//

import Foundation

class LocalizeConfig: NSObject {
var provider: LocalizeType
var fileName: String
var defaultLanguage: String
var currentLanguage: String?

init(
provider: LocalizeType = .strings,
fileName: String = "strings",
defaultLanguage: String = "en",
currentLanguage: String? = nil,
bundle: Bundle = .main) {

self.provider = provider
self.fileName = fileName
self.defaultLanguage = defaultLanguage
self.currentLanguage = currentLanguage
}
}
8 changes: 8 additions & 0 deletions Source/LocalizeStatic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ extension Localize {
return Localize.shared.displayNameForLanguage(language)
}

/// Determines whether a localized string exists for given key
///
/// - parameter key: localization key
/// - returns: boolean value determining whether a localized string exists for give key
public static func localizeExists(forKey key: String, table: String? = nil) -> Bool {
return Localize.shared.localizeExists(forKey: key, table: table)
}

// MARK: Config providers

/// Update provider to localize your app.
Expand Down
12 changes: 6 additions & 6 deletions Source/LocalizeString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public extension String {
/// that prevent replace untagged values
///
/// - returns: localized key or same text
public var localized: String {
var localized: String {
return Localize.localize(key: self)
}

Expand All @@ -25,7 +25,7 @@ public extension String {
/// that prevent replace untagged values
///
/// - returns: localized key or same text
public func localize() -> String {
func localize() -> String {
return Localize.localize(key: self)
}

Expand All @@ -34,7 +34,7 @@ public extension String {
/// that prevent replace untagged values
///
/// - returns: localized key or same text
public func localize(tableName: String) -> String {
func localize(tableName: String) -> String {
return Localize.localize(key: self, tableName: tableName)
}

Expand All @@ -44,7 +44,7 @@ public extension String {
/// - parameter String: The replacement value
///
/// - returns: localized key or same text
public func localize(value: String, tableName: String? = nil) -> String {
func localize(value: String, tableName: String? = nil) -> String {
return Localize.localize(key: self, replace: value, tableName: tableName)
}

Expand All @@ -54,7 +54,7 @@ public extension String {
/// - parameter Strings: The replacement values
///
/// - returns: localized key or same text
public func localize(values: String..., tableName: String? = nil) -> String {
func localize(values: String..., tableName: String? = nil) -> String {
return Localize.localize(key: self, values: values, tableName: tableName)
}

Expand All @@ -65,7 +65,7 @@ public extension String {
/// - parameter [String:String]: The replacement dictionary
///
/// - returns: localized key or same text
public func localize(
func localize(
dictionary values: [String: String],
tableName: String? = nil) -> String {

Expand Down
2 changes: 1 addition & 1 deletion Source/LocalizeStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class LocalizeStrings: LocalizeCommonProtocol {
/// - returns: list with storaged languages code
override var availableLanguages: [String] {
var availableLanguages = bundle.localizations
if let indexOfBase = availableLanguages.index(of: "Base") {
if let indexOfBase = availableLanguages.firstIndex(of: "Base") {
availableLanguages.remove(at: indexOfBase)
}
return availableLanguages
Expand Down
37 changes: 18 additions & 19 deletions Tests/BaseTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import XCTest
import Localize

class BaseTest: XCTestCase {

override func setUp() {
super.setUp()
Localize.update(provider: .json)
Expand All @@ -19,67 +18,67 @@ class BaseTest: XCTestCase {

func testRemoveKeyAndGetDefaultForLang() {
UserDefaults.standard.removeObject(forKey: localizeStorageKey)
XCTAssertTrue(Localize.currentLanguage == Locale.preferredLanguages.first)
XCTAssertEqual(Localize.currentLanguage, Locale.preferredLanguages.first)
}

func testSameKeyWithTableName() {
let localized = "heymamiwhereareyou".localize(values: "", "", tableName: "heymamiwhereareyou")
XCTAssertTrue(localized == "heymamiwhereareyou")
XCTAssertEqual(localized, "heymamiwhereareyou")
}

func testLocalizeInAnyDictionary() {
let localized = "heymomhowareyoy".localized
XCTAssertTrue(localized == "heymomhowareyoy")
}

func testLocalizeProperty() {
let localized = "hello.world".localized
XCTAssertTrue(localized == "Hello world!")
XCTAssertEqual(localized, "heymomhowareyoy")
}

func testLocalizeKey() {
let localized = "hello.world".localize()
XCTAssertTrue(localized == "Hello world!")
XCTAssertEqual(localized, "Hello world!")
}

func testLocalizeKeyWithValue() {
let localized = "name".localize(value: "Andres")
XCTAssertTrue(localized == "Hello Andres")
XCTAssertEqual(localized, "Hello Andres")
}

func testLocalizeKeyWithValues() {
let localized = "values".localize(values: "Andres", "Software Developer")
XCTAssertTrue(localized == "Hello everyone my name is Andres and I'm Software Developer , see you soon")
XCTAssertEqual(localized, "Hello everyone my name is Andres and I'm Software Developer , see you soon")
}

func testLocalizeKeyWithDictionary() {
let localized = "username".localize(dictionary: ["username": "andresilvagomez"])
XCTAssertTrue(localized == "My username is andresilvagomez")
XCTAssertEqual(localized, "My username is andresilvagomez")
}

func testLocalizeKeyWithManyLevels() {
let localized = "level.one.two.three".localize()
XCTAssertTrue(localized == "This is a multilevel key")
XCTAssertEqual(localized, "This is a multilevel key")
}

func testLocalizeKeyWithSingleLevel() {
let localized = "the.same.lavel".localize()
XCTAssertTrue(localized == "This is a localized in the same level")
XCTAssertEqual(localized, "This is a localized in the same level")
}

func testSearchInOtherFile() {
let localized = "hello.baby".localize(tableName: "other")
XCTAssertTrue(localized == "This is a welcome, new baby is here!")
XCTAssertEqual(localized, "This is a welcome, new baby is here!")
}

func testCheckIfKeyExist() {
XCTAssertTrue(Localize.localizeExists(forKey: "hello.baby", table: "other"))
XCTAssertTrue(Localize.localizeExists(forKey: "the.same.lavel"))
XCTAssertFalse(Localize.localizeExists(forKey: "hello.baby2"))
}

func testListOfAvailableLanguages() {
let languages = Localize.availableLanguages
XCTAssertTrue(languages.count == 3)
XCTAssertEqual(languages.count, 3)
}

func testCurrentLanguage() {
let language = Localize.currentLanguage
XCTAssertTrue(language == "en")
XCTAssertEqual(language, "en")
}

}
Loading

0 comments on commit 790ca83

Please sign in to comment.