fix: i18n regex logic (#5136)
This commit is contained in:
parent
826d82fd80
commit
b0c0ea60c2
@ -130,23 +130,40 @@ describe('i18n', () => {
|
||||
});
|
||||
|
||||
describe('getFilePath', () => {
|
||||
const args = ['md', 'src/content/index.md', 'index', 'de'];
|
||||
it('should return directory path based on locale when structure is I18N_STRUCTURE.MULTIPLE_FOLDERS', () => {
|
||||
expect(i18n.getFilePath(i18n.I18N_STRUCTURE.MULTIPLE_FOLDERS, ...args)).toEqual(
|
||||
'src/content/de/index.md',
|
||||
);
|
||||
expect(
|
||||
i18n.getFilePath(
|
||||
i18n.I18N_STRUCTURE.MULTIPLE_FOLDERS,
|
||||
'md',
|
||||
'src/content/index.md',
|
||||
'index',
|
||||
'de',
|
||||
),
|
||||
).toEqual('src/content/de/index.md');
|
||||
});
|
||||
|
||||
it('should return file path based on locale when structure is I18N_STRUCTURE.MULTIPLE_FILES', () => {
|
||||
expect(i18n.getFilePath(i18n.I18N_STRUCTURE.MULTIPLE_FILES, ...args)).toEqual(
|
||||
'src/content/index.de.md',
|
||||
);
|
||||
expect(
|
||||
i18n.getFilePath(
|
||||
i18n.I18N_STRUCTURE.MULTIPLE_FILES,
|
||||
'md',
|
||||
'src/content/file-with-md-in-the-name.md',
|
||||
'file-with-md-in-the-name',
|
||||
'de',
|
||||
),
|
||||
).toEqual('src/content/file-with-md-in-the-name.de.md');
|
||||
});
|
||||
|
||||
it('should not modify path when structure is I18N_STRUCTURE.SINGLE_FILE', () => {
|
||||
expect(i18n.getFilePath(i18n.I18N_STRUCTURE.SINGLE_FILE, ...args)).toEqual(
|
||||
'src/content/index.md',
|
||||
);
|
||||
expect(
|
||||
i18n.getFilePath(
|
||||
i18n.I18N_STRUCTURE.SINGLE_FILE,
|
||||
'md',
|
||||
'src/content/index.md',
|
||||
'index',
|
||||
'de',
|
||||
),
|
||||
).toEqual('src/content/index.md');
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Map, List } from 'immutable';
|
||||
import { set, trimEnd, groupBy } from 'lodash';
|
||||
import { set, trimEnd, groupBy, escapeRegExp } from 'lodash';
|
||||
import { Collection, Entry, EntryDraft, EntryField, EntryMap } from '../types/redux';
|
||||
import { selectEntrySlug } from '../reducers/collections';
|
||||
import { EntryValue } from '../valueObjects/Entry';
|
||||
@ -79,7 +79,7 @@ export function getFilePath(
|
||||
case I18N_STRUCTURE.MULTIPLE_FOLDERS:
|
||||
return path.replace(`/${slug}`, `/${locale}/${slug}`);
|
||||
case I18N_STRUCTURE.MULTIPLE_FILES:
|
||||
return path.replace(extension, `${locale}.${extension}`);
|
||||
return path.replace(new RegExp(`${escapeRegExp(extension)}$`), `${locale}.${extension}`);
|
||||
case I18N_STRUCTURE.SINGLE_FILE:
|
||||
default:
|
||||
return path;
|
||||
|
Loading…
x
Reference in New Issue
Block a user