Skip to content

Commit

Permalink
Merge pull request #21 from transifex/fix-default-locale-provider
Browse files Browse the repository at this point in the history
Preferred locale provider returns correct language code
  • Loading branch information
stelabouras authored and Dimitrios Bendilas committed Feb 24, 2021
2 parents 16a89e9 + dc54c4f commit adb327d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Transifex iOS SDK 0.1.0

*Febrary 4, 2021*
*February 4, 2021*

- Public release

## Transifex iOS SDK 0.1.1

*February 24, 2021*

- Fixed preferred locale provider
2 changes: 1 addition & 1 deletion Sources/Transifex/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ Error rendering source string '\(sourceString)' with string to render '\(stringT
/// A static class that is the main point of entry for all the functionality of Transifex Native throughout the SDK.
public final class TXNative : NSObject {
/// The SDK version
internal static let version = "0.1.0"
internal static let version = "0.1.1"

/// The filename of the file that holds the translated strings and it's bundled inside the app.
public static let STRINGS_FILENAME = "txstrings.json"
Expand Down
9 changes: 8 additions & 1 deletion Sources/Transifex/Locales.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,15 @@ public final class TXPreferredLocaleProvider : NSObject {
_currentLocale = TXPreferredLocaleProvider.getCurrentLocale()
}

private static func getPreferredLocale() -> Locale {
guard let preferredIdentifier = Locale.preferredLanguages.first else {
return Locale.autoupdatingCurrent
}
return Locale(identifier: preferredIdentifier)
}

private static func getCurrentLocale() -> String {
return NSLocale.autoupdatingCurrent.languageCode ?? "en"
return getPreferredLocale().languageCode ?? "en"
}
}

Expand Down
19 changes: 19 additions & 0 deletions Tests/TransifexTests/TransifexTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,22 @@ final class TransifexTests: XCTestCase {
XCTAssertEqual(result, "ERROR")
}

func testCurrentLocale() {
let appleLanguagesKey = "AppleLanguages"
let storedLanguages = UserDefaults.standard.value(forKey: appleLanguagesKey)

UserDefaults.standard.set([ "el" ],
forKey: appleLanguagesKey)

let locale = TXLocaleState(appLocales: [])

XCTAssertEqual(locale.currentLocale,
"el")

UserDefaults.standard.set(storedLanguages,
forKey: appleLanguagesKey)
}

static var allTests = [
("testDuplicateLocaleFiltering", testDuplicateLocaleFiltering),
("testCurrentLocaleProvider", testCurrentLocaleProvider),
Expand All @@ -463,5 +479,8 @@ final class TransifexTests: XCTestCase {
("testOverrideFilterCacheAll", testOverrideFilterCacheAll),
("testOverrideFilterCacheUntranslated", testOverrideFilterCacheUntranslated),
("testOverrideFilterCacheTranslated", testOverrideFilterCacheTranslated),
("testPlatformStrategyWithInvalidSourceString", testPlatformStrategyWithInvalidSourceString),
("testErrorPolicy", testErrorPolicy),
("testCurrentLocale", testCurrentLocale),
]
}

0 comments on commit adb327d

Please sign in to comment.