diff --git a/packages/netlify-cms-core/src/lib/__tests__/i18n.spec.js b/packages/netlify-cms-core/src/lib/__tests__/i18n.spec.js index 040cfa4b..4f91dd88 100644 --- a/packages/netlify-cms-core/src/lib/__tests__/i18n.spec.js +++ b/packages/netlify-cms-core/src/lib/__tests__/i18n.spec.js @@ -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'; diff --git a/packages/netlify-cms-core/src/lib/i18n.ts b/packages/netlify-cms-core/src/lib/i18n.ts index cdb112ce..b016d2c8 100644 --- a/packages/netlify-cms-core/src/lib/i18n.ts +++ b/packages/netlify-cms-core/src/lib/i18n.ts @@ -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: