Skip to content

Commit

Permalink
Merge pull request #25 from nodes-ios/feature/IsDefaultValueFix
Browse files Browse the repository at this point in the history
Is Default now set to fallback jsons
  • Loading branch information
andrewlloyd100 committed Aug 21, 2019
2 parents 53807fc + 8dccc96 commit 310b08f
Show file tree
Hide file tree
Showing 7 changed files with 254 additions and 3 deletions.
27 changes: 26 additions & 1 deletion TranslationsGenerator/Classes/Downloader/Downloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,32 @@ struct Downloader {
}
}

actualData = data
if let dat = data {

//convert data to a JSON object
let object = try? JSONSerialization.jsonObject(with: dat, options: [.allowFragments])
var json = object as? [String: AnyObject]
var newLanguageJSON = json?["meta"]?["language"] as? [String : Any]

//manipulate the fields required from the config meta to copy over to the fallback jsons
if let _ = newLanguageJSON?["is_default"] as? Bool {
newLanguageJSON?["is_default"] = localization.language.isDefault
}

//overwrite the inital json response with the update json response with new meta values
if var newMeta = json?["meta"] as? [String : Any] {
if let nlj = newLanguageJSON {
newMeta["language"] = nlj
json?["meta"] = newMeta as AnyObject
}
}
let newData = try? JSONSerialization.data(withJSONObject: json, options: [])
actualData = newData
}
else {
actualData = data
}

finalError = customError ?? error

self.semaphore.signal()
Expand Down
142 changes: 142 additions & 0 deletions TranslationsGeneratorTests/Translations.swift
Original file line number Diff line number Diff line change
@@ -1 +1,143 @@
// ----------------------------------------------------------------------
// File generated by NStack Translations Generator.
//
// Copyright (c) 2018 Nodes ApS
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ----------------------------------------------------------------------

import Foundation
import TranslationManager

public final class Translations: LocalizableModel {
public var oneMoreSection = OneMoreSection()
public var otherSection = OtherSection()
public var defaultSection = DefaultSection()

enum CodingKeys: String, CodingKey {
case oneMoreSection
case otherSection
case defaultSection = "default"
}

public override init() { super.init() }

public required init(from decoder: Decoder) throws {
super.init()
let container = try decoder.container(keyedBy: CodingKeys.self)
oneMoreSection = try container.decodeIfPresent(OneMoreSection.self, forKey: .oneMoreSection) ?? oneMoreSection
otherSection = try container.decodeIfPresent(OtherSection.self, forKey: .otherSection) ?? otherSection
defaultSection = try container.decodeIfPresent(DefaultSection.self, forKey: .defaultSection) ?? defaultSection
}
public override subscript(key: String) -> LocalizableSection? {
switch key {
case CodingKeys.oneMoreSection.stringValue: return oneMoreSection
case CodingKeys.otherSection.stringValue: return otherSection
case CodingKeys.defaultSection.stringValue: return defaultSection
default: return nil
}
}

public final class OneMoreSection: LocalizableSection {
public var test2 = ""
public var test1 = ""
public var soManyKeys = ""

enum CodingKeys: String, CodingKey {
case test2
case test1
case soManyKeys
}

public override init() { super.init() }

public required init(from decoder: Decoder) throws {
super.init()
let container = try decoder.container(keyedBy: CodingKeys.self)
test2 = try container.decodeIfPresent(String.self, forKey: .test2) ?? "__test2"
test1 = try container.decodeIfPresent(String.self, forKey: .test1) ?? "__test1"
soManyKeys = try container.decodeIfPresent(String.self, forKey: .soManyKeys) ?? "__soManyKeys"
}

public override subscript(key: String) -> String? {
switch key {
case CodingKeys.test2.stringValue: return test2
case CodingKeys.test1.stringValue: return test1
case CodingKeys.soManyKeys.stringValue: return soManyKeys
default: return nil
}
}
}

public final class OtherSection: LocalizableSection {
public var otherString = ""

enum CodingKeys: String, CodingKey {
case otherString
}

public override init() { super.init() }

public required init(from decoder: Decoder) throws {
super.init()
let container = try decoder.container(keyedBy: CodingKeys.self)
otherString = try container.decodeIfPresent(String.self, forKey: .otherString) ?? "__otherString"
}

public override subscript(key: String) -> String? {
switch key {
case CodingKeys.otherString.stringValue: return otherString
default: return nil
}
}
}

public final class DefaultSection: LocalizableSection {
public var emptyKey = ""
public var keyys = ""
public var successKey = ""

enum CodingKeys: String, CodingKey {
case emptyKey
case keyys
case successKey
}

public override init() { super.init() }

public required init(from decoder: Decoder) throws {
super.init()
let container = try decoder.container(keyedBy: CodingKeys.self)
emptyKey = try container.decodeIfPresent(String.self, forKey: .emptyKey) ?? "__emptyKey"
keyys = try container.decodeIfPresent(String.self, forKey: .keyys) ?? "__keyys"
successKey = try container.decodeIfPresent(String.self, forKey: .successKey) ?? "__successKey"
}

public override subscript(key: String) -> String? {
switch key {
case CodingKeys.emptyKey.stringValue: return emptyKey
case CodingKeys.keyys.stringValue: return keyys
case CodingKeys.successKey.stringValue: return successKey
default: return nil
}
}
}
}

