Skip to content

Commit

Permalink
fix: Fix Enable / Disable Locale matching logic. (#1318)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Sep 18, 2021
1 parent b288758 commit b745e39
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/client/src/client/server/serverSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function normalizeLocale(locale: string | string[] = ''): string {

export function normalizeToLocales(locale: string = ''): string[] {
return locale
.replace(/[|;]/g, ',')
.replace(/[|;\s]/g, ',')
.replace(/[*]/g, '')
.split(',')
.map(normalizeCode)
Expand Down
23 changes: 22 additions & 1 deletion packages/client/src/settings/settings.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,28 @@
import { hasWorkspaceLocation } from './settings';
import { hasWorkspaceLocation, addLocaleToCurrentLocale, removeLocaleFromCurrentLocale } from './settings';

describe('Validate settings.ts', () => {
test('hasWorkspaceLocation', () => {
expect(hasWorkspaceLocation()).toBe(false);
});

test.each`
locale | current | expected
${'en'} | ${'en'} | ${'en'}
${'nl_nl'} | ${'en'} | ${'en,nl-NL'}
${'nl,nl_nl,nl-Nl'} | ${'en'} | ${'en,nl,nl-NL'}
`('addLocaleToCurrentLocale $locale + $current => $expected', ({ locale, current, expected }) => {
expect(addLocaleToCurrentLocale(locale, current)).toBe(expected);
});

test.each`
locale | current | expected
${'en'} | ${'en'} | ${undefined}
${'nl_nl'} | ${'en'} | ${'en'}
${'nl_nl'} | ${'en,nl-NL'} | ${'en'}
${'nl,nl_nl'} | ${'en,nl'} | ${'en'}
${'en;nl_nl'} | ${'en,nl'} | ${'nl'}
${'en;nl_nl'} | ${'en, nl'} | ${'nl'}
`('removeLocaleFromCurrentLocale $current - $locale => $expected', ({ locale, current, expected }) => {
expect(removeLocaleFromCurrentLocale(locale, current)).toBe(expected);
});
});
40 changes: 26 additions & 14 deletions packages/client/src/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,8 @@ export function disableLocale(targets: ClientConfigTarget[], locale: string): Pr

export function enableLocaleForTarget(locale: string, enable: boolean, targets: ClientConfigTarget[]): Promise<void> {
const applyFn: (src: string | undefined) => string | undefined = enable
? (currentLanguage) =>
unique(
normalizeLocale(currentLanguage || 'en')
.split(',')
.concat(locale.split(','))
)
.filter((a) => !!a)
.join(',')
: (currentLanguage) => {
const value = unique(normalizeLocale(currentLanguage).split(','))
.filter((lang) => lang !== locale)
.join(',');
return value || undefined;
};
? (currentLanguage) => addLocaleToCurrentLocale(locale, currentLanguage)
: (currentLanguage) => removeLocaleFromCurrentLocale(locale, currentLanguage);
return setConfigFieldQuickPick(targets, 'language', applyFn);
}

Expand Down Expand Up @@ -263,3 +251,27 @@ export async function createConfigFileRelativeToDocumentUri(referenceDocUri?: Ur
await createConfigFile(configFile, overwrite);
return configFile;
}

function normalize(locale: string) {
return normalizeLocale(locale)
.split(',')
.filter((a) => !!a);
}

export function addLocaleToCurrentLocale(locale: string, currentLocale: string | undefined): string | undefined {
const toAdd = normalize(locale);
const currentSet = new Set(normalize(currentLocale || ''));

toAdd.forEach((locale) => currentSet.add(locale));

return [...currentSet].join(',') || undefined;
}

export function removeLocaleFromCurrentLocale(locale: string, currentLocale: string | undefined): string | undefined {
const toRemove = normalize(locale);
const currentSet = new Set(normalize(currentLocale || ''));

toRemove.forEach((locale) => currentSet.delete(locale));

return [...currentSet].join(',') || undefined;
}

0 comments on commit b745e39

Please sign in to comment.