diff --git a/Swifternalization.xcodeproj/project.pbxproj b/Swifternalization.xcodeproj/project.pbxproj index ddf90e5..9f25c23 100644 --- a/Swifternalization.xcodeproj/project.pbxproj +++ b/Swifternalization.xcodeproj/project.pbxproj @@ -31,6 +31,8 @@ 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 */; }; + 6D5BA6071B655E06000D7E49 /* TranslationExpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D5BA6061B655E06000D7E49 /* TranslationExpression.swift */; }; + 6D5BA6091B655F1D000D7E49 /* SimpleExpression.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6D5BA6081B655F1D000D7E49 /* SimpleExpression.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 */; }; @@ -146,6 +148,8 @@ 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 = ""; }; + 6D5BA6061B655E06000D7E49 /* TranslationExpression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TranslationExpression.swift; sourceTree = ""; }; + 6D5BA6081B655F1D000D7E49 /* SimpleExpression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SimpleExpression.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 = ""; }; @@ -341,6 +345,8 @@ children = ( 6D5BA5FA1B65253B000D7E49 /* SharedExpressionsProcessor.swift */, 6D5BA5FD1B6525F9000D7E49 /* TranslationsProcessor.swift */, + 6D5BA6061B655E06000D7E49 /* TranslationExpression.swift */, + 6D5BA6081B655F1D000D7E49 /* SimpleExpression.swift */, ); name = Processors; sourceTree = ""; @@ -643,6 +649,7 @@ 6DB3CC901B5EC29600A1220F /* ExpressionType.swift in Sources */, 6DB3CC7A1B5EBDA600A1220F /* LengthVariation.swift in Sources */, 6D6464841B40146100C46C6D /* KeyValueType.swift in Sources */, + 6D5BA6071B655E06000D7E49 /* TranslationExpression.swift in Sources */, 6D5BA5FE1B6525F9000D7E49 /* TranslationsProcessor.swift in Sources */, 6DB3CC761B5EBDA600A1220F /* ExpressionRepresentationType.swift in Sources */, 6DB3CC7C1B5EBDA600A1220F /* Processable.swift in Sources */, @@ -653,6 +660,7 @@ 6DB3CC771B5EBDA600A1220F /* SharedExpressionLoader.swift in Sources */, 6DBB6C691B4040F0002F39A3 /* InternalPattern.swift in Sources */, 6DB3CC801B5EBDA600A1220F /* ProcessableTranslationLengthVariationExpression.swift in Sources */, + 6D5BA6091B655F1D000D7E49 /* SimpleExpression.swift in Sources */, 6DB3CC7D1B5EBDA600A1220F /* ProcessableExpression.swift in Sources */, 6DBB6C911B40768A002F39A3 /* SharedPolishExpression.swift in Sources */, 6DB3CC7E1B5EBDA600A1220F /* ProcessableLengthVariationExpression.swift in Sources */, diff --git a/Swifternalization/ProcessableExpression.swift b/Swifternalization/ProcessableExpression.swift index 665ed17..ce2ab66 100644 --- a/Swifternalization/ProcessableExpression.swift +++ b/Swifternalization/ProcessableExpression.swift @@ -3,16 +3,16 @@ import Foundation /** Represents loaded expression that will be processed later. */ -struct ProcessableExpression: ExpressionRepresentationType, ExpressionPatternType, Processable { +struct ProcessableExpressionSimple: ExpressionRepresentationType, Processable { /// Identifier of expression. let identifier: String /// Pattern of expression. - let pattern: String + let value: String /// Creates expression. - init(identifier: String, pattern: String) { + init(identifier: String, value: String) { self.identifier = identifier - self.pattern = pattern + self.value = value } } diff --git a/Swifternalization/ProcessableTranslationExpression.swift b/Swifternalization/ProcessableTranslationExpression.swift index d3b7ab8..da6560d 100644 --- a/Swifternalization/ProcessableTranslationExpression.swift +++ b/Swifternalization/ProcessableTranslationExpression.swift @@ -8,10 +8,10 @@ struct ProcessableTranslationExpression: TranslationType, Processable { let key: String /// Array with loaded expressions. - let loadedExpressions: [ProcessableExpression] + let loadedExpressions: [ProcessableExpressionSimple] /// Creates instances of the class. - init(key: String, loadedExpressions: [ProcessableExpression]) { + init(key: String, loadedExpressions: [ProcessableExpressionSimple]) { self.key = key self.loadedExpressions = loadedExpressions } diff --git a/Swifternalization/SimpleExpression.swift b/Swifternalization/SimpleExpression.swift new file mode 100644 index 0000000..c6cd7bf --- /dev/null +++ b/Swifternalization/SimpleExpression.swift @@ -0,0 +1,22 @@ +// +// SimpleExpression.swift +// Swifternalization +// +// Created by Tomasz Szulc on 26/07/15. +// Copyright (c) 2015 Tomasz Szulc. All rights reserved. +// + +import Foundation + +class SimpleExpression: ExpressionRepresentationType { + /// Identifier of expression. + let identifier: String + + /// A localized value. + let value: String + + init(identifier: String, value: String) { + self.identifier = identifier + self.value = value + } +} diff --git a/Swifternalization/TranslationExpression.swift b/Swifternalization/TranslationExpression.swift new file mode 100644 index 0000000..d2456b3 --- /dev/null +++ b/Swifternalization/TranslationExpression.swift @@ -0,0 +1,23 @@ +// +// TranslationExpression.swift +// Swifternalization +// +// Created by Tomasz Szulc on 26/07/15. +// Copyright (c) 2015 Tomasz Szulc. All rights reserved. +// + +import Foundation + +class TranslationExpression: TranslationType { + /// Key that identifies translation. + let key: String + + /// Array with loaded expressions. + let loadedExpressions: [SimpleExpression] + + /// Creates instances of the class. + init(key: String, loadedExpressions: [SimpleExpression]) { + self.key = key + self.loadedExpressions = loadedExpressions + } +} diff --git a/Swifternalization/TranslationsLoader.swift b/Swifternalization/TranslationsLoader.swift index b9675f5..1ba08e0 100644 --- a/Swifternalization/TranslationsLoader.swift +++ b/Swifternalization/TranslationsLoader.swift @@ -70,10 +70,10 @@ final class TranslationsLoader: JSONFileLoader { return variations } - private class func parseExpressions(dict: DictWithStrings) -> [ProcessableExpression] { - var expressions = [ProcessableExpression]() + private class func parseExpressions(dict: DictWithStrings) -> [ProcessableExpressionSimple] { + var expressions = [ProcessableExpressionSimple]() for (expressionKey, translationValue) in dict { - expressions.append(ProcessableExpression(identifier: expressionKey, pattern: translationValue)) + expressions.append(ProcessableExpressionSimple(identifier: expressionKey, value: translationValue)) } return expressions } diff --git a/Swifternalization/TranslationsProcessor.swift b/Swifternalization/TranslationsProcessor.swift index 9ee69e7..af4a579 100644 --- a/Swifternalization/TranslationsProcessor.swift +++ b/Swifternalization/TranslationsProcessor.swift @@ -8,6 +8,37 @@ import Foundation -class TranslationsProcessor { - +class TranslationsProcessor { + class func processTranslations(baseTranslations: [T], preferedLanguageTranslations: [T], sharedExpressions: [SharedExpression]) { + + var uniqueBaseTranslations = baseTranslations + if preferedLanguageTranslations.count > 0 { + uniqueBaseTranslations = baseTranslations.filter({ + let base = $0 + return preferedLanguageTranslations.filter({$0.key == base.key}).count == 0 + }) + } + + var translationsReadyToProcess = preferedLanguageTranslations + uniqueBaseTranslations + + var translations: [TranslationType] = translationsReadyToProcess.map({ + if $0 is TranslationSimple { + return $0 + } else if $0 is ProcessableTranslationExpression { + let processableTranslation = $0 as! ProcessableTranslationExpression + + let expressions = processableTranslation.loadedExpressions.map({ (processableExpr: ProcessableExpressionSimple) -> SimpleExpression in + var identifier = processableExpr.identifier + if let sharedExpression = sharedExpressions.filter({$0.identifier == identifier}).first { + identifier = sharedExpression.pattern + } + return SimpleExpression(identifier: identifier, value: processableExpr.value) + }) + + return TranslationExpression(key: processableTranslation.key, loadedExpressions: expressions) + } + + return $0 + }) + } } \ No newline at end of file