Skip to content

Commit

Permalink
Merge branch 'swift2'
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkowz committed Nov 15, 2015
2 parents c3a786c + 19a6f0c commit edf824d
Show file tree
Hide file tree
Showing 18 changed files with 76 additions and 66 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,6 @@ Swifternalization is open sourced so everyone may contribute if want to. If you

There is no guide for contributors but if you added new functionality you must write unit tests for it.

## Swift 2
Swifternalization supports Swift 2 and works on Xcode 7 beta 4. Please check out *swift2* branch.

## Things to do in future releases
- Add more built-in expressions for another countries.
- Add support for float numbers in built in expressions that uses regular expressions.
Expand Down
9 changes: 6 additions & 3 deletions Swifternalization.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,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 */; };
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 */; };
6D5BA5F31B651809000D7E49 /* LengthVariation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DB3CC6A1B5EBDA600A1220F /* LengthVariation.swift */; };
Expand Down Expand Up @@ -384,7 +383,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0630;
LastUpgradeCheck = 0710;
ORGANIZATIONNAME = "Tomasz Szulc";
TargetAttributes = {
6D5004471B3EF91600A54B36 = {
Expand Down Expand Up @@ -429,7 +428,6 @@
files = (
6DD3B93B1B5ED35200C79EAC /* expressions.json in Resources */,
6DD3B93A1B5ED35200C79EAC /* base.json in Resources */,
6D5004961B3EFFC100A54B36 /* Info.plist in Resources */,
6DD3B93C1B5ED35200C79EAC /* pl.json in Resources */,
6D43BA1D1B8BBA270072DEA6 /* ru.json in Resources */,
);
Expand Down Expand Up @@ -562,6 +560,7 @@
CURRENT_PROJECT_VERSION = 1;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
Expand Down Expand Up @@ -643,6 +642,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.tomaszszulc.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
Expand All @@ -663,6 +663,7 @@
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.tomaszszulc.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
};
Expand All @@ -681,6 +682,7 @@
);
INFOPLIST_FILE = SwifternalizationTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.tomaszszulc.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
Expand All @@ -694,6 +696,7 @@
);
INFOPLIST_FILE = SwifternalizationTests/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "com.tomaszszulc.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0630"
LastUpgradeVersion = "0710"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down Expand Up @@ -37,10 +37,10 @@
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand All @@ -62,15 +62,18 @@
ReferencedContainer = "container:Swifternalization.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
Expand All @@ -85,10 +88,10 @@
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0630"
LastUpgradeVersion = "0710"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand All @@ -23,10 +23,10 @@
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
buildConfiguration = "Debug">
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
Expand All @@ -48,15 +48,18 @@
ReferencedContainer = "container:Swifternalization.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
buildConfiguration = "Debug"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
Expand All @@ -71,10 +74,10 @@
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
buildConfiguration = "Release"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
Expand Down
4 changes: 2 additions & 2 deletions Swifternalization/InequalityExpressionParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class InequalityExpressionParser: ExpressionParser {
if let value = Regex.matchInString(pattern, pattern: regex, capturingGroupIdx: capturingGroupIdx) {
return NSString(string: value).doubleValue
} else {
println("\(failureMessage), pattern: \(pattern), regex: \(regex)")
print("\(failureMessage), pattern: \(pattern), regex: \(regex)")
return nil
}
}
Expand All @@ -89,7 +89,7 @@ class InequalityExpressionParser: ExpressionParser {
let sign = InequalitySign(rawValue: rawValue) {
return sign
} else {
println("\(failureMessage), pattern: \(pattern), regex: \(regex)")
print("\(failureMessage), pattern: \(pattern), regex: \(regex)")
return nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion Swifternalization/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>com.tomaszszulc.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
16 changes: 10 additions & 6 deletions Swifternalization/JSONFileLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final class JSONFileLoader {
if let fileURL = bundle.URLForResource(fileName, withExtension: "json") {
return load(fileURL)
}
println("Cannot find file \(fileName).json.")
print("Cannot find file \(fileName).json.")
return nil
}

Expand All @@ -56,12 +56,16 @@ final class JSONFileLoader {
*/
private class func load(fileURL: NSURL) -> JSONDictionary? {
if let data = NSData(contentsOfURL: fileURL) {
var error: NSError?
if let dictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.allZeros, error: &error) as? JSONDictionary {
return dictionary
do {
if let dictionary = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? JSONDictionary {
return dictionary
}
print("Cannot parse JSON. It might be broken.")
return nil
} catch {
print("Cannot parse JSON. It might be broken.")
return nil
}
print("Cannot parse JSON. It might be broken.")
return nil
}
print("Cannot load content of file.")
return nil
Expand Down
22 changes: 11 additions & 11 deletions Swifternalization/Regex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ final class Regex {
class func matchInString(str: String, pattern: String, capturingGroupIdx: Int?) -> String? {
var resultString: String?

let range = NSMakeRange(0, count(str))
regexp(pattern)?.enumerateMatchesInString(str, options: nil, range: range, usingBlock: { result, flags, stop in
let range = NSMakeRange(0, str.startIndex.distanceTo(str.endIndex))
regexp(pattern)?.enumerateMatchesInString(str, options: NSMatchingOptions.ReportCompletion, range: range, usingBlock: { result, flags, stop in
if let result = result {
if let capturingGroupIdx = capturingGroupIdx where result.numberOfRanges > capturingGroupIdx {
resultString = self.substring(str, range: result.rangeAtIndex(capturingGroupIdx))
Expand All @@ -46,7 +46,7 @@ final class Regex {
:returns: `String` that matches pattern or nil.
*/
class func firstMatchInString(str: String, pattern: String) -> String? {
if let result = regexp(pattern)?.firstMatchInString(str, options: .ReportCompletion, range: NSMakeRange(0, count(str))) {
if let result = regexp(pattern)?.firstMatchInString(str, options: .ReportCompletion, range: NSMakeRange(0, str.startIndex.distanceTo(str.endIndex))) {
return substring(str, range: result.range)
}
return nil
Expand All @@ -61,7 +61,7 @@ final class Regex {
*/
class func matchesInString(str: String, pattern: String) -> [String] {
var matches = [String]()
if let results = regexp(pattern)?.matchesInString(str, options: .ReportCompletion, range: NSMakeRange(0, count(str))) as? [NSTextCheckingResult] {
if let results = regexp(pattern)?.matchesInString(str, options: .ReportCompletion, range: NSMakeRange(0, str.startIndex.distanceTo(str.endIndex))) {
for result in results {
matches.append(substring(str, range: result.range))
}
Expand All @@ -77,12 +77,12 @@ final class Regex {
:returns: `NSRegularExpression` object or nil if it cannot be created.
*/
private class func regexp(pattern: String) -> NSRegularExpression? {
var error: NSError? = nil
var regexp = NSRegularExpression(pattern: pattern, options: .CaseInsensitive, error: &error)
if error != nil {
println(error!)
do {
return try NSRegularExpression(pattern: pattern, options: NSRegularExpressionOptions.CaseInsensitive)
} catch let error as NSError {
print(error)
}
return regexp
return nil
}

/**
Expand All @@ -93,8 +93,8 @@ final class Regex {
:returns: A string contained in `range`.
*/
private class func substring(str: String, range: NSRange) -> String {
let startRange = advance(str.startIndex, range.location)
let endRange = advance(startRange, range.length)
let startRange = str.startIndex.advancedBy(range.location)
let endRange = startRange.advancedBy(range.length)

return str.substringWithRange(Range(start: startRange, end: endRange))
}
Expand Down
2 changes: 1 addition & 1 deletion Swifternalization/RegexExpressionParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class RegexExpressionParser: ExpressionParser {
if let regex = Regex.firstMatchInString(pattern, pattern: "(?<=^\(ExpressionPatternType.Regex.rawValue):).*") {
return regex
} else {
println("Cannot find any regular expression, pattern: \(pattern)")
print("Cannot find any regular expression, pattern: \(pattern)")
return nil
}
}
Expand Down
2 changes: 1 addition & 1 deletion Swifternalization/SharedExpressionsProcessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class SharedExpressionsProcessor {
let uniqueBaseExpressions = baseLanguageExpressions <! preferedLanguageExpressions

// Expressions from json files.
var loadedExpressions = uniqueBaseExpressions + preferedLanguageExpressions
let loadedExpressions = uniqueBaseExpressions + preferedLanguageExpressions

// Load prefered language nad base built-in expressions. Get unique.
let prefBuiltInExpressions = loadBuiltInExpressions(preferedLanguage)
Expand Down
4 changes: 2 additions & 2 deletions Swifternalization/Swifternalization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final public class Swifternalization {
:param: bundle A bundle when expressions.json and other files are located.
*/
public class func configure(bundle: NSBundle = NSBundle.mainBundle()) {
sharedInstance.load(bundle: bundle)
sharedInstance.load(bundle)
}

/**
Expand Down Expand Up @@ -172,6 +172,6 @@ final public class Swifternalization {
*/
private func getPreferredLanguage(bundle: NSBundle) -> CountryCode {
// Get preferred language, the one which is set on user's device
return bundle.preferredLocalizations.first as! CountryCode
return bundle.preferredLocalizations.first! as CountryCode
}
}
2 changes: 1 addition & 1 deletion Swifternalization/Translation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ struct Translation {
If variation width is shorter or equal `fittingWidth`
take associated value.
*/
for variation in expression.lengthVariations.sorted({$0.width < $1.width}) {
for variation in expression.lengthVariations.sort({$0.width < $1.width}) {
if variation.width <= fittingWidth! {
localizedValue = variation.value
}
Expand Down
6 changes: 3 additions & 3 deletions Swifternalization/TranslationsLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class TranslationsLoader {
if let type = detectElementType(dictionary) {
loadedTranslations.append(LoadedTranslation(type: type, key: key, content: dictionary))
} else {
println("Translation type is not supported for: \(dictionary)")
print("Translation type is not supported for: \(dictionary)")
}
}
}
Expand All @@ -42,7 +42,7 @@ final class TranslationsLoader {
var strings = 0

// Count every string or dict occurence.
for (key, value) in element {
for (_, value) in element {
if value is String {
strings++
} else if value is Dictionary<String, AnyObject> {
Expand All @@ -57,7 +57,7 @@ final class TranslationsLoader {
*/
if strings > 0 && dicts == 0 {
let key = element.keys.first!
let toIndex = advance(key.startIndex, 1)
let toIndex = key.startIndex.advancedBy(1)
return key.substringToIndex(toIndex) == "@" ? .WithLengthVariations : .WithExpressions
} else if strings >= 0 && dicts > 0 {
return .WithExpressionsAndLengthVariations
Expand Down
Loading

0 comments on commit edf824d

Please sign in to comment.