Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with path parser. #138

Merged
merged 5 commits into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@ fileprivate enum Paths: String {
static var all: [Paths] = [.language, .locale, .localeWithUnderscore, .osxCode, .osxLocale, .twoLettersCode]

func value(for localization: String) -> String {
guard let language = CrowdinSupportedLanguages.shared.crowdinSupportedLanguage(for: localization) else { return "" }
switch self {
case .language:
// swiftlint:disable force_unwrapping
let languageCode = Locale(identifier: localization).languageCode!
return enLocale.localizedString(forLanguageCode: languageCode) ?? ""
return language.name
case .locale:
return Locale(identifier: localization).identifier.replacingOccurrences(of: "_", with: "-")
return language.locale
case .localeWithUnderscore:
return Locale(identifier: localization).identifier.replacingOccurrences(of: "-", with: "_")
return language.locale.replacingOccurrences(of: "-", with: "_")
case .osxCode:
return Locale(identifier: localization).identifier + ".lproj"
return language.osxCode
case .osxLocale:
return Locale(identifier: localization).identifier
return language.osxLocale
case .twoLettersCode:
return Locale(identifier: localization).regionCode ?? Locale(identifier: localization).identifier
return language.twoLettersCode
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ extension LanguagesResponseData {
}
}

protocol SupportedLanguage: Codable {
var id: String { get }
var name: String { get }
var editorCode: String { get }
var twoLettersCode: String { get }
var threeLettersCode: String { get }
var locale: String { get }
var osxCode: String { get }
var osxLocale: String { get }
}

extension LanguagesResponseData: SupportedLanguage { }

class CrowdinSupportedLanguages {
static let shared = CrowdinSupportedLanguages()
let api = LanguagesAPI()
Expand Down Expand Up @@ -59,6 +72,21 @@ class CrowdinSupportedLanguages {
return language?.data.id
}

func crowdinSupportedLanguage(for localization: String) -> SupportedLanguage? {
var language = supportedLanguages?.data.first(where: { $0.data.iOSLocaleCode == localization })
if language == nil {
// This is possible for languages ​​with regions. In case we didn't find Crowdin language mapping, try to replace _ in location code with -
let alternateiOSLocaleCode = localization.replacingOccurrences(of: "_", with: "-")
language = supportedLanguages?.data.first(where: { $0.data.iOSLocaleCode == alternateiOSLocaleCode })
}
if language == nil {
// This is possible for languages ​​with regions. In case we didn't find Crowdin language mapping, try to get localization code and search again
let alternateiOSLocaleCode = localization.split(separator: "-").map({ String($0) }).first!
language = supportedLanguages?.data.first(where: { $0.data.iOSLocaleCode == alternateiOSLocaleCode })
}
return language?.data
}

func iOSLanguageCode(for crowdinLocalization: String) -> String? {
return supportedLanguages?.data.first(where: { $0.data.id == crowdinLocalization })?.data.iOSLocaleCode
}
Expand Down Expand Up @@ -91,6 +119,16 @@ class CrowdinSupportedLanguages {
}
}

func downloadSupportedLanguagesSync() {
let semaphore = DispatchSemaphore(value: 0)
self.downloadSupportedLanguages(completion: {
semaphore.signal()
}, error: { _ in
semaphore.signal()
})
_ = semaphore.wait(timeout: .now() + 60)
}

fileprivate func saveSupportedLanguages() {
guard let data = try? JSONEncoder().encode(supportedLanguages) else { return }
try? data.write(to: URL(fileURLWithPath: filePath), options: Data.WritingOptions.atomic)
Expand Down
68 changes: 23 additions & 45 deletions CrowdinSDK/Tests/CrowdinProvider/CrowdinPathsParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import XCTest
@testable import CrowdinSDK

class CrowdinPathsParserTests: XCTestCase {
var pathParser: CrowdinPathsParser!
var pathParser = CrowdinPathsParser.shared

override func setUp() {
self.pathParser = CrowdinPathsParser()
if CrowdinSupportedLanguages.shared.loaded == false {
CrowdinSupportedLanguages.shared.downloadSupportedLanguagesSync()
}
}

func testContainsLanguageCustomPath() {
Expand Down Expand Up @@ -35,11 +37,11 @@ class CrowdinPathsParserTests: XCTestCase {
// mark - Locale

func testParseLocaleCustomPathForEnLocalization() {
XCTAssert(self.pathParser.parse("%locale%/Localizable.strings", localization: "en") == "en/Localizable.strings", "")
XCTAssert(self.pathParser.parse("%locale%/Localizable.strings", localization: "en") == "en-US/Localizable.strings", "")
}

func testParseLocaleCustomPathForDeLocalization() {
XCTAssert(self.pathParser.parse("%locale%/Localizable.strings", localization: "de") == "de/Localizable.strings", "")
XCTAssert(self.pathParser.parse("%locale%/Localizable.strings", localization: "de") == "de-DE/Localizable.strings", "")
}

func testParseLocaleCustomPathForEnUSLocalization() {
Expand All @@ -50,16 +52,12 @@ class CrowdinPathsParserTests: XCTestCase {
XCTAssert(self.pathParser.parse("%locale%/Localizable.strings", localization: "en_US") == "en-US/Localizable.strings", "")
}

func testParseLocaleCustomPathForZhLocalization() {
XCTAssert(self.pathParser.parse("%locale%/Localizable.strings", localization: "zh") == "zh/Localizable.strings", "")
}

func testParseLocaleCustomPathForZhHantLocalization() {
XCTAssert(self.pathParser.parse("%locale%/Localizable.strings", localization: "zh_Hant") == "zh-Hant/Localizable.strings", "")
func testParseLocaleCustomPathForsZhHantLocalization() {
XCTAssert(self.pathParser.parse("%locale%/Localizable.strings", localization: "zh_Hant") == "zh-TW/Localizable.strings", "")
}

func testParseLocaleCustomPathForZhHansLocalization() {
XCTAssert(self.pathParser.parse("%locale%/Localizable.strings", localization: "zh_Hans") == "zh-Hans/Localizable.strings", "")
XCTAssert(self.pathParser.parse("%locale%/Localizable.strings", localization: "zh_Hans") == "zh-CN/Localizable.strings", "")
}

// mark - Language
Expand All @@ -77,33 +75,29 @@ class CrowdinPathsParserTests: XCTestCase {
}

func testParseLanguageCustomPathForEnUSLocalization() {
XCTAssert(self.pathParser.parse("%language%/Localizable.strings", localization: "en-US") == "English/Localizable.strings", "")
XCTAssert(self.pathParser.parse("%language%/Localizable.strings", localization: "en-US") == "English, United States/Localizable.strings", "")
}

func testParseLanguageCustomPathForEnUSWithUnderscoreLocalization() {
XCTAssert(self.pathParser.parse("%language%/Localizable.strings", localization: "en_US") == "English/Localizable.strings", "")
}

func testParseLanguageCustomPathForZhLocalization() {
XCTAssert(self.pathParser.parse("%language%/Localizable.strings", localization: "zh") == "Chinese/Localizable.strings", "")
XCTAssert(self.pathParser.parse("%language%/Localizable.strings", localization: "en_US") == "English, United States/Localizable.strings", "")
}

func testParseLanguageCustomPathForZhHantLocalization() {
XCTAssert(self.pathParser.parse("%language%/Localizable.strings", localization: "zh_Hant") == "Chinese/Localizable.strings", "")
XCTAssert(self.pathParser.parse("%language%/Localizable.strings", localization: "zh_Hant") == "Chinese Traditional/Localizable.strings", "")
}

func testParseLanguageCustomPathForZhHansLocalization() {
XCTAssert(self.pathParser.parse("%language%/Localizable.strings", localization: "zh_Hans") == "Chinese/Localizable.strings", "")
XCTAssert(self.pathParser.parse("%language%/Localizable.strings", localization: "zh_Hans") == "Chinese Simplified/Localizable.strings", "")
}

// mark - Locale With Underscore

func testParseLocaleWithUnderscoreCustomPathForEnLocalization() {
XCTAssert(self.pathParser.parse("%locale_with_underscore%/Localizable.strings", localization: "en") == "en/Localizable.strings", "")
XCTAssert(self.pathParser.parse("%locale_with_underscore%/Localizable.strings", localization: "en") == "en_US/Localizable.strings", "")
}

func testParseLocaleWithUnderscoreCustomPathForDeLocalization() {
XCTAssert(self.pathParser.parse("%locale_with_underscore%/Localizable.strings", localization: "de") == "de/Localizable.strings", "")
XCTAssert(self.pathParser.parse("%locale_with_underscore%/Localizable.strings", localization: "de") == "de_DE/Localizable.strings", "")
}

func testParseLocaleWithUnderscoreCustomPathForEnUSLocalization() {
Expand All @@ -114,16 +108,12 @@ class CrowdinPathsParserTests: XCTestCase {
XCTAssert(self.pathParser.parse("%locale_with_underscore%/Localizable.strings", localization: "en_US") == "en_US/Localizable.strings", "")
}

func testParseLocaleWithUnderscoreCustomPathForZhLocalization() {
XCTAssert(self.pathParser.parse("%locale_with_underscore%/Localizable.strings", localization: "zh") == "zh/Localizable.strings", "")
}

func testParseLocaleWithUnderscoreCustomPathForZhHantLocalization() {
XCTAssert(self.pathParser.parse("%locale_with_underscore%/Localizable.strings", localization: "zh_Hant") == "zh_Hant/Localizable.strings", "")
XCTAssert(self.pathParser.parse("%locale_with_underscore%/Localizable.strings", localization: "zh_Hant") == "zh_TW/Localizable.strings", "")
}

func testParseLocaleWithUnderscoreCustomPathForZhHansLocalization() {
XCTAssert(self.pathParser.parse("%locale_with_underscore%/Localizable.strings", localization: "zh_Hans") == "zh_Hans/Localizable.strings", "")
XCTAssert(self.pathParser.parse("%locale_with_underscore%/Localizable.strings", localization: "zh_Hans") == "zh_CN/Localizable.strings", "")
}

// mark - osx code
Expand All @@ -141,11 +131,7 @@ class CrowdinPathsParserTests: XCTestCase {
}

func testParseOsxCodeCustomPathForEnUSWithUnderscoreLocalization() {
XCTAssert(self.pathParser.parse("%osx_code%/Localizable.strings", localization: "en_US") == "en_US.lproj/Localizable.strings", "")
}

func testParseOsxCodeCustomPathForZhLocalization() {
XCTAssert(self.pathParser.parse("%osx_code%/Localizable.strings", localization: "zh") == "zh.lproj/Localizable.strings", "")
XCTAssert(self.pathParser.parse("%osx_code%/Localizable.strings", localization: "en_US") == "en-US.lproj/Localizable.strings", "")
}

func testParseOsxCodeCustomPathForZhHantLocalization() {
Expand All @@ -167,17 +153,13 @@ class CrowdinPathsParserTests: XCTestCase {
}

func testParseOsxLocaleCustomPathForEnUSLocalization() {
XCTAssert(self.pathParser.parse("%osx_locale%/Localizable.strings", localization: "en-US") == "en-US/Localizable.strings", "")
XCTAssert(self.pathParser.parse("%osx_locale%/Localizable.strings", localization: "en-US") == "en_US/Localizable.strings", "")
}

func testParseOsxLocaleCustomPathForEnUSWithUnderscoreLocalization() {
XCTAssert(self.pathParser.parse("%osx_locale%/Localizable.strings", localization: "en_US") == "en_US/Localizable.strings", "")
}

func testParseOsxLocaleCustomPathForZhLocalization() {
XCTAssert(self.pathParser.parse("%osx_locale%/Localizable.strings", localization: "zh") == "zh/Localizable.strings", "")
}

func testParseOsxLocaleCustomPathForZhHantLocalization() {
XCTAssert(self.pathParser.parse("%osx_locale%/Localizable.strings", localization: "zh-Hant") == "zh-Hant/Localizable.strings", "")
}
Expand All @@ -197,22 +179,18 @@ class CrowdinPathsParserTests: XCTestCase {
}

func testParseTwoLettersLocaleCustomPathForEnUSLocalization() {
XCTAssert(self.pathParser.parse("%two_letters_code%/Localizable.strings", localization: "en-US") == "US/Localizable.strings", "")
XCTAssert(self.pathParser.parse("%two_letters_code%/Localizable.strings", localization: "en-US") == "en/Localizable.strings", "")
}

func testParseTwoLettersLocaleCustomPathForEnUSWithUnderscoreLocalization() {
XCTAssert(self.pathParser.parse("%two_letters_code%/Localizable.strings", localization: "en_US") == "US/Localizable.strings", "")
}

func testParseTwoLettersLocaleCustomPathForZhLocalization() {
XCTAssert(self.pathParser.parse("%two_letters_code%/Localizable.strings", localization: "zh") == "zh/Localizable.strings", "")
XCTAssert(self.pathParser.parse("%two_letters_code%/Localizable.strings", localization: "en_US") == "en/Localizable.strings", "")
}

func testParseTwoLettersLocaleCustomPathForZhHantLocalization() {
XCTAssert(self.pathParser.parse("%two_letters_code%/Localizable.strings", localization: "zh-Hant") == "zh-Hant/Localizable.strings", "")
XCTAssert(self.pathParser.parse("%two_letters_code%/Localizable.strings", localization: "zh-Hant") == "zh/Localizable.strings", "")
}

func testParseTwoLettersLocaleCustomPathForZhHansLocalization() {
XCTAssert(self.pathParser.parse("%two_letters_code%/Localizable.strings", localization: "zh-Hans") == "zh-Hans/Localizable.strings", "")
XCTAssert(self.pathParser.parse("%two_letters_code%/Localizable.strings", localization: "zh-Hans") == "zh/Localizable.strings", "")
}
}
2 changes: 1 addition & 1 deletion Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 039dfa317e86dfe524af85b8816460f689111a5a

COCOAPODS: 1.10.0
COCOAPODS: 1.10.1
4 changes: 4 additions & 0 deletions Tests/Tests.xcodeproj/xcshareddata/xcschemes/Tests.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@
BlueprintName = "CrowdinSDK-Unit-CrowdinProvider_Tests"
ReferencedContainer = "container:Pods/Pods.xcodeproj">
</BuildableReference>
<LocationScenarioReference
identifier = "com.apple.dt.IDEFoundation.CurrentLocationScenarioIdentifier"
referenceType = "1">
</LocationScenarioReference>
</TestableReference>
<TestableReference
skipped = "NO">
Expand Down