Skip to content

Commit 6a42f98

Browse files
authored
fix(i18n): return the correct locale from filenames (#5922)
1 parent bf8b94f commit 6a42f98

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

Diff for: packages/netlify-cms-core/src/lib/__tests__/i18n.spec.js

+30
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,36 @@ describe('i18n', () => {
207207
});
208208
});
209209

210+
describe('getLocaleFromPath', () => {
211+
it('should return the locale from folder name in the path when structure is I18N_STRUCTURE.MULTIPLE_FOLDERS', () => {
212+
expect(
213+
i18n.getLocaleFromPath(
214+
i18n.I18N_STRUCTURE.MULTIPLE_FOLDERS,
215+
'md',
216+
'src/content/en/index.md',
217+
),
218+
).toEqual('en');
219+
});
220+
221+
it('should return the locale extension from the file name when structure is I18N_STRUCTURE.MULTIPLE_FILES', () => {
222+
expect(
223+
i18n.getLocaleFromPath(i18n.I18N_STRUCTURE.MULTIPLE_FILES, 'md', 'src/content/index.en.md'),
224+
).toEqual('en');
225+
});
226+
227+
it('issue #5909: return the correct locale extension for language gd', () => {
228+
expect(
229+
i18n.getLocaleFromPath(i18n.I18N_STRUCTURE.MULTIPLE_FILES, 'md', 'src/content/index.gd.md'),
230+
).toEqual('gd');
231+
});
232+
233+
it('should return an empty string when structure is I18N_STRUCTURE.SINGLE_FILE', () => {
234+
expect(
235+
i18n.getLocaleFromPath(i18n.I18N_STRUCTURE.SINGLE_FILE, 'md', 'src/content/index.md'),
236+
).toEqual('');
237+
});
238+
});
239+
210240
describe('getI18nFiles', () => {
211241
const locales = ['en', 'de', 'fr'];
212242
const default_locale = 'en';

Diff for: packages/netlify-cms-core/src/lib/i18n.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Map, List } from 'immutable';
2-
import { set, trimEnd, groupBy, escapeRegExp } from 'lodash';
2+
import { set, groupBy, escapeRegExp } from 'lodash';
33

44
import { selectEntrySlug } from '../reducers/collections';
55

@@ -98,7 +98,7 @@ export function getLocaleFromPath(structure: I18N_STRUCTURE, extension: string,
9898
return parts.pop();
9999
}
100100
case I18N_STRUCTURE.MULTIPLE_FILES: {
101-
const parts = trimEnd(path, `.${extension}`);
101+
const parts = path.slice(0, -`.${extension}`.length);
102102
return parts.split('.').pop();
103103
}
104104
case I18N_STRUCTURE.SINGLE_FILE:

0 commit comments

Comments
 (0)