Skip to content

Commit 985d9fe

Browse files
authored
Merge pull request #1635 from session-foundation/fix-locale-underscore-pos
fix: use first part of the locale with _/- in it as a full locale
2 parents 9c74d27 + 068ea17 commit 985d9fe

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

ts/mains/main_node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ import { setLatestRelease } from '../node/latest_desktop_release';
189189
import { isDevProd, isTestIntegration } from '../shared/env_vars';
190190
import { classicDark } from '../themes';
191191

192-
import { isSessionLocaleSet, getCrowdinLocale } from '../util/i18n/shared';
192+
import { isSessionLocaleSet, getCrowdinLocale, keepFullLocalePart } from '../util/i18n/shared';
193193
import { loadLocalizedDictionary } from '../node/locale';
194194
import { simpleDictionaryNoArgs } from '../localization/locales';
195195
import LIBSESSION_CONSTANTS from '../session/utils/libsession/libsession_constants';
@@ -774,7 +774,7 @@ app.on('ready', async () => {
774774
);
775775

776776
if (!isSessionLocaleSet()) {
777-
const appLocale = process.env.LANGUAGE || app.getLocale() || 'en';
777+
const appLocale = keepFullLocalePart(process.env.LANGUAGE || app.getLocale() || 'en');
778778
const loadedLocale = loadLocalizedDictionary({ appLocale });
779779
console.log(`appLocale is ${appLocale}`);
780780
console.log(`crowdin locale is ${loadedLocale.crowdinLocale}`);

ts/node/locale.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { setupI18n } from '../util/i18n/i18n';
22
import { CrowdinLocale, isCrowdinLocale } from '../localization/constants';
3+
import { keepFullLocalePart } from '../util/i18n/shared';
34

45
export function normalizeLocaleName(locale: string) {
5-
const dashedLocale = locale.replaceAll('_', '-');
6+
const dashedLocale = keepFullLocalePart(locale).replaceAll('_', '-');
67

78
// Note: this is a pain, but we somehow needs to keep in sync this logic and the LOCALE_PATH_MAPPING from
89
// https://github.com/session-foundation/session-shared-scripts/blob/main/crowdin/generate_desktop_strings.py

ts/node/spell_check.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { type BrowserWindow, Menu } from 'electron';
22
import { sync as osLocaleSync } from 'os-locale';
33
import { tr } from '../localization/localeTools';
4+
import { keepFullLocalePart } from '../util/i18n/shared';
45

56
export const setup = (browserWindow: BrowserWindow) => {
67
const { session } = browserWindow.webContents;
78
// NOTE: we do not rely on the locale parsed by node here because we want
89
// to support a broader list of spell checks than what the app is localised for.
910
// For instance: en_AU is not a supported language on crowdin, but we still want the user to
1011
// - if they have the dictionary installed for it - we should be able to spell check "esky", "arvo" or "bogan"
11-
const userLocale = process.env.LANGUAGE
12-
? process.env.LANGUAGE
13-
: osLocaleSync().replace(/_/g, '-');
12+
const userLocale = keepFullLocalePart(process.env.LANGUAGE || osLocaleSync().replace(/_/g, '-'));
1413
const userLocales = [userLocale, userLocale.split('-')[0], userLocale.split('_')[0]];
1514

1615
const available = session.availableSpellCheckerLanguages;

ts/util/i18n/shared.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,29 @@ export function getTimeLocaleDictionary() {
2020
return (timeLocaleMap as Record<string, Locale>)[getBrowserLocale()] || timeLocaleMap.en;
2121
}
2222

23+
/**
24+
* Some users have a locale setup with a ':' in it.
25+
* When this happens, the full locale (i.e. the part with the "_") can sometimes be on the left, sometimes on the right.
26+
* This function will always return the part with the "_" if found, otherwise the full locale.
27+
*/
28+
export function keepFullLocalePart(locale: string) {
29+
const firstUnderscore = locale.indexOf('_');
30+
const firstDash = locale.indexOf('-');
31+
const firstSemiColon = locale.indexOf(':');
32+
if (firstSemiColon === -1) {
33+
return locale;
34+
}
35+
const firstUnderscoreOrDash = Math.max(firstUnderscore, firstDash);
36+
37+
if (firstSemiColon > firstUnderscoreOrDash) {
38+
// the semicolon is after the underscore, so we return the start of the string (de_DE:en we return de_DE)
39+
return locale.substring(0, firstSemiColon);
40+
}
41+
42+
// the semicolon is before the underscore, so we return the end of the string (in en:de_DE wew return de_DE)
43+
return locale.substring(firstSemiColon + 1);
44+
}
45+
2346
/**
2447
* Returns the current locale as supported by Session (i.e. one generated by crowdin)
2548
*/
@@ -36,7 +59,7 @@ export function getCrowdinLocale(): CrowdinLocale {
3659
* Returns the closest supported locale by the browser.
3760
*/
3861
export function getBrowserLocale() {
39-
const browserLocale = process.env.LANGUAGE || getCrowdinLocale() || 'en';
62+
const browserLocale = keepFullLocalePart(process.env.LANGUAGE || getCrowdinLocale() || 'en');
4063

4164
// supportedLocalesOf will throw if the locales has a '_' instead of a '-' in it.
4265
const userLocaleDashed = browserLocale.replaceAll('_', '-');

0 commit comments

Comments
 (0)