Skip to content

Commit

Permalink
Processing translations - WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkowz committed Jul 26, 2015
1 parent 3208b88 commit 5932213
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 11 deletions.
8 changes: 8 additions & 0 deletions Swifternalization.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -146,6 +148,8 @@
6D5BA5FD1B6525F9000D7E49 /* TranslationsProcessor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TranslationsProcessor.swift; sourceTree = "<group>"; };
6D5BA5FF1B6526F0000D7E49 /* SharedExpression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SharedExpression.swift; path = ../SharedExpression.swift; sourceTree = "<group>"; };
6D5BA6031B6537D5000D7E49 /* SharedExpressionsProcessorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SharedExpressionsProcessorTests.swift; sourceTree = "<group>"; };
6D5BA6061B655E06000D7E49 /* TranslationExpression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TranslationExpression.swift; sourceTree = "<group>"; };
6D5BA6081B655F1D000D7E49 /* SimpleExpression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SimpleExpression.swift; sourceTree = "<group>"; };
6D6282911B3F04C800E65FCD /* Expression.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Expression.swift; sourceTree = "<group>"; };
6D6282931B3F052B00E65FCD /* TranslatablePairTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TranslatablePairTests.swift; sourceTree = "<group>"; };
6D6282971B3F13C300E65FCD /* ExpressionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExpressionTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -341,6 +345,8 @@
children = (
6D5BA5FA1B65253B000D7E49 /* SharedExpressionsProcessor.swift */,
6D5BA5FD1B6525F9000D7E49 /* TranslationsProcessor.swift */,
6D5BA6061B655E06000D7E49 /* TranslationExpression.swift */,
6D5BA6081B655F1D000D7E49 /* SimpleExpression.swift */,
);
name = Processors;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand All @@ -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 */,
Expand Down
8 changes: 4 additions & 4 deletions Swifternalization/ProcessableExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
4 changes: 2 additions & 2 deletions Swifternalization/ProcessableTranslationExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
22 changes: 22 additions & 0 deletions Swifternalization/SimpleExpression.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
23 changes: 23 additions & 0 deletions Swifternalization/TranslationExpression.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
6 changes: 3 additions & 3 deletions Swifternalization/TranslationsLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
35 changes: 33 additions & 2 deletions Swifternalization/TranslationsProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,37 @@

import Foundation

class TranslationsProcessor {

class TranslationsProcessor<T where T: TranslationType> {
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
})
}
}

0 comments on commit 5932213

Please sign in to comment.