Skip to content

Commit

Permalink
Implement processing loaded translations
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkowz committed Jul 30, 2015
1 parent 702e549 commit 1034f9c
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 155 deletions.
72 changes: 33 additions & 39 deletions Swifternalization.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions Swifternalization/ExpressionPatternType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// ExpressionPatternType.swift
// Swifternalization
//
// Created by Tomasz Szulc on 21/07/15.
// Copyright (c) 2015 Tomasz Szulc. All rights reserved.
//

import Foundation

/// Supported expression types
enum ExpressionPatternType: String {
/// works on Int only, e.g. `x<5`, `x=3`
case Inequality = "ie"

/// works on Int only, e.g. `4<x<10`, `1<=x<18`
case InequalityExtended = "iex"

/// regular expression, e.g. `[02-9]+`
case Regex = "exp"
}
9 changes: 0 additions & 9 deletions Swifternalization/ExpressionRepresentationType.swift

This file was deleted.

26 changes: 7 additions & 19 deletions Swifternalization/ExpressionType.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
//
// ExpressionPatternType.swift
// Swifternalization
//
// Created by Tomasz Szulc on 21/07/15.
// Copyright (c) 2015 Tomasz Szulc. All rights reserved.
//

import Foundation

/// Supported expression types
enum ExpressionPatternType: String {
/// works on Int only, e.g. `x<5`, `x=3`
case Inequality = "ie"

/// works on Int only, e.g. `4<x<10`, `1<=x<18`
case InequalityExtended = "iex"

/// regular expression, e.g. `[02-9]+`
case Regex = "exp"
}
/**
Represents expressions in the framework.
*/
protocol ExpressionType {
/// Pattern of expression.
var pattern: String {get}
}
10 changes: 2 additions & 8 deletions Swifternalization/LengthVariation.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import Foundation

/**
Struct that represents length variation.
Length variation representation.
*/
struct LengthVariation: LengthVariationType {
/// Length - width of a screen.
let length: Int

/// localized string.
let value: String

/// Create length variation object.
init(length: Int, value: String) {
self.length = length
self.value = value
}
}
}
20 changes: 20 additions & 0 deletions Swifternalization/LengthVariationExpression.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// LengthVariationExpression.swift
// Swifternalization
//
// Created by Tomasz Szulc on 30/07/15.
// Copyright (c) 2015 Tomasz Szulc. All rights reserved.
//

import Foundation

