Skip to content

Commit

Permalink
fix: i18n regex logic (#5136)
Browse files Browse the repository at this point in the history
  • Loading branch information
artt authored Mar 21, 2021
1 parent 826d82f commit b0c0ea6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
37 changes: 27 additions & 10 deletions packages/netlify-cms-core/src/lib/__tests__/i18n.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});

Expand Down
4 changes: 2 additions & 2 deletions packages/netlify-cms-core/src/lib/i18n.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit b0c0ea6

Please sign in to comment.