mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-24 12:07:58 +02:00
add getCurrentLocaleConfig utils
This commit is contained in:
parent
68cae1bf4c
commit
db7b2f334e
3 changed files with 78 additions and 1 deletions
|
@ -5,13 +5,15 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import path from 'path';
|
||||
import * as path from 'path';
|
||||
import {
|
||||
mergeTranslations,
|
||||
updateTranslationFileMessages,
|
||||
getPluginI18nPath,
|
||||
localizePath,
|
||||
getCurrentLocaleConfig,
|
||||
} from '../i18nUtils';
|
||||
import type {I18n, I18nLocaleConfig} from '@docusaurus/types';
|
||||
|
||||
describe('mergeTranslations', () => {
|
||||
it('works', () => {
|
||||
|
@ -179,3 +181,65 @@ describe('localizePath', () => {
|
|||
).toBe('/baseUrl/');
|
||||
});
|
||||
});
|
||||
|
||||
describe('getCurrentLocaleConfig', () => {
|
||||
const localeConfigEn: I18nLocaleConfig = {
|
||||
path: 'path',
|
||||
direction: 'rtl',
|
||||
htmlLang: 'en',
|
||||
calendar: 'calendar',
|
||||
label: 'EN',
|
||||
translate: true,
|
||||
};
|
||||
const localeConfigFr: I18nLocaleConfig = {
|
||||
path: 'path',
|
||||
direction: 'rtl',
|
||||
htmlLang: 'fr',
|
||||
calendar: 'calendar',
|
||||
label: 'FR',
|
||||
translate: true,
|
||||
};
|
||||
|
||||
function i18n(params: Partial<I18n>): I18n {
|
||||
return {
|
||||
defaultLocale: 'en',
|
||||
localeConfigs: {},
|
||||
locales: ['en'],
|
||||
path: 'path',
|
||||
currentLocale: 'en',
|
||||
...params,
|
||||
};
|
||||
}
|
||||
|
||||
it('returns single locale config', () => {
|
||||
expect(
|
||||
getCurrentLocaleConfig(
|
||||
i18n({currentLocale: 'en', localeConfigs: {en: localeConfigEn}}),
|
||||
),
|
||||
).toEqual(localeConfigEn);
|
||||
});
|
||||
|
||||
it('returns correct locale config among 2', () => {
|
||||
expect(
|
||||
getCurrentLocaleConfig(
|
||||
i18n({
|
||||
currentLocale: 'fr',
|
||||
localeConfigs: {en: localeConfigEn, fr: localeConfigFr},
|
||||
}),
|
||||
),
|
||||
).toEqual(localeConfigFr);
|
||||
});
|
||||
|
||||
it('throws for locale config that does not exist', () => {
|
||||
expect(() =>
|
||||
getCurrentLocaleConfig(
|
||||
i18n({
|
||||
currentLocale: 'fr',
|
||||
localeConfigs: {en: localeConfigEn},
|
||||
}),
|
||||
),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
`"Can't find locale config for locale \`fr\`"`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -7,12 +7,14 @@
|
|||
|
||||
import path from 'path';
|
||||
import _ from 'lodash';
|
||||
import logger from '@docusaurus/logger';
|
||||
import {DEFAULT_PLUGIN_ID} from './constants';
|
||||
import {normalizeUrl} from './urlUtils';
|
||||
import type {
|
||||
TranslationFileContent,
|
||||
TranslationFile,
|
||||
I18n,
|
||||
I18nLocaleConfig,
|
||||
} from '@docusaurus/types';
|
||||
|
||||
/**
|
||||
|
@ -112,3 +114,13 @@ export function localizePath({
|
|||
// Url paths; add a trailing slash so it's a valid base URL
|
||||
return normalizeUrl([originalPath, i18n.currentLocale, '/']);
|
||||
}
|
||||
|
||||
export function getCurrentLocaleConfig(i18n: I18n): I18nLocaleConfig {
|
||||
const localeConfig = i18n.localeConfigs[i18n.currentLocale];
|
||||
if (!localeConfig) {
|
||||
throw new Error(
|
||||
`Can't find locale config for locale ${logger.code(i18n.currentLocale)}`,
|
||||
);
|
||||
}
|
||||
return localeConfig;
|
||||
}
|
||||
|
|
|
@ -34,6 +34,7 @@ export {
|
|||
updateTranslationFileMessages,
|
||||
getPluginI18nPath,
|
||||
localizePath,
|
||||
getCurrentLocaleConfig,
|
||||
} from './i18nUtils';
|
||||
export {mapAsyncSequential, findAsyncSequential} from './jsUtils';
|
||||
export {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue