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

i18n - allow plugins to specify multiple paths #46578

Merged
merged 2 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* eslint-disable */

i18n('plugin_3.duplicate_id', { defaultMessage: 'Message 1' });

i18n.translate('plugin_3.duplicate_id', {
defaultMessage: 'Message 2',
description: 'Message description',
});
10 changes: 6 additions & 4 deletions src/dev/i18n/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
*/

import { resolve } from 'path';
import _ from 'lodash';

// @ts-ignore
import { normalizePath, readFileAsync } from '.';

export interface I18nConfig {
paths: Record<string, string>;
paths: Record<string, string[]>;
exclude: string[];
translations: string[];
prefix?: string;
Expand All @@ -49,8 +50,9 @@ export async function assignConfigFromPath(
...JSON.parse(await readFileAsync(resolve(configPath))),
};

for (const [namespace, path] of Object.entries(additionalConfig.paths)) {
config.paths[namespace] = normalizePath(resolve(configPath, '..', path));
for (const [namespace, namespacePaths] of Object.entries(additionalConfig.paths)) {
const paths = Array.isArray(namespacePaths) ? namespacePaths : [namespacePaths];
config.paths[namespace] = paths.map(path => normalizePath(resolve(configPath, '..', path)));
}

for (const exclude of additionalConfig.exclude) {
Expand All @@ -71,7 +73,7 @@ export async function assignConfigFromPath(
* @param config I18n config instance.
*/
export function filterConfigPaths(inputPaths: string[], config: I18nConfig) {
const availablePaths = Object.values(config.paths);
const availablePaths = _.flatten(Object.values(config.paths));
spalger marked this conversation as resolved.
Show resolved Hide resolved
const pathsForExtraction = new Set();

for (const inputPath of inputPaths) {
Expand Down
4 changes: 2 additions & 2 deletions src/dev/i18n/extract_default_translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ function filterEntries(entries, exclude) {
export function validateMessageNamespace(id, filePath, allowedPaths, reporter) {
const normalizedPath = normalizePath(filePath);

const [expectedNamespace] = Object.entries(allowedPaths).find(([, pluginPath]) =>
normalizedPath.startsWith(`${pluginPath}/`)
const [expectedNamespace] = Object.entries(allowedPaths).find(([, pluginPaths]) =>
pluginPaths.some(pluginPath => normalizedPath.startsWith(`${pluginPath}/`))
);

if (!id.startsWith(`${expectedNamespace}.`)) {
Expand Down
24 changes: 21 additions & 3 deletions src/dev/i18n/extract_default_translations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ const pluginsPaths = [
path.join(fixturesPath, 'test_plugin_1'),
path.join(fixturesPath, 'test_plugin_2'),
path.join(fixturesPath, 'test_plugin_3'),
path.join(fixturesPath, 'test_plugin_3_additional_path'),
];

const config = {
paths: {
plugin_1: 'src/dev/i18n/__fixtures__/extract_default_translations/test_plugin_1',
plugin_2: 'src/dev/i18n/__fixtures__/extract_default_translations/test_plugin_2',
plugin_3: 'src/dev/i18n/__fixtures__/extract_default_translations/test_plugin_3',
plugin_1: ['src/dev/i18n/__fixtures__/extract_default_translations/test_plugin_1'],
plugin_2: ['src/dev/i18n/__fixtures__/extract_default_translations/test_plugin_2'],
plugin_3: [
'src/dev/i18n/__fixtures__/extract_default_translations/test_plugin_3',
'src/dev/i18n/__fixtures__/extract_default_translations/test_plugin_3_additional_path'
],
},
exclude: [],
};
Expand Down Expand Up @@ -69,6 +73,20 @@ describe('dev/i18n/extract_default_translations', () => {
expect(() => validateMessageNamespace(id, filePath, config.paths)).not.toThrow();
});

test('validates message namespace with multiple paths', () => {
const id = 'plugin_3.message-id';
const filePath1 = path.resolve(
__dirname,
'__fixtures__/extract_default_translations/test_plugin_3/test_file.html'
);
const filePath2 = path.resolve(
__dirname,
'__fixtures__/extract_default_translations/test_plugin_3_additional_path/test_file.html'
);
expect(() => validateMessageNamespace(id, filePath1, config.paths)).not.toThrow();
expect(() => validateMessageNamespace(id, filePath2, config.paths)).not.toThrow();
});

test('throws on wrong message namespace', () => {
const report = jest.fn();
const id = 'wrong_plugin_namespace.message-id';
Expand Down
4 changes: 2 additions & 2 deletions src/dev/i18n/integrate_locale_files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ const defaultIntegrateOptions = {
ignoreUnused: false,
config: {
paths: {
'plugin-1': 'src/dev/i18n/__fixtures__/integrate_locale_files/test_plugin_1',
'plugin-2': 'src/dev/i18n/__fixtures__/integrate_locale_files/test_plugin_2',
'plugin-1': ['src/dev/i18n/__fixtures__/integrate_locale_files/test_plugin_1'],
'plugin-2': ['src/dev/i18n/__fixtures__/integrate_locale_files/test_plugin_2'],
},
exclude: [],
translations: [],
Expand Down
20 changes: 11 additions & 9 deletions src/dev/i18n/integrate_locale_files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,19 @@ async function writeMessages(
// Use basename of source file name to write the same locale name as the source file has.
const fileName = path.basename(options.sourceFileName);
for (const [namespace, messages] of localizedMessagesByNamespace) {
const destPath = path.resolve(options.config.paths[namespace], 'translations');
for (const namespacedPath of options.config.paths[namespace]) {
const destPath = path.resolve(namespacedPath, 'translations');

try {
await accessAsync(destPath);
} catch (_) {
await makeDirAsync(destPath);
}
try {
await accessAsync(destPath);
} catch (_) {
await makeDirAsync(destPath);
}

const writePath = path.resolve(destPath, fileName);
await writeFileAsync(writePath, serializeToJson(messages, formats));
options.log.success(`Translations have been integrated to ${normalizePath(writePath)}`);
const writePath = path.resolve(destPath, fileName);
await writeFileAsync(writePath, serializeToJson(messages, formats));
options.log.success(`Translations have been integrated to ${normalizePath(writePath)}`);
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/dev/i18n/tasks/extract_untracked_translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import { createFailError } from '@kbn/dev-utils';
import _ from 'lodash';
import {
I18nConfig,
matchEntriesWithExctractors,
Expand All @@ -42,7 +43,7 @@ export async function extractUntrackedMessagesTask({
reporter: any;
}) {
const inputPaths = Array.isArray(path) ? path : [path || './'];
const availablePaths = Object.values(config.paths);
const availablePaths = _.flatten(Object.values(config.paths));
spalger marked this conversation as resolved.
Show resolved Hide resolved
const ignore = availablePaths.concat([
'**/build/**',
'**/webpackShims/**',
Expand Down