From 22943d30a999840f9eea6a7eaacf6952f88fe684 Mon Sep 17 00:00:00 2001 From: Tomasz Szulc Date: Sat, 1 Aug 2015 00:00:24 +0200 Subject: [PATCH] Fix test environment to run old tests --- Swifternalization.xcodeproj/project.pbxproj | 26 +----------- Swifternalization/JSONFileLoader.swift | 8 ++-- Swifternalization/LengthVariation.swift | 4 +- .../LoadedTranslationsProcessor.swift | 4 +- Swifternalization/Swifternalization.swift | 21 +++++++--- .../Base.lproj/Expressions.strings | 12 ------ .../Base.lproj/Localizable.strings | 21 ---------- .../JSONFileLoaderTests.swift | 16 +++---- SwifternalizationTests/base.json | 42 +++++++++---------- SwifternalizationTests/expressions.json | 6 +-- 10 files changed, 57 insertions(+), 103 deletions(-) delete mode 100644 SwifternalizationTests/Base.lproj/Expressions.strings delete mode 100644 SwifternalizationTests/Base.lproj/Localizable.strings diff --git a/Swifternalization.xcodeproj/project.pbxproj b/Swifternalization.xcodeproj/project.pbxproj index ede3edb..da6aca3 100644 --- a/Swifternalization.xcodeproj/project.pbxproj +++ b/Swifternalization.xcodeproj/project.pbxproj @@ -21,7 +21,6 @@ 6D5004541B3EF91600A54B36 /* Swifternalization.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6D5004481B3EF91600A54B36 /* Swifternalization.framework */; }; 6D50045B1B3EF91600A54B36 /* SwifternalizationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D50045A1B3EF91600A54B36 /* SwifternalizationTests.swift */; }; 6D5004661B3EF92600A54B36 /* Swifternalization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D5004641B3EF92600A54B36 /* Swifternalization.swift */; }; - 6D5004921B3EFF6D00A54B36 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 6D5004941B3EFF6D00A54B36 /* Localizable.strings */; }; 6D5004961B3EFFC100A54B36 /* Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 6D5004591B3EF91600A54B36 /* Info.plist */; }; 6D5BA5F01B651796000D7E49 /* TranslationsLoaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D5BA5EF1B651796000D7E49 /* TranslationsLoaderTests.swift */; }; 6D5BA5F11B6517A6000D7E49 /* TranslationsLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC731B5EBDA600A1220F /* TranslationsLoader.swift */; }; @@ -78,7 +77,6 @@ 6DB3CC901B5EC29600A1220F /* ExpressionPatternType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC8F1B5EC29600A1220F /* ExpressionPatternType.swift */; }; 6DB3CC911B5EC29E00A1220F /* ExpressionPatternType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC8F1B5EC29600A1220F /* ExpressionPatternType.swift */; }; 6DBB6C521B401B8A002F39A3 /* Swifternalization.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D5004641B3EF92600A54B36 /* Swifternalization.swift */; }; - 6DBB6C591B4026B8002F39A3 /* Expressions.strings in Resources */ = {isa = PBXBuildFile; fileRef = 6DBB6C5B1B4026B8002F39A3 /* Expressions.strings */; }; 6DBB6C691B4040F0002F39A3 /* InternalPattern.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DBB6C681B4040F0002F39A3 /* InternalPattern.swift */; }; 6DBB6C6A1B40412D002F39A3 /* InternalPattern.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DBB6C681B4040F0002F39A3 /* InternalPattern.swift */; }; 6DBB6C871B40718F002F39A3 /* Expressions.strings in Resources */ = {isa = PBXBuildFile; fileRef = 6DBB6C891B40718F002F39A3 /* Expressions.strings */; }; @@ -121,7 +119,6 @@ 6D5004591B3EF91600A54B36 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 6D50045A1B3EF91600A54B36 /* SwifternalizationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwifternalizationTests.swift; sourceTree = ""; }; 6D5004641B3EF92600A54B36 /* Swifternalization.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Swifternalization.swift; sourceTree = ""; }; - 6D5004931B3EFF6D00A54B36 /* Base */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = Base; path = Base.lproj/Localizable.strings; sourceTree = ""; }; 6D5BA5EF1B651796000D7E49 /* TranslationsLoaderTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TranslationsLoaderTests.swift; sourceTree = ""; }; 6D5BA5FA1B65253B000D7E49 /* SharedExpressionsProcessor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SharedExpressionsProcessor.swift; sourceTree = ""; }; 6D5BA5FF1B6526F0000D7E49 /* SharedExpression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SharedExpression.swift; path = ../SharedExpression.swift; sourceTree = ""; }; @@ -163,7 +160,6 @@ 6DB3CC6A1B5EBDA600A1220F /* LengthVariation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LengthVariation.swift; sourceTree = ""; }; 6DB3CC731B5EBDA600A1220F /* TranslationsLoader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TranslationsLoader.swift; sourceTree = ""; }; 6DB3CC8F1B5EC29600A1220F /* ExpressionPatternType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExpressionPatternType.swift; sourceTree = ""; }; - 6DBB6C5A1B4026B8002F39A3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = Base; path = Base.lproj/Expressions.strings; sourceTree = ""; }; 6DBB6C681B4040F0002F39A3 /* InternalPattern.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InternalPattern.swift; sourceTree = ""; }; 6DBB6C881B40718F002F39A3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = Base; path = Base.lproj/Expressions.strings; sourceTree = ""; }; 6DBB6C8A1B407190002F39A3 /* pl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = pl; path = pl.lproj/Expressions.strings; sourceTree = ""; }; @@ -283,8 +279,6 @@ 6D6282931B3F052B00E65FCD /* TranslatablePairTests.swift */, 6D140F451B56D04300359143 /* Helpers */, 6D5004581B3EF91600A54B36 /* Supporting Files */, - 6D5004941B3EFF6D00A54B36 /* Localizable.strings */, - 6DBB6C5B1B4026B8002F39A3 /* Expressions.strings */, 6DD3B93D1B5ED35600C79EAC /* Localizable Files */, ); path = SwifternalizationTests; @@ -360,8 +354,8 @@ 6DD3B93D1B5ED35600C79EAC /* Localizable Files */ = { isa = PBXGroup; children = ( - 6DD3B9371B5ED35200C79EAC /* base.json */, 6DD3B9381B5ED35200C79EAC /* expressions.json */, + 6DD3B9371B5ED35200C79EAC /* base.json */, 6DD3B9391B5ED35200C79EAC /* pl.json */, ); name = "Localizable Files"; @@ -506,8 +500,6 @@ 6DD3B93A1B5ED35200C79EAC /* base.json in Resources */, 6D5004961B3EFFC100A54B36 /* Info.plist in Resources */, 6DD3B93C1B5ED35200C79EAC /* pl.json in Resources */, - 6DBB6C591B4026B8002F39A3 /* Expressions.strings in Resources */, - 6D5004921B3EFF6D00A54B36 /* Localizable.strings in Resources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -634,14 +626,6 @@ /* End PBXTargetDependency section */ /* Begin PBXVariantGroup section */ - 6D5004941B3EFF6D00A54B36 /* Localizable.strings */ = { - isa = PBXVariantGroup; - children = ( - 6D5004931B3EFF6D00A54B36 /* Base */, - ); - name = Localizable.strings; - sourceTree = ""; - }; 6D6283491B3F622A00E65FCD /* Localizable.strings */ = { isa = PBXVariantGroup; children = ( @@ -651,14 +635,6 @@ name = Localizable.strings; sourceTree = ""; }; - 6DBB6C5B1B4026B8002F39A3 /* Expressions.strings */ = { - isa = PBXVariantGroup; - children = ( - 6DBB6C5A1B4026B8002F39A3 /* Base */, - ); - name = Expressions.strings; - sourceTree = ""; - }; 6DBB6C891B40718F002F39A3 /* Expressions.strings */ = { isa = PBXVariantGroup; children = ( diff --git a/Swifternalization/JSONFileLoader.swift b/Swifternalization/JSONFileLoader.swift index c310847..bdfac40 100644 --- a/Swifternalization/JSONFileLoader.swift +++ b/Swifternalization/JSONFileLoader.swift @@ -17,8 +17,8 @@ final class JSONFileLoader { :param: bundle A bundle when file is located. :returns: Returns json of file or nil if cannot load a file. */ - class func loadTranslations(countryCode: CountryCode, bundle: NSBundle = NSBundle.mainBundle()) -> JSONDictionary? { - return self.load(countryCode, bundle: bundle) + class func loadTranslations(countryCode: CountryCode, bundle: NSBundle = NSBundle.mainBundle()) -> JSONDictionary { + return self.load(countryCode, bundle: bundle) ?? [:] } /** @@ -28,8 +28,8 @@ final class JSONFileLoader { :param: bundle A bundle when file is located. :returns: dictionary with expressions or nil. */ - class func loadExpressions(countryCode: CountryCode, bundle: NSBundle = NSBundle.mainBundle()) -> Dictionary? { - return self.load("expressions", bundle: bundle)?[countryCode] as? Dictionary + class func loadExpressions(countryCode: CountryCode, bundle: NSBundle = NSBundle.mainBundle()) -> Dictionary { + return self.load("expressions", bundle: bundle)?[countryCode] as? Dictionary ?? [:] } /** diff --git a/Swifternalization/LengthVariation.swift b/Swifternalization/LengthVariation.swift index 09dd4b3..9fa797e 100644 --- a/Swifternalization/LengthVariation.swift +++ b/Swifternalization/LengthVariation.swift @@ -4,8 +4,8 @@ import Foundation Length variation representation. */ struct LengthVariation { - /// Length - width of a screen. - let length: Int + /// width of a screen. + let width: Int /// localized string. let value: String diff --git a/Swifternalization/LoadedTranslationsProcessor.swift b/Swifternalization/LoadedTranslationsProcessor.swift index 8111d2c..bf423cc 100644 --- a/Swifternalization/LoadedTranslationsProcessor.swift +++ b/Swifternalization/LoadedTranslationsProcessor.swift @@ -68,7 +68,7 @@ class LoadedTranslationsProcessor { // Translation contains length expressions like @100, @200, etc. var lengthVariations = [LengthVariation]() for (key, value) in $0.content as! Dictionary { - lengthVariations.append(LengthVariation(length: self.parseNumberFromLengthVariation(key), value: value)) + lengthVariations.append(LengthVariation(width: self.parseNumberFromLengthVariation(key), value: value)) } return Translation(key: $0.key, expressions: [Expression(pattern: $0.key, localizedValue: lengthVariations.last!.value, lengthVariations: lengthVariations)]) @@ -85,7 +85,7 @@ class LoadedTranslationsProcessor { if value is Dictionary { var lengthVariations = [LengthVariation]() for (lvKey, lvValue) in value as! Dictionary { - lengthVariations.append(LengthVariation(length: self.parseNumberFromLengthVariation(lvKey), value: lvValue)) + lengthVariations.append(LengthVariation(width: self.parseNumberFromLengthVariation(lvKey), value: lvValue)) } expressions.append(Expression(pattern: pattern, localizedValue: lengthVariations.last!.value, lengthVariations: lengthVariations)) } else if value is String { diff --git a/Swifternalization/Swifternalization.swift b/Swifternalization/Swifternalization.swift index 31b552b..53886a2 100644 --- a/Swifternalization/Swifternalization.swift +++ b/Swifternalization/Swifternalization.swift @@ -31,7 +31,18 @@ public class Swifternalization { } else if matchingExpression.lengthVariations.count == 0 { return matchingExpression.localizedValue } else if matchingExpression.lengthVariations.count > 0 { - /// GET PROPER VARIANT + let sortedVariations = matchingExpression.lengthVariations.sorted({$0.width < $1.width}) + + var selectedValue = matchingExpression.localizedValue + for variation in sortedVariations { + if variation.width <= fittingWidth { + selectedValue = variation.value + } else { + break + } + } + + return selectedValue } } } @@ -53,12 +64,12 @@ public class Swifternalization { let base = "base" let language = getPreferredLanguage(bundle) - let baseExpressions = SharedExpressionsLoader.loadExpressions(JSONFileLoader.loadExpressions(base, bundle: bundle) ?? [:]) - let languageExpressions = SharedExpressionsLoader.loadExpressions(JSONFileLoader.loadExpressions(language, bundle: bundle) ?? [:]) + let baseExpressions = SharedExpressionsLoader.loadExpressions(JSONFileLoader.loadExpressions(base, bundle: bundle)) + let languageExpressions = SharedExpressionsLoader.loadExpressions(JSONFileLoader.loadExpressions(language, bundle: bundle)) let expressions = SharedExpressionsProcessor.processSharedExpression(language, preferedLanguageExpressions: languageExpressions, baseLanguageExpressions: baseExpressions) - let baseTranslations = TranslationsLoader.loadTranslations(JSONFileLoader.loadTranslations(base, bundle: bundle) ?? [:]) - let languageTranslations = TranslationsLoader.loadTranslations(JSONFileLoader.loadTranslations(language, bundle: bundle) ?? [:]) + let baseTranslations = TranslationsLoader.loadTranslations(JSONFileLoader.loadTranslations(base, bundle: bundle)) + let languageTranslations = TranslationsLoader.loadTranslations(JSONFileLoader.loadTranslations(language, bundle: bundle)) translations = LoadedTranslationsProcessor.processTranslations(baseTranslations, preferedLanguageTranslations: languageTranslations, sharedExpressions: expressions) } diff --git a/SwifternalizationTests/Base.lproj/Expressions.strings b/SwifternalizationTests/Base.lproj/Expressions.strings deleted file mode 100644 index 1d5c4ed..0000000 --- a/SwifternalizationTests/Base.lproj/Expressions.strings +++ /dev/null @@ -1,12 +0,0 @@ -/* - Expressions.strings - Swifternalization - - Created by Tomasz Szulc on 28/06/15. - Copyright (c) 2015 Tomasz Szulc. All rights reserved. -*/ - -"ten" = "ie:x=10"; -">20" = "ie:x>20"; - -"custom-pl-few" = "exp:(.*(?=1).[0-9]$)|(^[05-9]$)|(.*(?!1).[0156789])"; \ No newline at end of file diff --git a/SwifternalizationTests/Base.lproj/Localizable.strings b/SwifternalizationTests/Base.lproj/Localizable.strings deleted file mode 100644 index acb109d..0000000 --- a/SwifternalizationTests/Base.lproj/Localizable.strings +++ /dev/null @@ -1,21 +0,0 @@ -"welcome-key" = "welcome"; - -"only-in-base" = "this is from base"; - -"cars{one}" = "one car"; -"cars{ie:x>=2}" = "%d cars"; -"cars{ie:x<=-2}" = "minus %d cars"; - -// Polish language -"pl-cars{one}" = "jeden samochód"; -"pl-cars{iex:2<=x<=4}" = "%d samochody"; -"pl-cars{iex:-4<=x<=-2}" = "-2 - -4 samochody"; - -// Polish tricky language support -"pl-police-cars{ie:x=1}" = "1 samochód policyjny"; -"pl-police-cars{exp:(((?!1).[2-4]{1})$)|(^[2-4]$)}" = "%d samochody policyjne"; -"pl-police-cars{custom-pl-few}" = "%d samochodów policyjnych"; - -// Shared expression test - custom -"things{ten}" = "10 things"; -"things{>20}" = ">20 things"; \ No newline at end of file diff --git a/SwifternalizationTests/JSONFileLoaderTests.swift b/SwifternalizationTests/JSONFileLoaderTests.swift index fbd61b9..91fa82e 100644 --- a/SwifternalizationTests/JSONFileLoaderTests.swift +++ b/SwifternalizationTests/JSONFileLoaderTests.swift @@ -12,36 +12,36 @@ import XCTest class JSONFileLoaderTests: XCTestCase { // Expressions - func loadExpressions(cc: CountryCode) -> Dictionary? { + func loadExpressions(cc: CountryCode) -> Dictionary { return JSONFileLoader.loadExpressions(cc, bundle: NSBundle.testBundle()) } func testShouldLoadBaseExpressions() { - XCTAssertNotNil(loadExpressions("base"), "") + XCTAssertFalse(loadExpressions("base").isEmpty, "") } func testShouldLoadPLExpressions() { - XCTAssertNotNil(loadExpressions("pl"), "") + XCTAssertFalse(loadExpressions("pl").isEmpty, "") } func testShouldNotLoadDEExpressions() { - XCTAssertNil(loadExpressions("de"), "") + XCTAssertTrue(loadExpressions("de").isEmpty, "") } // Translations - func loadTranslations(cc: CountryCode) -> JSONDictionary? { + func loadTranslations(cc: CountryCode) -> JSONDictionary { return JSONFileLoader.loadTranslations(cc, bundle: NSBundle.testBundle()) } func testShouldLoadBaseTranslations() { - XCTAssertNotNil(loadTranslations("base"), "") + XCTAssertFalse(loadTranslations("base").isEmpty, "") } func testShouldLoadPLTranslations() { - XCTAssertNotNil(loadTranslations("pl"), "") + XCTAssertFalse(loadTranslations("pl").isEmpty, "") } func testShouldNotLoadDETranslations() { - XCTAssertNil(loadTranslations("de"), "") + XCTAssertTrue(loadTranslations("de").isEmpty, "") } } diff --git a/SwifternalizationTests/base.json b/SwifternalizationTests/base.json index e0b4524..542262c 100644 --- a/SwifternalizationTests/base.json +++ b/SwifternalizationTests/base.json @@ -1,28 +1,28 @@ { - "welcome": "welcome", + "welcome-key": "welcome", + + "only-in-base": "this is from base", "cars": { - "one": "1 car", - "ie:x=2": "2 cars", - "more": "%d cars" + "one": "one car", + "ie:x>=2": "%d cars", + "ie:x<=-2": "minus %d cars" }, - - "forgot-password": { - "@100": "Forgot Password? Help.", - "@200": "Forgot Password? Get password Help.", - "@300": "Forgotten Your Password? Get password Help." + + "pl-cars": { + "one": "jeden samochód", + "iex:2<=x<=4": "%d samochody", + "iex:-4<=x<=-2": "-2 - -4 samochody" }, - - "car-sentence": { - "one": { - "@100": "one car", - "@200": "just one car", - "@300": "you've got just one car" - }, - - "more": { - "@100": "%d cars", - "@300": "you've got %d cars" - } + + "pl-police-cars": { + "ie:x=1": "1 samochód policyjny", + "exp:(((?!1).[2-4]{1})$)|(^[2-4]$)": "%d samochody policyjne", + "custom-pl-few": "%d samochodów policyjnych" + }, + + "things": { + "ten": "10 things", + ">20": ">20 things" } } diff --git a/SwifternalizationTests/expressions.json b/SwifternalizationTests/expressions.json index 953b595..6b20b7b 100644 --- a/SwifternalizationTests/expressions.json +++ b/SwifternalizationTests/expressions.json @@ -1,8 +1,8 @@ { "base": { - "one": "ie:x=1", - "two": "ie:x=2", - "more": "exp:(^[^1])|(^\\d{2,})" + "ten": "ie:x=10", + ">20": "ie:x>20", + "custom-pl-few": "exp:(.*(?=1).[0-9]$)|(^[05-9]$)|(.*(?!1).[0156789])" }, "pl": {