2021-09-23 18:09:58 +00:00
|
|
|
import * as LocaleCurrencyMap from 'locale-currency/map';
|
|
|
|
import * as LocaleCurrency from 'locale-currency';
|
2021-08-26 16:35:11 +00:00
|
|
|
import {nativeCompare} from 'app/common/gutil';
|
2021-09-23 18:09:58 +00:00
|
|
|
import {localeCodes} from "app/common/LocaleCodes";
|
2021-08-26 16:35:11 +00:00
|
|
|
|
2021-09-23 18:09:58 +00:00
|
|
|
const DEFAULT_CURRENCY = "USD";
|
2021-08-26 16:35:11 +00:00
|
|
|
|
|
|
|
export interface Locale {
|
|
|
|
name: string;
|
|
|
|
code: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export let locales: Readonly<Locale[]>;
|
|
|
|
|
|
|
|
// Intl.DisplayNames is only supported on recent browsers, so proceed with caution.
|
|
|
|
try {
|
2021-09-23 18:09:58 +00:00
|
|
|
const regionDisplay = new Intl.DisplayNames('en', {type: 'region'});
|
|
|
|
const languageDisplay = new Intl.DisplayNames('en', {type: 'language'});
|
|
|
|
const display = (code: string) => {
|
|
|
|
try {
|
|
|
|
const locale = new Intl.Locale(code);
|
(core) Speed up and upgrade build.
Summary:
- Upgrades to build-related packages:
- Upgrade typescript, related libraries and typings.
- Upgrade webpack, eslint; add tsc-watch, node-dev, eslint_d.
- Build organization changes:
- Build webpack from original typescript, transpiling only; with errors still
reported by a background tsc watching process.
- Typescript-related changes:
- Reduce imports of AWS dependencies (very noticeable speedup)
- Avoid auto-loading global @types
- Client code is now built with isolatedModules flag (for safe transpilation)
- Use allowJs to avoid copying JS files manually.
- Linting changes
- Enhance Arcanist ESLintLinter to run before/after commands, and set up to use eslint_d
- Update eslint config, and include .eslintignore to avoid linting generated files.
- Include a bunch of eslint-prompted and eslint-generated fixes
- Add no-unused-expression rule to eslint, and fix a few warnings about it
- Other items:
- Refactor cssInput to avoid circular dependency
- Remove a bit of unused code, libraries, dependencies
Test Plan: No behavior changes, all existing tests pass. There are 30 tests fewer reported because `test_gpath.py` was removed (it's been unused for years)
Reviewers: paulfitz
Reviewed By: paulfitz
Subscribers: paulfitz
Differential Revision: https://phab.getgrist.com/D3498
2022-06-27 20:09:41 +00:00
|
|
|
const regionName = regionDisplay.of(locale.region!);
|
2021-09-23 18:09:58 +00:00
|
|
|
const languageName = languageDisplay.of(locale.language);
|
|
|
|
return `${regionName} (${languageName})`;
|
|
|
|
} catch (ex) {
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Leave only those that are supported by current system (can be translated to human readable form).
|
|
|
|
// Though, this file is in common, it is safe to filter by current system
|
|
|
|
// as the list should be already filtered by codes that are supported by the backend.
|
|
|
|
locales = Intl.DisplayNames.supportedLocalesOf(localeCodes).map(code => {
|
|
|
|
return {name: display(code), code};
|
2021-08-26 16:35:11 +00:00
|
|
|
});
|
|
|
|
} catch {
|
|
|
|
// Fall back to using the locale code as the display name.
|
2021-09-23 18:09:58 +00:00
|
|
|
locales = localeCodes.map(code => ({name: code, code}));
|
2021-08-26 16:35:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface Currency {
|
|
|
|
name: string;
|
|
|
|
code: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export let currencies: Readonly<Currency[]>;
|
|
|
|
|
2021-09-23 18:09:58 +00:00
|
|
|
// locale-currency package doesn't have South Sudanese pound currency or a default value for Kosovo
|
|
|
|
LocaleCurrencyMap["SS"] = "SSP";
|
|
|
|
LocaleCurrencyMap["XK"] = "EUR";
|
|
|
|
const currenciesCodes = Object.values(LocaleCurrencyMap);
|
|
|
|
export function getCurrency(code: string) {
|
2022-04-27 17:46:24 +00:00
|
|
|
const currency = LocaleCurrency.getCurrency(code ?? 'en-US');
|
2021-09-23 18:09:58 +00:00
|
|
|
// Fallback to USD
|
|
|
|
return currency ?? DEFAULT_CURRENCY;
|
|
|
|
}
|
|
|
|
|
2021-08-26 16:35:11 +00:00
|
|
|
// Intl.DisplayNames is only supported on recent browsers, so proceed with caution.
|
|
|
|
try {
|
|
|
|
const currencyDisplay = new Intl.DisplayNames('en', {type: 'currency'});
|
2021-09-23 18:09:58 +00:00
|
|
|
currencies = [...new Set(currenciesCodes)].map(code => {
|
(core) Speed up and upgrade build.
Summary:
- Upgrades to build-related packages:
- Upgrade typescript, related libraries and typings.
- Upgrade webpack, eslint; add tsc-watch, node-dev, eslint_d.
- Build organization changes:
- Build webpack from original typescript, transpiling only; with errors still
reported by a background tsc watching process.
- Typescript-related changes:
- Reduce imports of AWS dependencies (very noticeable speedup)
- Avoid auto-loading global @types
- Client code is now built with isolatedModules flag (for safe transpilation)
- Use allowJs to avoid copying JS files manually.
- Linting changes
- Enhance Arcanist ESLintLinter to run before/after commands, and set up to use eslint_d
- Update eslint config, and include .eslintignore to avoid linting generated files.
- Include a bunch of eslint-prompted and eslint-generated fixes
- Add no-unused-expression rule to eslint, and fix a few warnings about it
- Other items:
- Refactor cssInput to avoid circular dependency
- Remove a bit of unused code, libraries, dependencies
Test Plan: No behavior changes, all existing tests pass. There are 30 tests fewer reported because `test_gpath.py` was removed (it's been unused for years)
Reviewers: paulfitz
Reviewed By: paulfitz
Subscribers: paulfitz
Differential Revision: https://phab.getgrist.com/D3498
2022-06-27 20:09:41 +00:00
|
|
|
return {name: currencyDisplay.of(code)!, code};
|
2021-08-26 16:35:11 +00:00
|
|
|
});
|
|
|
|
} catch {
|
|
|
|
// Fall back to using the currency code as the display name.
|
2021-09-23 18:09:58 +00:00
|
|
|
currencies = [...new Set(currenciesCodes)].map(code => {
|
|
|
|
return {name: code, code};
|
2021-08-26 16:35:11 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
currencies = [...currencies].sort((a, b) => nativeCompare(a.code, b.code));
|
2023-01-24 13:13:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
export function getCountryCode(locale: string) {
|
|
|
|
// We have some defaults defined.
|
|
|
|
if (locale === 'en') { return 'US'; }
|
|
|
|
let countryCode = locale.split(/[-_]/)[1];
|
|
|
|
if (countryCode) { return countryCode.toUpperCase(); }
|
|
|
|
countryCode = locale.toUpperCase();
|
|
|
|
// Test if we can use language as a country code.
|
|
|
|
if (localeCodes.map(code => code.split(/[-_]/)[1]).includes(countryCode)) {
|
|
|
|
return countryCode;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|