/**
Represents expression that contains length variations.
*/
struct LengthVariationExpression: ExpressionType {
/// Pattern of expression.
let pattern: String

/// Array with length variations.
let variations: [LengthVariation]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// SimpleTranslation.swift
// LengthVariationTranslation.swift
// Swifternalization
//
// Created by Tomasz Szulc on 30/07/15.
Expand All @@ -8,13 +8,13 @@

import Foundation

/**
Represents simple key-value translation.
/**
Represents length variation translation.
*/
struct SimpleTranslation: TranslationType {
struct LengthVariationTranslation: TranslationType {
/// Key that identifies a translation.
let key: String

/// Localized string.
let value: String
/// Length variations.
let variations: [LengthVariation]
}
8 changes: 3 additions & 5 deletions Swifternalization/LoadedTranslation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ struct LoadedTranslation {
/// A type of translation.
let type: LoadedTranslationType

/// Key that identifies this translation
var key: String

/// A content of translation just loaded from a file.
let content: Dictionary<String, AnyObject>

/// Key that identifies this translation
var key: String {
return content.keys.first!
}
}
59 changes: 47 additions & 12 deletions Swifternalization/LoadedTranslationsProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class LoadedTranslationsProcessor {
expressions as well as built-in ones. Translations are processed in shared
expressions processor.
*/
class func processTrnslations(baseTranslations: [LoadedTranslation], preferedLanguageTranslations: [LoadedTranslation], sharedExpressions: [SharedExpression]) {
class func processTrnslations(baseTranslations: [LoadedTranslation], preferedLanguageTranslations: [LoadedTranslation], sharedExpressions: [SharedExpression]) -> [TranslationType] {

// Find those base translations that are not contained in prefered
// language translations.
Expand All @@ -39,32 +39,67 @@ class LoadedTranslationsProcessor {
})
}

var translationsReadyToProcess = preferedLanguageTranslations + uniqueBaseTranslations
var translations: [TranslationType] = translationsReadyToProcess.map({
let translationsReadyToProcess = preferedLanguageTranslations + uniqueBaseTranslations

// Create array with translations. Array is just a map created from
// loaded translations. There are few types of translations and
// expressions used by the framework.
return translationsReadyToProcess.map({
switch $0.type {
case .Simple:
// Simple translation with key and value.
let value = $0.content[$0.key] as! String
return SimpleTranslation(key: $0.key, value: value)
return TranslationWithExpressions(key: $0.key, expressions: [SimpleExpression(pattern: $0.key, localizedValue: value)])

case .WithExpressions:
var expressions = [SimpleExpression]()
// Translation that contains expression.
// Every time when new expressions is about to create,
// the shared expressions are filtered to get expression that
// matches key and if there is a key it is replaced with real
// expression pattern.
var expressions = [ExpressionType]()
for (key, value) in $0.content as! Dictionary<String, String> {
let pattern = sharedExpressions.filter({$0.identifier == key}).first?.pattern ?? key
expressions.append(SimpleExpression(pattern: pattern, localizedValue: value))
}
return TranslationWithExpressions(key: $0.key, expressions: expressions)


// TEMPORARY
//
//
case .WithLengthVariations:
return SimpleTranslation(key: $0.key, value: $0.content[$0.key]! as! String)
// Translation contains length expressions like @100, @200, etc.
var lengthVariations = [LengthVariation]()
for (key, value) in $0.content as! Dictionary<String, String> {
lengthVariations.append(LengthVariation(length: self.parseNumberFromLengthVariation(key), value: value))
}
return LengthVariationTranslation(key: $0.key, variations: lengthVariations)

case .WithExpressionsAndLengthVariations:
return SimpleTranslation(key: $0.key, value: $0.content[$0.key]! as! String)

// The most advanced translation type. It contains expressions
// that contain length variations. THe job done here is similar
// to the one in .WithExpressions and .WithLengthVariations
// cases. key is filtered in shared expressions to get one of
// shared expressions and then method builds array of variations.
var expressions = [ExpressionType]()
for (key, value) in $0.content as! Dictionary<String, Dictionary<String, String>> {
let pattern = sharedExpressions.filter({$0.identifier == key}).first?.pattern ?? key

var lengthVariations = [LengthVariation]()
for (lvKey, lvValue) in value as Dictionary<String, String> {
lengthVariations.append(LengthVariation(length: self.parseNumberFromLengthVariation(lvKey), value: lvValue))
}
expressions.append(LengthVariationExpression(pattern: pattern, variations: lengthVariations))
}
return TranslationWithExpressions(key: $0.key, expressions: expressions)
}
})
}

/**
Parses nubmer from length variation key.

:param: string A string that contains length variation string like @100.
:returns: A number parsed from the string.
*/
private class func parseNumberFromLengthVariation(string: String) -> Int {
return (Regex.matchInString(string, pattern: "@(\\d+)", capturingGroupIdx: 1)! as NSString).integerValue
}
}
12 changes: 0 additions & 12 deletions Swifternalization/Processable.swift

This file was deleted.

20 changes: 4 additions & 16 deletions Swifternalization/SimpleExpression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,13 @@

import Foundation

class SimpleExpression: ExpressionType {
/**
Represents simple epxressions that has only pattern and localized value.
*/
struct SimpleExpression: ExpressionType {
/// Pattern of expression.
let pattern: String

/// A localized value.
let localizedValue: String

init(pattern: String, localizedValue: String) {
self.pattern = pattern
self.localizedValue = localizedValue
}

/**
Validates passed string.

:param: text A text to be validated.
:returns: True if text matches validation rules, otherwise false.
*/
func validate(text: String) -> Bool {
return true
}
}
18 changes: 0 additions & 18 deletions Swifternalization/TranslationLengthVariation.swift

This file was deleted.

9 changes: 0 additions & 9 deletions Swifternalization/TranslationTypeOld.swift

This file was deleted.

4 changes: 2 additions & 2 deletions Swifternalization/TranslationsLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ final class TranslationsLoader: JSONFileLoader {
if let json = self.load(countryCode, bundle: bundle) {
for (key, value) in json {
if value is String {
loadedTranslations.append(LoadedTranslation(type: .Simple, content: [key: value]))
loadedTranslations.append(LoadedTranslation(type: .Simple, key: key, content: [key: value]))
} else {
let dictionary = value as! JSONDictionary
if let type = detectElementType(dictionary) {
loadedTranslations.append(LoadedTranslation(type: type, content: dictionary))
loadedTranslations.append(LoadedTranslation(type: type, key: key, content: dictionary))
} else {
println("Translation type is not supported for: \(dictionary)")
}
Expand Down

0 comments on commit 1034f9c

Please sign in to comment.