17 changes: 17 additions & 0 deletions TranslationsGeneratorTests/TranslationsGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,21 @@ class TranslationsGeneratorTests: XCTestCase {
let code = try Generator.writeDataToDisk(dData!, settings, localeId: "en-GB")
XCTAssert(code.count > 0)
}

func testPassThroughIsDefaultToFallbackJsons() throws {

XCTAssertNotNil(settings)
let dSettings = try settings.downloaderSettings()
XCTAssertNotNil(dSettings)
let localisations = try Downloader.localizationsWithDownloaderSettings(dSettings)

for locale in localisations ?? [] {
XCTAssertNotNil(locale)
let dData = try Downloader.dataWithDownloaderSettings(dSettings, localization: locale)
XCTAssertNotNil(dData)

let code = try Generator.writeDataToDisk(dData!, settings, localeId: locale.language.locale)
XCTAssert(code.count > 0)
}
}
}
31 changes: 31 additions & 0 deletions TranslationsGeneratorTests/Translations_da-DK.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"data" : {
"default" : {
"emptyKey" : "__emptyKey",
"keyys" : "danishKeyys",
"successKey" : "DET VAR EN SUCCESS"
},
"oneMoreSection" : {
"soManyKeys" : "OrDanishValues",
"test1" : "__test1",
"test2" : "__test2"
},
"otherSection" : {
"otherString" : "Danish Haha"
}
},
"meta" : {
"language" : {
"direction" : "LRM",
"id" : 6,
"is_best_fit" : false,
"is_default" : false,
"locale" : "da-DK",
"name" : "Danish"
},
"platform" : {
"id" : 24,
"slug" : "mobile"
}
}
}
31 changes: 31 additions & 0 deletions TranslationsGeneratorTests/Translations_de-DE.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"data" : {
"default" : {
"emptyKey" : "__emptyKey",
"keyys" : "__keyys",
"successKey" : "__successKey"
},
"oneMoreSection" : {
"soManyKeys" : "__soManyKeys",
"test1" : "__test1",
"test2" : "__test2"
},
"otherSection" : {
"otherString" : "__otherString"
}
},
"meta" : {
"language" : {
"direction" : "LRM",
"id" : 9,
"is_best_fit" : false,
"is_default" : false,
"locale" : "de-DE",
"name" : "German (Germany)"
},
"platform" : {
"id" : 24,
"slug" : "mobile"
}
}
}
5 changes: 3 additions & 2 deletions TranslationsGeneratorTests/Translations_en-GB.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"data" : {
"default" : {
"emptyKey" : "__emptyKey",
"keyys" : "englishKeyys",
"successKey" : "Success"
},
Expand All @@ -17,8 +18,8 @@
"language" : {
"direction" : "LRM",
"id" : 11,
"is_best_fit" : false,
"is_default" : false,
"is_best_fit" : true,
"is_default" : true,
"locale" : "en-GB",
"name" : "English (UK)"
},
Expand Down
4 changes: 4 additions & 0 deletions nstack-translations-generator.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
2301ED3322CF66AB00DA51FB /* Language.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2301ED3222CF66AB00DA51FB /* Language.swift */; };
231C6CD022FB148600C1B26F /* Translations.swift in Sources */ = {isa = PBXBuildFile; fileRef = 231C6CCE22FB148600C1B26F /* Translations.swift */; };
231C6CD122FB148600C1B26F /* Translations_en-GB.json in Resources */ = {isa = PBXBuildFile; fileRef = 231C6CCF22FB148600C1B26F /* Translations_en-GB.json */; };
23E79B3F230C57AD00CC2F60 /* Translations_da-DK.json in Resources */ = {isa = PBXBuildFile; fileRef = 23E79B3E230C57AD00CC2F60 /* Translations_da-DK.json */; };
4BEB675802B887383D43E489 /* Pods_TranslationsGenerator.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6E542EC77F964BEA400B6BF8 /* Pods_TranslationsGenerator.framework */; };
A95D3AA6B98CC1065FAA7AE9 /* Pods_TranslationsGeneratorTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5793EFD3A20502E2E0D0C7BF /* Pods_TranslationsGeneratorTests.framework */; };
B4715A6A224841230020F25B /* Downloader.swift in Sources */ = {isa = PBXBuildFile; fileRef = B4715A5E224841220020F25B /* Downloader.swift */; };
Expand Down Expand Up @@ -98,6 +99,7 @@
2301ED3222CF66AB00DA51FB /* Language.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Language.swift; sourceTree = "<group>"; };
231C6CCE22FB148600C1B26F /* Translations.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Translations.swift; sourceTree = "<group>"; };
231C6CCF22FB148600C1B26F /* Translations_en-GB.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "Translations_en-GB.json"; sourceTree = "<group>"; };
23E79B3E230C57AD00CC2F60 /* Translations_da-DK.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = "Translations_da-DK.json"; sourceTree = "<group>"; };
3CAE0327A1B39683BC5E858A /* Pods-TranslationsGeneratorTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-TranslationsGeneratorTests.debug.xcconfig"; path = "../nstack-translations-generator/Pods/Target Support Files/Pods-TranslationsGeneratorTests/Pods-TranslationsGeneratorTests.debug.xcconfig"; sourceTree = "<group>"; };
5793EFD3A20502E2E0D0C7BF /* Pods_TranslationsGeneratorTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_TranslationsGeneratorTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6BDD6A274A7EF9C93589B97E /* Pods_nstack_translations_generatorBundle.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_nstack_translations_generatorBundle.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -283,6 +285,7 @@
C05B0A9E2174B126003612DE /* TranslationsGeneratorTests */ = {
isa = PBXGroup;
children = (
23E79B3E230C57AD00CC2F60 /* Translations_da-DK.json */,
231C6CCF22FB148600C1B26F /* Translations_en-GB.json */,
231C6CCE22FB148600C1B26F /* Translations.swift */,
C05B0A9F2174B126003612DE /* TranslationsGeneratorTests.swift */,
Expand Down Expand Up @@ -464,6 +467,7 @@
buildActionMask = 2147483647;
files = (
231C6CD122FB148600C1B26F /* Translations_en-GB.json in Resources */,
23E79B3F230C57AD00CC2F60 /* Translations_da-DK.json in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit 310b08f

Please sign in to comment.