From 3208b881158a2f0654a20e95b3fda4688fc422bc Mon Sep 17 00:00:00 2001 From: Tomasz Szulc Date: Sun, 26 Jul 2015 17:56:00 +0200 Subject: [PATCH] Add SharedExpressionsLoader --- Swifternalization.xcodeproj/project.pbxproj | 62 ++++++++--- .../SharedBaseExpression.swift | 8 +- .../SharedPolishExpression.swift | 4 +- .../SharedExpression.swift | 28 ++--- ...der.swift => SharedExpressionLoader.swift} | 8 +- .../SharedExpressionsConfigurator.swift | 4 +- .../SharedExpressionsProcessor.swift | 104 ++++++++++++++++++ Swifternalization/Swifternalization.swift | 2 +- Swifternalization/TranslationsProcessor.swift | 13 +++ .../SharedBaseExpressionTests.swift | 8 +- ...ift => SharedExpressionsLoaderTests.swift} | 8 +- .../SharedExpressionsProcessorTests.swift | 22 ++++ .../SharedPolishExpressionTests.swift | 4 +- SwifternalizationTests/expressions.json | 4 +- 14 files changed, 223 insertions(+), 56 deletions(-) rename Swifternalization/{Shared Expressions => }/SharedExpression.swift (56%) rename Swifternalization/{ExpressionsLoader.swift => SharedExpressionLoader.swift} (73%) create mode 100644 Swifternalization/SharedExpressionsProcessor.swift create mode 100644 Swifternalization/TranslationsProcessor.swift rename SwifternalizationTests/{ExpressionsLoaderTests.swift => SharedExpressionsLoaderTests.swift} (56%) create mode 100644 SwifternalizationTests/SharedExpressionsProcessorTests.swift diff --git a/Swifternalization.xcodeproj/project.pbxproj b/Swifternalization.xcodeproj/project.pbxproj index b3a4648..ddf90e5 100644 --- a/Swifternalization.xcodeproj/project.pbxproj +++ b/Swifternalization.xcodeproj/project.pbxproj @@ -25,6 +25,12 @@ 6D5BA5F71B651825000D7E49 /* ProcessableLengthVariationExpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC6E1B5EBDA600A1220F /* ProcessableLengthVariationExpression.swift */; }; 6D5BA5F81B65182D000D7E49 /* ProcessableTranslationLengthVariationExpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC701B5EBDA600A1220F /* ProcessableTranslationLengthVariationExpression.swift */; }; 6D5BA5F91B651831000D7E49 /* ProcessableTranslationExpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC6F1B5EBDA600A1220F /* ProcessableTranslationExpression.swift */; }; + 6D5BA5FB1B65253B000D7E49 /* SharedExpressionsProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D5BA5FA1B65253B000D7E49 /* SharedExpressionsProcessor.swift */; }; + 6D5BA5FE1B6525F9000D7E49 /* TranslationsProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D5BA5FD1B6525F9000D7E49 /* TranslationsProcessor.swift */; }; + 6D5BA6001B6526F0000D7E49 /* SharedExpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D5BA5FF1B6526F0000D7E49 /* SharedExpression.swift */; }; + 6D5BA6011B65271A000D7E49 /* SharedExpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D5BA5FF1B6526F0000D7E49 /* SharedExpression.swift */; }; + 6D5BA6041B6537D5000D7E49 /* SharedExpressionsProcessorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D5BA6031B6537D5000D7E49 /* SharedExpressionsProcessorTests.swift */; }; + 6D5BA6051B653935000D7E49 /* SharedExpressionsProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D5BA5FA1B65253B000D7E49 /* SharedExpressionsProcessor.swift */; }; 6D6282921B3F04C800E65FCD /* Expression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D6282911B3F04C800E65FCD /* Expression.swift */; }; 6D6282941B3F052B00E65FCD /* TranslatablePairTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D6282931B3F052B00E65FCD /* TranslatablePairTests.swift */; }; 6D6282951B3F05DE00E65FCD /* TranslatablePair.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D5004651B3EF92600A54B36 /* TranslatablePair.swift */; }; @@ -68,7 +74,7 @@ 6D6464851B40146600C46C6D /* KeyValueType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D6464831B40146100C46C6D /* KeyValueType.swift */; }; 6DB3CC751B5EBDA600A1220F /* CountryCode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC651B5EBDA600A1220F /* CountryCode.swift */; }; 6DB3CC761B5EBDA600A1220F /* ExpressionRepresentationType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC661B5EBDA600A1220F /* ExpressionRepresentationType.swift */; }; - 6DB3CC771B5EBDA600A1220F /* ExpressionsLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC671B5EBDA600A1220F /* ExpressionsLoader.swift */; }; + 6DB3CC771B5EBDA600A1220F /* SharedExpressionLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC671B5EBDA600A1220F /* SharedExpressionLoader.swift */; }; 6DB3CC781B5EBDA600A1220F /* ExpressionPatternType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC681B5EBDA600A1220F /* ExpressionPatternType.swift */; }; 6DB3CC791B5EBDA600A1220F /* JSONFileLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC691B5EBDA600A1220F /* JSONFileLoader.swift */; }; 6DB3CC7A1B5EBDA600A1220F /* LengthVariation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC6A1B5EBDA600A1220F /* LengthVariation.swift */; }; @@ -95,10 +101,8 @@ 6DBB6C6C1B40431D002F39A3 /* LocalizableFilesLoaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DBB6C6B1B40431D002F39A3 /* LocalizableFilesLoaderTests.swift */; }; 6DBB6C871B40718F002F39A3 /* Expressions.strings in Resources */ = {isa = PBXBuildFile; fileRef = 6DBB6C891B40718F002F39A3 /* Expressions.strings */; }; 6DBB6C8F1B40768A002F39A3 /* SharedBaseExpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DBB6C8C1B40768A002F39A3 /* SharedBaseExpression.swift */; }; - 6DBB6C901B40768A002F39A3 /* SharedExpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DBB6C8D1B40768A002F39A3 /* SharedExpression.swift */; }; 6DBB6C911B40768A002F39A3 /* SharedPolishExpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DBB6C8E1B40768A002F39A3 /* SharedPolishExpression.swift */; }; 6DBB6C921B40769F002F39A3 /* SharedBaseExpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DBB6C8C1B40768A002F39A3 /* SharedBaseExpression.swift */; }; - 6DBB6C931B40769F002F39A3 /* SharedExpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DBB6C8D1B40768A002F39A3 /* SharedExpression.swift */; }; 6DBB6C941B40769F002F39A3 /* SharedPolishExpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DBB6C8E1B40768A002F39A3 /* SharedPolishExpression.swift */; }; 6DBB6C961B4076E8002F39A3 /* SharedBaseExpressionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DBB6C951B4076E8002F39A3 /* SharedBaseExpressionTests.swift */; }; 6DBB6C981B4077FA002F39A3 /* SharedPolishExpressionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DBB6C971B4077FA002F39A3 /* SharedPolishExpressionTests.swift */; }; @@ -107,8 +111,8 @@ 6DD3B93C1B5ED35200C79EAC /* pl.json in Resources */ = {isa = PBXBuildFile; fileRef = 6DD3B9391B5ED35200C79EAC /* pl.json */; }; 6DD3B9411B5ED38B00C79EAC /* JSONFileLoaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DD3B9401B5ED38B00C79EAC /* JSONFileLoaderTests.swift */; }; 6DD3B9421B5ED3F000C79EAC /* JSONFileLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC691B5EBDA600A1220F /* JSONFileLoader.swift */; }; - 6DD3B9441B5ED55500C79EAC /* ExpressionsLoaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DD3B9431B5ED55500C79EAC /* ExpressionsLoaderTests.swift */; }; - 6DD3B9451B5ED58A00C79EAC /* ExpressionsLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC671B5EBDA600A1220F /* ExpressionsLoader.swift */; }; + 6DD3B9441B5ED55500C79EAC /* SharedExpressionsLoaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DD3B9431B5ED55500C79EAC /* SharedExpressionsLoaderTests.swift */; }; + 6DD3B9451B5ED58A00C79EAC /* SharedExpressionLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC671B5EBDA600A1220F /* SharedExpressionLoader.swift */; }; 6DD3B9461B5ED5B500C79EAC /* ProcessableExpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC6D1B5EBDA600A1220F /* ProcessableExpression.swift */; }; 6DD3B9471B5ED61100C79EAC /* ExpressionPatternType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC681B5EBDA600A1220F /* ExpressionPatternType.swift */; }; 6DD3B9481B5ED61500C79EAC /* Processable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC6C1B5EBDA600A1220F /* Processable.swift */; }; @@ -138,6 +142,10 @@ 6D5004651B3EF92600A54B36 /* TranslatablePair.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TranslatablePair.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 = ""; }; + 6D5BA5FD1B6525F9000D7E49 /* TranslationsProcessor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TranslationsProcessor.swift; sourceTree = ""; }; + 6D5BA5FF1B6526F0000D7E49 /* SharedExpression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SharedExpression.swift; path = ../SharedExpression.swift; sourceTree = ""; }; + 6D5BA6031B6537D5000D7E49 /* SharedExpressionsProcessorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SharedExpressionsProcessorTests.swift; sourceTree = ""; }; 6D6282911B3F04C800E65FCD /* Expression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Expression.swift; sourceTree = ""; }; 6D6282931B3F052B00E65FCD /* TranslatablePairTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TranslatablePairTests.swift; sourceTree = ""; }; 6D6282971B3F13C300E65FCD /* ExpressionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExpressionTests.swift; sourceTree = ""; }; @@ -171,7 +179,7 @@ 6D6464831B40146100C46C6D /* KeyValueType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = KeyValueType.swift; sourceTree = ""; }; 6DB3CC651B5EBDA600A1220F /* CountryCode.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CountryCode.swift; sourceTree = ""; }; 6DB3CC661B5EBDA600A1220F /* ExpressionRepresentationType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExpressionRepresentationType.swift; sourceTree = ""; }; - 6DB3CC671B5EBDA600A1220F /* ExpressionsLoader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExpressionsLoader.swift; sourceTree = ""; }; + 6DB3CC671B5EBDA600A1220F /* SharedExpressionLoader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SharedExpressionLoader.swift; sourceTree = ""; }; 6DB3CC681B5EBDA600A1220F /* ExpressionPatternType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExpressionPatternType.swift; sourceTree = ""; }; 6DB3CC691B5EBDA600A1220F /* JSONFileLoader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONFileLoader.swift; sourceTree = ""; }; 6DB3CC6A1B5EBDA600A1220F /* LengthVariation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LengthVariation.swift; sourceTree = ""; }; @@ -193,7 +201,6 @@ 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 = ""; }; 6DBB6C8C1B40768A002F39A3 /* SharedBaseExpression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SharedBaseExpression.swift; sourceTree = ""; }; - 6DBB6C8D1B40768A002F39A3 /* SharedExpression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SharedExpression.swift; sourceTree = ""; }; 6DBB6C8E1B40768A002F39A3 /* SharedPolishExpression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SharedPolishExpression.swift; sourceTree = ""; }; 6DBB6C951B4076E8002F39A3 /* SharedBaseExpressionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SharedBaseExpressionTests.swift; sourceTree = ""; }; 6DBB6C971B4077FA002F39A3 /* SharedPolishExpressionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SharedPolishExpressionTests.swift; sourceTree = ""; }; @@ -201,7 +208,7 @@ 6DD3B9381B5ED35200C79EAC /* expressions.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = expressions.json; sourceTree = ""; }; 6DD3B9391B5ED35200C79EAC /* pl.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = pl.json; sourceTree = ""; }; 6DD3B9401B5ED38B00C79EAC /* JSONFileLoaderTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JSONFileLoaderTests.swift; sourceTree = ""; }; - 6DD3B9431B5ED55500C79EAC /* ExpressionsLoaderTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExpressionsLoaderTests.swift; sourceTree = ""; }; + 6DD3B9431B5ED55500C79EAC /* SharedExpressionsLoaderTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SharedExpressionsLoaderTests.swift; sourceTree = ""; }; 6DD3B94A1B5ED65A00C79EAC /* NSBundle+TestExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSBundle+TestExtension.swift"; sourceTree = ""; }; /* End PBXFileReference section */ @@ -267,6 +274,7 @@ 6DD3B93E1B5ED36F00C79EAC /* Loaders */, 6DB3CC8D1B5EC1FF00A1220F /* Matchers and Parsers */, 6DB3CC8C1B5EC1E000A1220F /* Processable */, + 6D5BA5FC1B652543000D7E49 /* Processors */, 6DB3CC8A1B5EC19400A1220F /* Protocols */, 6DB3CC8B1B5EC1A400A1220F /* Typealiases */, 6DB3CC6A1B5EBDA600A1220F /* LengthVariation.swift */, @@ -298,6 +306,7 @@ children = ( 6D140F451B56D04300359143 /* Helpers */, 6DD3B93F1B5ED37A00C79EAC /* Loaders */, + 6D5BA6021B6537BE000D7E49 /* Processors */, 6D6282971B3F13C300E65FCD /* ExpressionTests.swift */, 6D6282BE1B3F42CA00E65FCD /* InequalityExpressionMatcherTests.swift */, 6D6282A81B3F25DC00E65FCD /* InequalityExpressionParserTests.swift */, @@ -327,6 +336,23 @@ name = "Supporting Files"; sourceTree = ""; }; + 6D5BA5FC1B652543000D7E49 /* Processors */ = { + isa = PBXGroup; + children = ( + 6D5BA5FA1B65253B000D7E49 /* SharedExpressionsProcessor.swift */, + 6D5BA5FD1B6525F9000D7E49 /* TranslationsProcessor.swift */, + ); + name = Processors; + sourceTree = ""; + }; + 6D5BA6021B6537BE000D7E49 /* Processors */ = { + isa = PBXGroup; + children = ( + 6D5BA6031B6537D5000D7E49 /* SharedExpressionsProcessorTests.swift */, + ); + name = Processors; + sourceTree = ""; + }; 6D6283261B3F613D00E65FCD /* DemoOrdering */ = { isa = PBXGroup; children = ( @@ -418,8 +444,8 @@ 6DBB6C8B1B40767B002F39A3 /* Shared Expressions */ = { isa = PBXGroup; children = ( + 6D5BA5FF1B6526F0000D7E49 /* SharedExpression.swift */, 6DBB6C8C1B40768A002F39A3 /* SharedBaseExpression.swift */, - 6DBB6C8D1B40768A002F39A3 /* SharedExpression.swift */, 6DBB6C8E1B40768A002F39A3 /* SharedPolishExpression.swift */, ); path = "Shared Expressions"; @@ -438,7 +464,7 @@ 6DD3B93E1B5ED36F00C79EAC /* Loaders */ = { isa = PBXGroup; children = ( - 6DB3CC671B5EBDA600A1220F /* ExpressionsLoader.swift */, + 6DB3CC671B5EBDA600A1220F /* SharedExpressionLoader.swift */, 6DB3CC691B5EBDA600A1220F /* JSONFileLoader.swift */, 6DB3CC731B5EBDA600A1220F /* TranslationsLoader.swift */, ); @@ -449,7 +475,7 @@ isa = PBXGroup; children = ( 6DD3B9401B5ED38B00C79EAC /* JSONFileLoaderTests.swift */, - 6DD3B9431B5ED55500C79EAC /* ExpressionsLoaderTests.swift */, + 6DD3B9431B5ED55500C79EAC /* SharedExpressionsLoaderTests.swift */, 6D5BA5EF1B651796000D7E49 /* TranslationsLoaderTests.swift */, ); name = Loaders; @@ -604,25 +630,27 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 6D5BA5FB1B65253B000D7E49 /* SharedExpressionsProcessor.swift in Sources */, 6DB3CC7B1B5EBDA600A1220F /* LengthVariationType.swift in Sources */, 6DB3CC811B5EBDA600A1220F /* TranslationLengthVariation.swift in Sources */, 6D6282A31B3F247000E65FCD /* ExpressionMatcher.swift in Sources */, 6D5004661B3EF92600A54B36 /* Swifternalization.swift in Sources */, 6D62829A1B3F17CA00E65FCD /* Regex.swift in Sources */, 6DB3CC841B5EBDA600A1220F /* TranslationType.swift in Sources */, - 6DBB6C901B40768A002F39A3 /* SharedExpression.swift in Sources */, 6D5004671B3EF92600A54B36 /* TranslatablePair.swift in Sources */, 6D6282B51B3F3C4100E65FCD /* InequalitySign.swift in Sources */, 6DBB6C8F1B40768A002F39A3 /* SharedBaseExpression.swift in Sources */, 6DB3CC901B5EC29600A1220F /* ExpressionType.swift in Sources */, 6DB3CC7A1B5EBDA600A1220F /* LengthVariation.swift in Sources */, 6D6464841B40146100C46C6D /* KeyValueType.swift in Sources */, + 6D5BA5FE1B6525F9000D7E49 /* TranslationsProcessor.swift in Sources */, 6DB3CC761B5EBDA600A1220F /* ExpressionRepresentationType.swift in Sources */, 6DB3CC7C1B5EBDA600A1220F /* Processable.swift in Sources */, 6DB3CC751B5EBDA600A1220F /* CountryCode.swift in Sources */, + 6D5BA6001B6526F0000D7E49 /* SharedExpression.swift in Sources */, 6D62829F1B3F1FA000E65FCD /* InequalityExpressionParser.swift in Sources */, 6D6282A51B3F24A800E65FCD /* ExpressionParser.swift in Sources */, - 6DB3CC771B5EBDA600A1220F /* ExpressionsLoader.swift in Sources */, + 6DB3CC771B5EBDA600A1220F /* SharedExpressionLoader.swift in Sources */, 6DBB6C691B4040F0002F39A3 /* InternalPattern.swift in Sources */, 6DB3CC801B5EBDA600A1220F /* ProcessableTranslationLengthVariationExpression.swift in Sources */, 6DB3CC7D1B5EBDA600A1220F /* ProcessableExpression.swift in Sources */, @@ -650,9 +678,9 @@ files = ( 6D6282C91B3F4F6700E65FCD /* RegexExpressionParser.swift in Sources */, 6DBB6C981B4077FA002F39A3 /* SharedPolishExpressionTests.swift in Sources */, - 6DD3B9441B5ED55500C79EAC /* ExpressionsLoaderTests.swift in Sources */, + 6DD3B9441B5ED55500C79EAC /* SharedExpressionsLoaderTests.swift in Sources */, 6D6282C11B3F43C600E65FCD /* InequalityExtendedExpressionMatcherTests.swift in Sources */, - 6DD3B9451B5ED58A00C79EAC /* ExpressionsLoader.swift in Sources */, + 6DD3B9451B5ED58A00C79EAC /* SharedExpressionLoader.swift in Sources */, 6D6282981B3F13C300E65FCD /* ExpressionTests.swift in Sources */, 6DBB6C661B40369A002F39A3 /* SharedExpressionsConfigurator.swift in Sources */, 6D50045B1B3EF91600A54B36 /* SwifternalizationTests.swift in Sources */, @@ -660,12 +688,14 @@ 6D6282B81B3F3E2200E65FCD /* InequalitySign.swift in Sources */, 6D5BA5F31B651809000D7E49 /* LengthVariation.swift in Sources */, 6DB3CC861B5EBF4800A1220F /* CountryCode.swift in Sources */, + 6D5BA6051B653935000D7E49 /* SharedExpressionsProcessor.swift in Sources */, 6D6282A71B3F25BE00E65FCD /* ExpressionMatcher.swift in Sources */, 6DD3B94B1B5ED65A00C79EAC /* NSBundle+TestExtension.swift in Sources */, 6D6464851B40146600C46C6D /* KeyValueType.swift in Sources */, 6DD3B9481B5ED61500C79EAC /* Processable.swift in Sources */, 6D6282C81B3F4F6400E65FCD /* RegexExpressionMatcher.swift in Sources */, 6DD3B9421B5ED3F000C79EAC /* JSONFileLoader.swift in Sources */, + 6D5BA6011B65271A000D7E49 /* SharedExpression.swift in Sources */, 6D5BA5F91B651831000D7E49 /* ProcessableTranslationExpression.swift in Sources */, 6D5BA5F21B6517FE000D7E49 /* TranslationType.swift in Sources */, 6DBB6C6A1B40412D002F39A3 /* InternalPattern.swift in Sources */, @@ -673,7 +703,6 @@ 6DB3CC911B5EC29E00A1220F /* ExpressionType.swift in Sources */, 6D6282941B3F052B00E65FCD /* TranslatablePairTests.swift in Sources */, 6DBB6C941B40769F002F39A3 /* SharedPolishExpression.swift in Sources */, - 6DBB6C931B40769F002F39A3 /* SharedExpression.swift in Sources */, 6DBB6C521B401B8A002F39A3 /* Swifternalization.swift in Sources */, 6D5BA5F71B651825000D7E49 /* ProcessableLengthVariationExpression.swift in Sources */, 6D5BA5F01B651796000D7E49 /* TranslationsLoaderTests.swift in Sources */, @@ -682,6 +711,7 @@ 6D6282BF1B3F42CA00E65FCD /* InequalityExpressionMatcherTests.swift in Sources */, 6D6282961B3F063A00E65FCD /* Expression.swift in Sources */, 6DBB6C921B40769F002F39A3 /* SharedBaseExpression.swift in Sources */, + 6D5BA6041B6537D5000D7E49 /* SharedExpressionsProcessorTests.swift in Sources */, 6D6282B31B3F3C2800E65FCD /* InequalityExtendedExpressionParser.swift in Sources */, 6D140F441B56D03D00359143 /* RandomNumbers.swift in Sources */, 6D6282AD1B3F327C00E65FCD /* InequalityExpressionMatcher.swift in Sources */, diff --git a/Swifternalization/Shared Expressions/SharedBaseExpression.swift b/Swifternalization/Shared Expressions/SharedBaseExpression.swift index ea243ee..30ee66a 100644 --- a/Swifternalization/Shared Expressions/SharedBaseExpression.swift +++ b/Swifternalization/Shared Expressions/SharedBaseExpression.swift @@ -14,16 +14,16 @@ class SharedBaseExpression: SharedExpressionProtocol { return [ /// Matches value equals 1. - SharedExpression(key: "one", pattern: "ie:x=1"), + SharedExpression(identifier: "one", pattern: "ie:x=1"), /// Matches value greater than 1. - SharedExpression(key: ">one", pattern: "ie:x>1"), + SharedExpression(identifier: ">one", pattern: "ie:x>1"), /// Matches value equals 2. - SharedExpression(key: "two", pattern: "ie:x=2"), + SharedExpression(identifier: "two", pattern: "ie:x=2"), /// Matches value other than 1. - SharedExpression(key: "other", pattern: "exp:(^[^1])|(^\\d{2,})") + SharedExpression(identifier: "other", pattern: "exp:(^[^1])|(^\\d{2,})") ] } } \ No newline at end of file diff --git a/Swifternalization/Shared Expressions/SharedPolishExpression.swift b/Swifternalization/Shared Expressions/SharedPolishExpression.swift index db107cd..7c4d46c 100644 --- a/Swifternalization/Shared Expressions/SharedPolishExpression.swift +++ b/Swifternalization/Shared Expressions/SharedPolishExpression.swift @@ -19,7 +19,7 @@ class SharedPolishExpression: SharedExpressionProtocol { - 22 samochody, 1334 samochody, 53 samochody - 2 minuty, 4 minuty, 23 minuty */ - SharedExpression(key: "few", pattern: "exp:(((?!1).[2-4]{1})$)|(^[2-4]$)"), + SharedExpression(identifier: "few", pattern: "exp:(((?!1).[2-4]{1})$)|(^[2-4]$)"), /** 0, (5-9), (10-21), (25-31), ..., (..0, ..1, ..5-9) @@ -28,7 +28,7 @@ class SharedPolishExpression: SharedExpressionProtocol { - 0 samochodów, 10 samochodów, 26 samochodów, 1147 samochodów - 5 minut, 18 minut, 117 minut, 1009 minut */ - SharedExpression(key: "many", pattern: "exp:(^[05-9]$)|(.*(?=1).[0-9]$)|(^[0-9]{1}.*[0156789]$)"), + SharedExpression(identifier: "many", pattern: "exp:(^[05-9]$)|(.*(?=1).[0-9]$)|(^[0-9]{1}.*[0156789]$)"), ] } } diff --git a/Swifternalization/Shared Expressions/SharedExpression.swift b/Swifternalization/SharedExpression.swift similarity index 56% rename from Swifternalization/Shared Expressions/SharedExpression.swift rename to Swifternalization/SharedExpression.swift index f106e46..bf2b2f5 100644 --- a/Swifternalization/Shared Expressions/SharedExpression.swift +++ b/Swifternalization/SharedExpression.swift @@ -1,15 +1,16 @@ // -// SharedExpression.swift +// ProcessableSharedExpression.swift // Swifternalization // -// Created by Tomasz Szulc on 28/06/15. +// Created by Tomasz Szulc on 26/07/15. // Copyright (c) 2015 Tomasz Szulc. All rights reserved. // +import Foundation /** -Protocol that is implemented by classes that contains shared expressions. -Shared expressions are built-in expressions that user can easily use when +Protocol that is implemented by classes/structs that contains shared expressions. +Shared expressions are built-in expressions that user can easily use when localizing app. Rules: http://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html @@ -22,21 +23,16 @@ protocol SharedExpressionProtocol { /** Represents built-in expression and expressions from Expressions.strings file. */ -struct SharedExpression { - /// Key of expression. - let key: Key +struct SharedExpression: ExpressionRepresentationType, ExpressionPatternType { + /// Identifier of expression. + let identifier: String /// Pattern of expression. - let pattern: ExpressionPattern + let pattern: String - /** - Creates shared expression. - - :param: key A key of expression. - :param: pattern A pattern of expression. - */ - init(key: Key, pattern: ExpressionPattern) { - self.key = key + /// Creates expression. + init(identifier: String, pattern: String) { + self.identifier = identifier self.pattern = pattern } } \ No newline at end of file diff --git a/Swifternalization/ExpressionsLoader.swift b/Swifternalization/SharedExpressionLoader.swift similarity index 73% rename from Swifternalization/ExpressionsLoader.swift rename to Swifternalization/SharedExpressionLoader.swift index 7f83995..95c638d 100644 --- a/Swifternalization/ExpressionsLoader.swift +++ b/Swifternalization/SharedExpressionLoader.swift @@ -3,7 +3,7 @@ import Foundation /** Used to load content from `expressions.json` file for specified language. */ -final class ExpressionsLoader: JSONFileLoader { +final class SharedExpressionsLoader: JSONFileLoader { /** Loads expressions for specified language. @@ -12,12 +12,12 @@ final class ExpressionsLoader: JSONFileLoader { :returns: array of loaded expressions. */ - class func loadExpressions(countryCode: CountryCode, bundle: NSBundle) -> [ProcessableExpression] { - var expressions = [ProcessableExpression]() + class func loadExpressions(countryCode: CountryCode, bundle: NSBundle) -> [SharedExpression] { + var expressions = [SharedExpression]() if let json = self.load("expressions", bundle: bundle), let expressionsDict = json[countryCode] as? Dictionary { for (identifier, pattern) in expressionsDict { - expressions.append(ProcessableExpression(identifier: identifier, pattern: pattern)) + expressions.append(SharedExpression(identifier: identifier, pattern: pattern)) } } else { println("expressions.json file structure is incorrect.") diff --git a/Swifternalization/SharedExpressionsConfigurator.swift b/Swifternalization/SharedExpressionsConfigurator.swift index 9f55727..b6f888f 100644 --- a/Swifternalization/SharedExpressionsConfigurator.swift +++ b/Swifternalization/SharedExpressionsConfigurator.swift @@ -85,7 +85,7 @@ class SharedExpressionsConfigurator { private class func convert(expressionsDict: KVDict) -> [SharedExpression] { var result = [SharedExpression]() for (key, pattern) in expressionsDict { - result.append(SharedExpression(key: key, pattern: pattern)) + result.append(SharedExpression(identifier: key, pattern: pattern)) } return result } @@ -117,7 +117,7 @@ class SharedExpressionsConfigurator { */ private class func mergeExpressions(var source: [SharedExpression], additional: [SharedExpression]) -> [SharedExpression] { for additionalExp in additional { - if source.filter({$0.key == additionalExp.key}).first == nil { + if source.filter({$0.identifier == additionalExp.identifier}).first == nil { source.append(additionalExp) } } diff --git a/Swifternalization/SharedExpressionsProcessor.swift b/Swifternalization/SharedExpressionsProcessor.swift new file mode 100644 index 0000000..11ad957 --- /dev/null +++ b/Swifternalization/SharedExpressionsProcessor.swift @@ -0,0 +1,104 @@ +// +// ExpressionsProcessor.swift +// Swifternalization +// +// Created by Tomasz Szulc on 26/07/15. +// Copyright (c) 2015 Tomasz Szulc. All rights reserved. +// + +import Foundation + +/** +Swifternalization contains some built-in country-related shared expressions. +Developer can create its own expressions in expressions.json file for +base and preferred languages. + +The class is responsible for proper loading the built-in shared ones and +those loaded from project's files. It handles overriding of built-in and loads +those from Base and preferred language version. + +It always load base expressions, then looking for language specific. +If in Base are some expressions that overrides built-ins, that's fine. +The class adds developer's custom expressions and adds only those of built-in +that are not contained in the Base. + +The same is for preferred languages but also here what is important is that +at first preferred language file expressions are loaded, then if something +different is defined in Base it will be also loaded and then the built-in +expression that differs. +*/ +class SharedExpressionsProcessor { + + /** + Method takes expression for both Base and preferred language localizations + and also internally loads built-in expressions, combine them and returns + expressions for Base and prefered language. + + :param: preferedLanguage A user device's language. + :param: preferedLanguageExpressions Expressions from expressions.json + :param: baseLanguageExpressions Expressions from base section of + expression.json. + + :returns: array of shared expressions for Base and preferred language. + */ + class func processSharedExpression(preferedLanguage: CountryCode, preferedLanguageExpressions: [SharedExpression], baseLanguageExpressions: [SharedExpression]) -> [SharedExpression] { + /* + Get unique base expressions that are not presented in prefered language + expressions. Those from base will be used in a case when programmer + will ask for string localization and when there is no such expression + in prefered language section defined. + + It means two things: + 1. Programmer make this expression shared through prefered language + and this is good as base expression. + 2. He forgot to define such expression for prefered language. + */ + var uniqueBaseExpressions = baseLanguageExpressions + if preferedLanguageExpressions.count > 0 { + uniqueBaseExpressions = baseLanguageExpressions.filter({ + let pref = $0 + return preferedLanguageExpressions.filter({$0.identifier == pref.identifier}).count == 0 + }) + } + + // Expressions from json files. + var loadedExpressions = uniqueBaseExpressions + preferedLanguageExpressions + + + // Load prefered language nad base built-in expressions. Get unique. + let prefBuiltInExpressions = loadBuiltInExpressions(preferedLanguage) + let baseBuiltInExpressions = SharedBaseExpression.allExpressions() + let uniqueBaseBuiltInExpressions = baseBuiltInExpressions.filter({ + let pref = $0 + return prefBuiltInExpressions.filter({$0.identifier == pref.identifier}).count == 0 + }) + + // Unique built-in expressions made of base + prefered language. + let builtInExpressions = uniqueBaseBuiltInExpressions + prefBuiltInExpressions + + /* + To get it done we must get only unique built-in expressions that are not + in loaded expressions. + */ + let uniqueBuiltInExpressions = builtInExpressions.filter({ + let builtIn = $0 + return loadedExpressions.filter({$0.identifier == builtIn.identifier}).count == 0 + }) + + return loadedExpressions + uniqueBuiltInExpressions + } + + /** + Method loads built-in framework's built-in expressions for specific language. + + :param: language A preferred user's language. + :returns: Shared expressions for specific language. If there is no + expression for passed language empty array is returned. + */ + private class func loadBuiltInExpressions(language: Language) -> [SharedExpression] { + switch language { + case "pl": return SharedPolishExpression.allExpressions() + default: return [] + } + } +} diff --git a/Swifternalization/Swifternalization.swift b/Swifternalization/Swifternalization.swift index 49f5155..3dac14f 100644 --- a/Swifternalization/Swifternalization.swift +++ b/Swifternalization/Swifternalization.swift @@ -226,7 +226,7 @@ public class Swifternalization { for (tKey, tValue) in translatableDict { // Check if there is expression in tKey if let expressionPattern = Expression.parseExpressionPattern(tKey), - let sharedExpression = expressions.filter({$0.key == expressionPattern}).first, + let sharedExpression = expressions.filter({$0.identifier == expressionPattern}).first, // Create expression with pattern from Expressions.strings and // it it is correct use it. let updatedExpression = Expression.expressionFromString("{" + sharedExpression.pattern + "}"), diff --git a/Swifternalization/TranslationsProcessor.swift b/Swifternalization/TranslationsProcessor.swift new file mode 100644 index 0000000..9ee69e7 --- /dev/null +++ b/Swifternalization/TranslationsProcessor.swift @@ -0,0 +1,13 @@ +// +// TranslationsProcessor.swift +// Swifternalization +// +// Created by Tomasz Szulc on 26/07/15. +// Copyright (c) 2015 Tomasz Szulc. All rights reserved. +// + +import Foundation + +class TranslationsProcessor { + +} \ No newline at end of file diff --git a/SwifternalizationTests/SharedBaseExpressionTests.swift b/SwifternalizationTests/SharedBaseExpressionTests.swift index 50440fd..3ba83ff 100644 --- a/SwifternalizationTests/SharedBaseExpressionTests.swift +++ b/SwifternalizationTests/SharedBaseExpressionTests.swift @@ -13,7 +13,7 @@ import Swifternalization class SharedBaseExpressionTests: XCTestCase { func testOne() { - let sharedExp = SharedBaseExpression.allExpressions().filter({$0.key == "one"}).first! + let sharedExp = SharedBaseExpression.allExpressions().filter({$0.identifier == "one"}).first! let expression = Expression(pattern: sharedExp.pattern)! XCTAssertTrue(expression.validate("1"), "Should match 1") @@ -21,7 +21,7 @@ class SharedBaseExpressionTests: XCTestCase { } func testMoreThanOne() { - let sharedExp = SharedBaseExpression.allExpressions().filter({$0.key == ">one"}).first! + let sharedExp = SharedBaseExpression.allExpressions().filter({$0.identifier == ">one"}).first! let expression = Expression(pattern: sharedExp.pattern)! XCTAssertTrue(expression.validate("2"), "Should match 2") @@ -30,7 +30,7 @@ class SharedBaseExpressionTests: XCTestCase { } func testTwo() { - let sharedExp = SharedBaseExpression.allExpressions().filter({$0.key == "two"}).first! + let sharedExp = SharedBaseExpression.allExpressions().filter({$0.identifier == "two"}).first! let expression = Expression(pattern: sharedExp.pattern)! XCTAssertTrue(expression.validate("2"), "Should match 2") @@ -38,7 +38,7 @@ class SharedBaseExpressionTests: XCTestCase { } func testOther() { - let sharedExp = SharedBaseExpression.allExpressions().filter({$0.key == "other"}).first! + let sharedExp = SharedBaseExpression.allExpressions().filter({$0.identifier == "other"}).first! let expression = Expression(pattern: sharedExp.pattern)! XCTAssertTrue(expression.validate("0"), "Should match 0") diff --git a/SwifternalizationTests/ExpressionsLoaderTests.swift b/SwifternalizationTests/SharedExpressionsLoaderTests.swift similarity index 56% rename from SwifternalizationTests/ExpressionsLoaderTests.swift rename to SwifternalizationTests/SharedExpressionsLoaderTests.swift index 4673893..80e06cb 100644 --- a/SwifternalizationTests/ExpressionsLoaderTests.swift +++ b/SwifternalizationTests/SharedExpressionsLoaderTests.swift @@ -9,20 +9,20 @@ import UIKit import XCTest -class ExpressionsLoaderTests: XCTestCase { +class SharedExpressionsLoaderTests: XCTestCase { func testShouldLoadBase() { - let content = ExpressionsLoader.loadExpressions("base", bundle: NSBundle.testBundle()) + let content = SharedExpressionsLoader.loadExpressions("base", bundle: NSBundle.testBundle()) XCTAssertTrue(content.count > 0, "") } func testShouldLoadPL() { - let content = ExpressionsLoader.loadExpressions("pl", bundle: NSBundle.testBundle()) + let content = SharedExpressionsLoader.loadExpressions("pl", bundle: NSBundle.testBundle()) XCTAssertTrue(content.count > 0, "") } func testShouldNotLoadDE() { - let content = ExpressionsLoader.loadExpressions("de", bundle: NSBundle.testBundle()) + let content = SharedExpressionsLoader.loadExpressions("de", bundle: NSBundle.testBundle()) XCTAssertFalse(content.count > 0, "") } } diff --git a/SwifternalizationTests/SharedExpressionsProcessorTests.swift b/SwifternalizationTests/SharedExpressionsProcessorTests.swift new file mode 100644 index 0000000..90e96ea --- /dev/null +++ b/SwifternalizationTests/SharedExpressionsProcessorTests.swift @@ -0,0 +1,22 @@ +// +// SharedExpressionsProcessor.swift +// Swifternalization +// +// Created by Tomasz Szulc on 26/07/15. +// Copyright (c) 2015 Tomasz Szulc. All rights reserved. +// + +import Foundation +import XCTest + +class SharedExpressionsProcessorTests: XCTestCase { + + func testThatAllExpressionsShouldBeLoadedCorreclty() { + let baseExpressions = SharedExpressionsLoader.loadExpressions("base", bundle: NSBundle.testBundle()) + let preferedExpressions = SharedExpressionsLoader.loadExpressions("pl", bundle: NSBundle.testBundle()) + + let sharedExpressions = SharedExpressionsProcessor.processSharedExpression("pl", preferedLanguageExpressions: preferedExpressions, baseLanguageExpressions: baseExpressions) + + XCTAssertEqual(sharedExpressions.count, 8, "") + } +} diff --git a/SwifternalizationTests/SharedPolishExpressionTests.swift b/SwifternalizationTests/SharedPolishExpressionTests.swift index aab30c9..8cbf12f 100644 --- a/SwifternalizationTests/SharedPolishExpressionTests.swift +++ b/SwifternalizationTests/SharedPolishExpressionTests.swift @@ -12,7 +12,7 @@ import XCTest class SharedPolishExpressionTests: XCTestCase { func testFew() { - let sharedExp = SharedPolishExpression.allExpressions().filter({$0.key == "few"}).first! + let sharedExp = SharedPolishExpression.allExpressions().filter({$0.identifier == "few"}).first! let expression = Expression(pattern: sharedExp.pattern)! XCTAssertTrue(expression.validate("2"), "Should match 2") @@ -27,7 +27,7 @@ class SharedPolishExpressionTests: XCTestCase { } func testMany() { - let sharedExp = SharedPolishExpression.allExpressions().filter({$0.key == "many"}).first! + let sharedExp = SharedPolishExpression.allExpressions().filter({$0.identifier == "many"}).first! let expression = Expression(pattern: sharedExp.pattern)! XCTAssertTrue(expression.validate("10"), "Should match 10") diff --git a/SwifternalizationTests/expressions.json b/SwifternalizationTests/expressions.json index 05f0b5d..953b595 100644 --- a/SwifternalizationTests/expressions.json +++ b/SwifternalizationTests/expressions.json @@ -6,6 +6,8 @@ }, "pl": { - "few": "exp:(((?!1).[2-4]{1})$)|(^[2-4]$)" + "few": "exp:(((?!1).[2-4]{1})$)|(^[2-4]$)", + "two": "ie:x=2", + "three": "ie:x=3" } }