Skip to content

Commit 9f81b3d

Browse files
authored
I18n: Change locale format to IETF language tag (grafana#52596)
* I18n: Change locale format to our agreed format * fix test * Build lingui messages files before ci tests
1 parent 1ee21b8 commit 9f81b3d

File tree

9 files changed

+38
-22
lines changed

9 files changed

+38
-22
lines changed

Diff for: .linguirc

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"locales": [
3-
"en",
4-
"fr",
5-
"es",
3+
"en-US",
4+
"fr-FR",
5+
"es-ES",
66
"pseudo-LOCALE"
77
],
88
"catalogs": [
@@ -20,11 +20,11 @@
2020
}
2121
],
2222
"fallbackLocales": {
23-
"pseudo-LOCALE": "en",
24-
"default": "en"
23+
"pseudo-LOCALE": "en-US",
24+
"default": "en-US"
2525
},
2626
"pseudoLocale": "pseudo-LOCALE",
27-
"sourceLocale": "en",
27+
"sourceLocale": "en-US",
2828
"format": "po",
2929
"formatOptions": {
3030
"lineNumbers": false

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"lint": "yarn run lint:ts && yarn run lint:sass",
2222
"lint:ts": "eslint . --ext .js,.tsx,.ts --cache",
2323
"lint:sass": "yarn stylelint '{public/sass,packages}/**/*.scss' --cache",
24-
"test:ci": "mkdir -p reports/junit && JEST_JUNIT_OUTPUT_DIR=reports/junit jest --ci --reporters=default --reporters=jest-junit -w ${TEST_MAX_WORKERS:-100%}",
24+
"test:ci": "yarn i18n:compile && mkdir -p reports/junit && JEST_JUNIT_OUTPUT_DIR=reports/junit jest --ci --reporters=default --reporters=jest-junit -w ${TEST_MAX_WORKERS:-100%}",
2525
"lint:fix": "yarn lint:ts --fix",
2626
"packages:build": "lerna run clean && lerna run build --ignore @grafana-plugins/input-datasource",
2727
"packages:docsExtract": "rm -rf ./reports/docs && lerna run docsExtract",

Diff for: public/app/core/components/SharedPreferences/SharedPreferences.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ describe('SharedPreferences', () => {
175175
queryHistory: {
176176
homeTab: '',
177177
},
178-
locale: 'fr',
178+
locale: 'fr-FR',
179179
});
180180
});
181181

Diff for: public/app/core/components/SharedPreferences/SharedPreferences.tsx

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
WeekStartPicker,
2121
FeatureBadge,
2222
} from '@grafana/ui';
23+
import { ENGLISH_US, FRENCH_FRANCE, SPANISH_SPAIN } from 'app/core/internationalization/constants';
2324
import { PreferencesService } from 'app/core/services/PreferencesService';
2425
import { backendSrv } from 'app/core/services/backend_srv';
2526
import { DashboardSearchItem, DashboardSearchItemType } from 'app/features/search/types';
@@ -50,21 +51,21 @@ const languages: Array<SelectableValue<string>> = [
5051
}),
5152
},
5253
{
53-
value: 'en',
54+
value: ENGLISH_US,
5455
label: t({
5556
id: 'common.locale.en',
5657
message: 'English',
5758
}),
5859
},
5960
{
60-
value: 'es',
61+
value: SPANISH_SPAIN,
6162
label: t({
6263
id: 'common.locale.es',
6364
message: 'Spanish',
6465
}),
6566
},
6667
{
67-
value: 'fr',
68+
value: FRENCH_FRANCE,
6869
label: t({
6970
id: 'common.locale.fr',
7071
message: 'French',

Diff for: public/app/core/internationalization/constants.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
type LocaleIdentifier = `${string}-${string}`;
2+
3+
export const ENGLISH_US: LocaleIdentifier = 'en-US';
4+
export const FRENCH_FRANCE: LocaleIdentifier = 'fr-FR';
5+
export const SPANISH_SPAIN: LocaleIdentifier = 'es-ES';
6+
7+
export const DEFAULT_LOCALE: LocaleIdentifier = ENGLISH_US;
8+
9+
export const VALID_LOCALES: LocaleIdentifier[] = [ENGLISH_US, FRENCH_FRANCE, SPANISH_SPAIN];

Diff for: public/app/core/internationalization/index.tsx

+17-11
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,45 @@ import React, { useEffect } from 'react';
44

55
import config from 'app/core/config';
66

7-
import { messages } from '../../../locales/en/messages';
7+
import { messages as fallbackMessages } from '../../../locales/en-US/messages';
8+
9+
import { DEFAULT_LOCALE, FRENCH_FRANCE, SPANISH_SPAIN, VALID_LOCALES } from './constants';
810

911
let i18nInstance: I18n;
1012

11-
export async function getI18n(locale = 'en') {
13+
export async function getI18n(localInput = DEFAULT_LOCALE) {
1214
if (i18nInstance) {
1315
return i18nInstance;
1416
}
17+
18+
const validatedLocale = VALID_LOCALES.includes(localInput) ? localInput : DEFAULT_LOCALE;
19+
1520
// Dynamically load the messages for the user's locale
1621
const imp =
1722
config.featureToggles.internationalization &&
18-
(await import(`../../../locales/${locale}/messages`).catch((err) => {
23+
(await import(`../../../locales/${validatedLocale}/messages`).catch((err) => {
1924
// TODO: Properly return an error if we can't find the messages for a locale
2025
return err;
2126
}));
22-
i18n.load(locale, imp?.messages || messages);
27+
28+
i18n.load(validatedLocale, imp?.messages || fallbackMessages);
2329

2430
// Browser support for Intl.PluralRules is good and covers what we support in .browserlistrc,
2531
// but because this could potentially be in a the critical path of loading the frontend lets
2632
// be extra careful
2733
// If this isnt loaded, Lingui will log a warning and plurals will not be translated correctly.
2834
const supportsPluralRules = 'Intl' in window && 'PluralRules' in Intl;
2935
if (supportsPluralRules) {
30-
const pluralsOrdinal = new Intl.PluralRules(locale, { type: 'ordinal' });
31-
const pluralsCardinal = new Intl.PluralRules(locale, { type: 'cardinal' });
32-
i18n.loadLocaleData(locale, {
36+
const pluralsOrdinal = new Intl.PluralRules(validatedLocale, { type: 'ordinal' });
37+
const pluralsCardinal = new Intl.PluralRules(validatedLocale, { type: 'cardinal' });
38+
i18n.loadLocaleData(validatedLocale, {
3339
plurals(count: number, ordinal: boolean) {
3440
return (ordinal ? pluralsOrdinal : pluralsCardinal).select(count);
3541
},
3642
});
3743
}
3844

39-
i18n.activate(locale);
45+
i18n.activate(validatedLocale);
4046
i18nInstance = i18n;
4147

4248
return i18nInstance;
@@ -52,13 +58,13 @@ export function I18nProvider({ children }: I18nProviderProps) {
5258
// TODO: Use locale preference instead of weekStart
5359
switch (config.bootData.user.weekStart) {
5460
case 'saturday':
55-
loc = 'es';
61+
loc = SPANISH_SPAIN;
5662
break;
5763
case 'sunday':
58-
loc = 'fr';
64+
loc = FRENCH_FRANCE;
5965
break;
6066
default:
61-
loc = 'en';
67+
loc = DEFAULT_LOCALE;
6268
break;
6369
}
6470
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)