feat(core): allow customizing the i18n directory path (#7386)

Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
This commit is contained in:
Joshua Chen 2022-06-02 23:37:14 +08:00 committed by GitHub
parent c07a514730
commit abe5450526
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 147 additions and 166 deletions

View file

@ -65,34 +65,33 @@ describe('getPluginI18nPath', () => {
it('gets correct path', () => {
expect(
getPluginI18nPath({
siteDir: __dirname,
locale: 'zh-Hans',
localizationDir: '<SITE_DIR>/i18n/zh-Hans',
pluginName: 'plugin-content-docs',
pluginId: 'community',
subPaths: ['foo'],
}),
).toMatchInlineSnapshot(
`"<PROJECT_ROOT>/packages/docusaurus-utils/src/__tests__/i18n/zh-Hans/plugin-content-docs-community/foo"`,
`"<SITE_DIR>/i18n/zh-Hans/plugin-content-docs-community/foo"`,
);
});
it('gets correct path for default plugin', () => {
expect(
getPluginI18nPath({
siteDir: __dirname,
locale: 'zh-Hans',
localizationDir: '<SITE_DIR>/i18n/zh-Hans',
pluginName: 'plugin-content-docs',
subPaths: ['foo'],
}).replace(__dirname, ''),
).toMatchInlineSnapshot(`"/i18n/zh-Hans/plugin-content-docs/foo"`);
}),
).toMatchInlineSnapshot(
`"<SITE_DIR>/i18n/zh-Hans/plugin-content-docs/foo"`,
);
});
it('gets correct path when no sub-paths', () => {
expect(
getPluginI18nPath({
siteDir: __dirname,
locale: 'zh-Hans',
localizationDir: '<SITE_DIR>/i18n/zh-Hans',
pluginName: 'plugin-content-docs',
}).replace(__dirname, ''),
).toMatchInlineSnapshot(`"/i18n/zh-Hans/plugin-content-docs"`);
}),
).toMatchInlineSnapshot(`"<SITE_DIR>/i18n/zh-Hans/plugin-content-docs"`);
});
});
@ -104,6 +103,7 @@ describe('localizePath', () => {
path: '/baseUrl',
i18n: {
defaultLocale: 'en',
path: 'i18n',
locales: ['en', 'fr'],
currentLocale: 'fr',
localeConfigs: {},
@ -120,6 +120,7 @@ describe('localizePath', () => {
path: '/baseFsPath',
i18n: {
defaultLocale: 'en',
path: 'i18n',
locales: ['en', 'fr'],
currentLocale: 'fr',
localeConfigs: {},
@ -136,6 +137,7 @@ describe('localizePath', () => {
path: '/baseUrl/',
i18n: {
defaultLocale: 'en',
path: 'i18n',
locales: ['en', 'fr'],
currentLocale: 'en',
localeConfigs: {},
@ -152,6 +154,7 @@ describe('localizePath', () => {
path: '/baseUrl/',
i18n: {
defaultLocale: 'en',
path: 'i18n',
locales: ['en', 'fr'],
currentLocale: 'en',
localeConfigs: {},
@ -167,6 +170,7 @@ describe('localizePath', () => {
path: '/baseUrl/',
i18n: {
defaultLocale: 'en',
path: 'i18n',
locales: ['en', 'fr'],
currentLocale: 'en',
localeConfigs: {},

View file

@ -75,7 +75,7 @@ export const THEME_PATH = `${SRC_DIR_NAME}/theme`;
* All translation-related data live here, relative to site directory. Content
* will be namespaced by locale.
*/
export const I18N_DIR_NAME = 'i18n';
export const DEFAULT_I18N_DIR_NAME = 'i18n';
/**
* Translations for React code.

View file

@ -7,7 +7,7 @@
import path from 'path';
import _ from 'lodash';
import {DEFAULT_PLUGIN_ID, I18N_DIR_NAME} from './constants';
import {DEFAULT_PLUGIN_ID} from './constants';
import {normalizeUrl} from './urlUtils';
import type {
TranslationFileContent,
@ -46,24 +46,18 @@ export function updateTranslationFileMessages(
* expect everything it needs for translations to be found under this path.
*/
export function getPluginI18nPath({
siteDir,
locale,
localizationDir,
pluginName,
pluginId = DEFAULT_PLUGIN_ID,
subPaths = [],
}: {
siteDir: string;
locale: string;
localizationDir: string;
pluginName: string;
pluginId?: string | undefined;
subPaths?: string[];
}): string {
return path.join(
siteDir,
I18N_DIR_NAME,
// Namespace first by locale: convenient to work in a single folder for a
// translator
locale,
localizationDir,
// Make it convenient to use for single-instance
// ie: return "docs", not "docs-default" nor "docs/default"
`${pluginName}${pluginId === DEFAULT_PLUGIN_ID ? '' : `-${pluginId}`}`,

View file

@ -17,7 +17,7 @@ export {
DEFAULT_STATIC_DIR_NAME,
OUTPUT_STATIC_ASSETS_DIR_NAME,
THEME_PATH,
I18N_DIR_NAME,
DEFAULT_I18N_DIR_NAME,
CODE_TRANSLATIONS_FILE_NAME,
DEFAULT_PORT,
DEFAULT_PLUGIN_ID,