fix(i18n): return the correct locale from filenames (#5922)

This commit is contained in:
Andi Pabst 2021-10-25 12:19:13 +02:00 committed by GitHub
parent bf8b94f011
commit 6a42f98b51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 2 deletions

View File

@ -207,6 +207,36 @@ describe('i18n', () => {
});
});
describe('getLocaleFromPath', () => {
it('should return the locale from folder name in the path when structure is I18N_STRUCTURE.MULTIPLE_FOLDERS', () => {
expect(
i18n.getLocaleFromPath(
i18n.I18N_STRUCTURE.MULTIPLE_FOLDERS,
'md',
'src/content/en/index.md',
),
).toEqual('en');
});
it('should return the locale extension from the file name when structure is I18N_STRUCTURE.MULTIPLE_FILES', () => {
expect(
i18n.getLocaleFromPath(i18n.I18N_STRUCTURE.MULTIPLE_FILES, 'md', 'src/content/index.en.md'),
).toEqual('en');
});
it('issue #5909: return the correct locale extension for language gd', () => {
expect(
i18n.getLocaleFromPath(i18n.I18N_STRUCTURE.MULTIPLE_FILES, 'md', 'src/content/index.gd.md'),
).toEqual('gd');
});
it('should return an empty string when structure is I18N_STRUCTURE.SINGLE_FILE', () => {
expect(
i18n.getLocaleFromPath(i18n.I18N_STRUCTURE.SINGLE_FILE, 'md', 'src/content/index.md'),
).toEqual('');
});
});
describe('getI18nFiles', () => {
const locales = ['en', 'de', 'fr'];
const default_locale = 'en';

View File

@ -1,5 +1,5 @@
import { Map, List } from 'immutable';
import { set, trimEnd, groupBy, escapeRegExp } from 'lodash';
import { set, groupBy, escapeRegExp } from 'lodash';
import { selectEntrySlug } from '../reducers/collections';
@ -98,7 +98,7 @@ export function getLocaleFromPath(structure: I18N_STRUCTURE, extension: string,
return parts.pop();
}
case I18N_STRUCTURE.MULTIPLE_FILES: {
const parts = trimEnd(path, `.${extension}`);
const parts = path.slice(0, -`.${extension}`.length);
return parts.split('.').pop();
}
case I18N_STRUCTURE.SINGLE_FILE: