Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix (i18n): Return the Correct Locale from Filenames - fixes #5909 #5922

Merged
merged 1 commit into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/netlify-cms-core/src/lib/__tests__/i18n.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
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, escapeRegExp } from 'lodash';
import { set, groupBy, escapeRegExp } from 'lodash';

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

Expand Down Expand Up @@ -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:
Expand Down