gristlabs_grist-core/app/common/Locales.ts
Jarosław Sadziński 6f00106d7c (core) Raw renames
Summary:
A new way for renaming tables.

  - There is a new popup to rename section (where you can also rename the table)
  - Renaming/Deleting page doesn't modify/delete the table.
  - Renaming table can rename a page if the names match (and the page contains a section with that table).
  - User can rename table in Raw Data UI in two ways - either on the listing or by using the section name popup
  - As before, there is no way to change tableId - it is derived from a table name.
  - When the section name is empty the table name is shown instead.
  - White space for section name is allowed (to discuss) - so the user can just paste '   '.
  - Empty name for a page is not allowed (but white space is).
  - Some bugs related to deleting tables with attached summary tables (and with undoing this operation) were fixed (but not all of them yet).

Test Plan: Updated tests.

Reviewers: georgegevoian

Reviewed By: georgegevoian

Subscribers: georgegevoian

Differential Revision: https://phab.getgrist.com/D3360
2022-04-27 22:21:55 +02:00

71 lines
2.4 KiB
TypeScript

import * as LocaleCurrencyMap from 'locale-currency/map';
import * as LocaleCurrency from 'locale-currency';
import {nativeCompare} from 'app/common/gutil';
import {localeCodes} from "app/common/LocaleCodes";
const DEFAULT_CURRENCY = "USD";
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 {
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);
const regionName = regionDisplay.of(locale.region);
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};
});
} catch {
// Fall back to using the locale code as the display name.
locales = localeCodes.map(code => ({name: code, code}));
}
export interface Currency {
name: string;
code: string;
}
export let currencies: Readonly<Currency[]>;
// 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) {
const currency = LocaleCurrency.getCurrency(code ?? 'en-US');
// Fallback to USD
return currency ?? DEFAULT_CURRENCY;
}
// Intl.DisplayNames is only supported on recent browsers, so proceed with caution.
try {
const currencyDisplay = new Intl.DisplayNames('en', {type: 'currency'});
currencies = [...new Set(currenciesCodes)].map(code => {
return {name: currencyDisplay.of(code), code};
});
} catch {
// Fall back to using the currency code as the display name.
currencies = [...new Set(currenciesCodes)].map(code => {
return {name: code, code};
});
}
currencies = [...currencies].sort((a, b) => nativeCompare(a.code, b.code));