Skip to content

Commit

Permalink
Rollback some changes
Browse files Browse the repository at this point in the history
  • Loading branch information
zoontek committed Oct 20, 2019
1 parent d9e1de4 commit f9d5eb5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 63 deletions.
Expand Up @@ -177,18 +177,6 @@ private void emitLocalizationDidChange() {
}
}

private @Nullable String getFirstCountryCode(@Nonnull List<Locale> locales) {
for (int i = 0; i < locales.size(); i++) {
String countryCode = getCountryCode(locales.get(i));

if (countryCode != null) {
return countryCode;
}
}

return null;
}

private @Nullable String getCurrencyCode(@Nonnull Locale locale) {
try {
Currency currency = Currency.getInstance(locale);
Expand Down Expand Up @@ -244,11 +232,11 @@ private boolean getUsesAutoTimeZone() {

private @Nonnull WritableMap getExported() {
List<Locale> deviceLocales = getLocales();
Locale firstLocale = deviceLocales.get(0);
String firstCountryCode = getFirstCountryCode(deviceLocales);
Locale currentLocale = deviceLocales.get(0);
String currentCountryCode = getCountryCode(currentLocale);

if (firstCountryCode == null) {
firstCountryCode = "US";
if (currentCountryCode == null) {
currentCountryCode = "US";
}

List<String> languageTagsList = new ArrayList<>();
Expand All @@ -263,7 +251,7 @@ private boolean getUsesAutoTimeZone() {
String currencyCode = getCurrencyCode(deviceLocale);

if (countryCode == null) {
countryCode = firstCountryCode;
countryCode = currentCountryCode;
}

String languageTag = createLanguageTag(languageCode, scriptCode, countryCode);
Expand Down Expand Up @@ -296,16 +284,16 @@ private boolean getUsesAutoTimeZone() {
WritableMap exported = Arguments.createMap();

exported.putString("calendar", "gregorian");
exported.putString("country", firstCountryCode);
exported.putString("country", currentCountryCode);
exported.putArray("currencies", currencies);
exported.putArray("locales", locales);
exported.putMap("numberFormatSettings", getNumberFormatSettings(firstLocale));
exported.putString("temperatureUnit", USES_FAHRENHEIT.contains(firstCountryCode) ? "fahrenheit" : "celsius");
exported.putMap("numberFormatSettings", getNumberFormatSettings(currentLocale));
exported.putString("temperatureUnit", USES_FAHRENHEIT.contains(currentCountryCode) ? "fahrenheit" : "celsius");
exported.putString("timeZone", TimeZone.getDefault().getID());
exported.putBoolean("uses24HourClock", DateFormat.is24HourFormat(getReactApplicationContext()));
exported.putBoolean("usesAutoDateAndTime", getUsesAutoDateAndTime());
exported.putBoolean("usesAutoTimeZone", getUsesAutoTimeZone());
exported.putBoolean("usesMetricSystem", !USES_IMPERIAL.contains(firstCountryCode));
exported.putBoolean("usesMetricSystem", !USES_IMPERIAL.contains(currentCountryCode));

return exported;
}
Expand Down
45 changes: 19 additions & 26 deletions ios/RNLocalize.m
Expand Up @@ -31,18 +31,6 @@
return countryCode;
}

static NSString * _Nonnull getFirstCountryCode(NSArray<NSLocale *> * _Nonnull locales) {
for (NSLocale *locale in locales) {
NSString *countryCode = getCountryCode(locale);

if (countryCode != nil) {
return countryCode;
}
}

return @"US";
}

static NSString * _Nonnull createLanguageTag(NSString * _Nonnull languageCode,
NSString * _Nullable scriptCode,
NSString * _Nonnull countryCode) {
Expand Down Expand Up @@ -109,28 +97,33 @@ static bool getUsesMetricSystem(NSLocale * _Nonnull locale) {
}

static NSDictionary * _Nonnull getExported() {
NSMutableArray<NSLocale *> *deviceLocales = [NSMutableArray array];
NSLocale *currentLocale = [NSLocale autoupdatingCurrentLocale];
NSString *currentCountryCode = getCountryCode(currentLocale);
NSString *currentCurrency = [currentLocale objectForKey:NSLocaleCurrencyCode];

for (NSString *identifier in [NSLocale preferredLanguages]) {
[deviceLocales addObject:[[NSLocale alloc] initWithLocaleIdentifier:identifier]];
if (currentCountryCode == nil) {
currentCountryCode = @"US"; // only happen in the simulator
}

NSLocale *firstLocale = [deviceLocales objectAtIndex:0];
NSString *firstCountryCode = getFirstCountryCode(deviceLocales);

NSMutableArray<NSString *> *languageTagsList = [NSMutableArray array];
NSMutableArray<NSDictionary *> *locales = [NSMutableArray array];
NSMutableArray<NSString *> *currencies = [NSMutableArray array];

for (NSLocale *deviceLocale in deviceLocales) {
if (currentCurrency != nil) {
[currencies addObject:currentCurrency];
}

for (NSString *identifier in [NSLocale preferredLanguages]) {
NSLocale *deviceLocale = [[NSLocale alloc] initWithLocaleIdentifier:identifier];

NSString *languageCode = [deviceLocale objectForKey:NSLocaleLanguageCode];
NSString *scriptCode = [deviceLocale objectForKey:NSLocaleScriptCode];
NSString *countryCode = getCountryCode(deviceLocale);
NSString *currencyCode = [deviceLocale objectForKey:NSLocaleCurrencyCode];
bool isRTL = [NSLocale characterDirectionForLanguage:languageCode] == NSLocaleLanguageDirectionRightToLeft;

if (countryCode == nil) {
countryCode = firstCountryCode;
countryCode = currentCountryCode;
}

NSString *languageTag = createLanguageTag(languageCode, scriptCode, countryCode);
Expand Down Expand Up @@ -161,15 +154,15 @@ static bool getUsesMetricSystem(NSLocale * _Nonnull locale) {
}

return @{
@"calendar": getCalendar(firstLocale),
@"country": firstCountryCode,
@"calendar": getCalendar(currentLocale),
@"country": currentCountryCode,
@"currencies": currencies,
@"locales": locales,
@"numberFormatSettings": getNumberFormatSettings(firstLocale),
@"temperatureUnit": getTemperatureUnit(firstLocale, firstCountryCode),
@"numberFormatSettings": getNumberFormatSettings(currentLocale),
@"temperatureUnit": getTemperatureUnit(currentLocale, currentCountryCode),
@"timeZone": [[NSTimeZone localTimeZone] name],
@"uses24HourClock": @(getUses24HourClock(firstLocale)),
@"usesMetricSystem": @(getUsesMetricSystem(firstLocale)),
@"uses24HourClock": @(getUses24HourClock(currentLocale)),
@"usesMetricSystem": @(getUsesMetricSystem(currentLocale)),
};
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "react-native-localize",
"version": "1.4.0",
"version": "1.3.1",
"license": "MIT",
"description": "A toolbox for your React Native app localization.",
"author": "Mathieu Acthernoene <zoontek@gmail.com>",
Expand Down
28 changes: 13 additions & 15 deletions src/module.web.js
Expand Up @@ -11,9 +11,7 @@ import type { Locale, LocalizationConstants } from "./types";

function getCountryCode(languageTagParts: string[]): ?string {
// overwrite Latin America and Caribbean region
return languageTagParts[1] === "419"
? (languageTagParts[1] = "UN")
: languageTagParts[1];
return languageTagParts[1] === "419" ? "UN" : languageTagParts[1];
}

function getLocaleFromLanguageTag(
Expand All @@ -32,25 +30,25 @@ function getLocaleFromLanguageTag(
};
}

function generateConstants(
languageTags: $ReadOnlyArray<string>,
): LocalizationConstants {
let firstCountryCode = "US";

function getFirstCountryCode(languageTags: $ReadOnlyArray<string>): ?string {
for (let i = 0; i < languageTags.length; i++) {
const countryCode = getCountryCode(languageTags[i].split("-"));

if (countryCode) {
firstCountryCode = countryCode;
break;
return countryCode;
}
}
}

const currencies: string[] = [];
function generateConstants(
languageTags: $ReadOnlyArray<string>,
): LocalizationConstants {
const countryCode = getFirstCountryCode(languageTags);
const locales: Locale[] = [];
const currencies: string[] = [];

languageTags.forEach(languageTag => {
const locale = getLocaleFromLanguageTag(languageTag, firstCountryCode);
const locale = getLocaleFromLanguageTag(languageTag, countryCode);
const currency = CURRENCIES[locale.countryCode];

if (!locales.find(_ => _.languageTag === locale.languageTag)) {
Expand Down Expand Up @@ -84,16 +82,16 @@ function generateConstants(

return {
calendar: "gregorian",
country: firstCountryCode,
country: countryCode,
currencies,
locales,
numberFormatSettings,
temperatureUnit: USES_FAHRENHEIT.includes(firstCountryCode)
temperatureUnit: USES_FAHRENHEIT.includes(countryCode)
? "fahrenheit"
: "celsius",
timeZone: dateFormatter.resolvedOptions().timeZone || "Etc/UTC",
uses24HourClock,
usesMetricSystem: !USES_IMPERIAL.includes(firstCountryCode),
usesMetricSystem: !USES_IMPERIAL.includes(countryCode),
};
}

Expand Down

0 comments on commit f9d5eb5

Please sign in to comment.