Skip to content

Commit

Permalink
[i18n] add get_kibana_translation_paths tests (elastic#81624)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
Bamieh and kibanamachine committed Oct 27, 2020
1 parent f3e6671 commit fa9e55f
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 57 deletions.
58 changes: 58 additions & 0 deletions src/legacy/server/i18n/get_kibana_translation_paths.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { I18N_RC } from './constants';
import { fromRoot } from '../../../core/server/utils';

jest.mock('./get_translation_paths', () => ({ getTranslationPaths: jest.fn() }));
import { getKibanaTranslationPaths } from './get_kibana_translation_paths';
import { getTranslationPaths as mockGetTranslationPaths } from './get_translation_paths';

describe('getKibanaTranslationPaths', () => {
const mockConfig = { get: jest.fn() };

beforeEach(() => {
jest.resetAllMocks();
});

it('calls getTranslationPaths against kibana root and kibana-extra', async () => {
mockConfig.get.mockReturnValue([]);
await getKibanaTranslationPaths(mockConfig);
expect(mockGetTranslationPaths).toHaveBeenNthCalledWith(1, {
cwd: fromRoot('.'),
glob: `*/${I18N_RC}`,
});

expect(mockGetTranslationPaths).toHaveBeenNthCalledWith(2, {
cwd: fromRoot('../kibana-extra'),
glob: `*/${I18N_RC}`,
});
});

it('calls getTranslationPaths for each config returned in plugin.paths and plugins.scanDirs', async () => {
mockConfig.get.mockReturnValueOnce(['a', 'b']).mockReturnValueOnce(['c']);
await getKibanaTranslationPaths(mockConfig);
expect(mockConfig.get).toHaveBeenNthCalledWith(1, 'plugins.paths');
expect(mockConfig.get).toHaveBeenNthCalledWith(2, 'plugins.scanDirs');

expect(mockGetTranslationPaths).toHaveBeenNthCalledWith(2, { cwd: 'a', glob: I18N_RC });
expect(mockGetTranslationPaths).toHaveBeenNthCalledWith(3, { cwd: 'b', glob: I18N_RC });
expect(mockGetTranslationPaths).toHaveBeenNthCalledWith(4, { cwd: 'c', glob: `*/${I18N_RC}` });
});
});
42 changes: 42 additions & 0 deletions src/legacy/server/i18n/get_kibana_translation_paths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { KibanaConfig } from '../kbn_server';
import { fromRoot } from '../../../core/server/utils';
import { I18N_RC } from './constants';
import { getTranslationPaths } from './get_translation_paths';

export async function getKibanaTranslationPaths(config: Pick<KibanaConfig, 'get'>) {
return await Promise.all([
getTranslationPaths({
cwd: fromRoot('.'),
glob: `*/${I18N_RC}`,
}),
...(config.get('plugins.paths') as string[]).map((cwd) =>
getTranslationPaths({ cwd, glob: I18N_RC })
),
...(config.get('plugins.scanDirs') as string[]).map((cwd) =>
getTranslationPaths({ cwd, glob: `*/${I18N_RC}` })
),
getTranslationPaths({
cwd: fromRoot('../kibana-extra'),
glob: `*/${I18N_RC}`,
}),
]);
}
63 changes: 63 additions & 0 deletions src/legacy/server/i18n/i18n_mixin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { i18n, i18nLoader } from '@kbn/i18n';
import { basename } from 'path';
import { Server } from 'hapi';
import type { UsageCollectionSetup } from '../../../plugins/usage_collection/server';
import { getKibanaTranslationPaths } from './get_kibana_translation_paths';
import KbnServer, { KibanaConfig } from '../kbn_server';
import { registerLocalizationUsageCollector } from './localization';

export async function i18nMixin(
kbnServer: KbnServer,
server: Server,
config: Pick<KibanaConfig, 'get'>
) {
const locale = config.get('i18n.locale') as string;

const translationPaths = await getKibanaTranslationPaths(config);

const currentTranslationPaths = ([] as string[])
.concat(...translationPaths)
.filter((translationPath) => basename(translationPath, '.json') === locale);
i18nLoader.registerTranslationFiles(currentTranslationPaths);

const translations = await i18nLoader.getTranslationsByLocale(locale);
i18n.init(
Object.freeze({
locale,
...translations,
})
);

const getTranslationsFilePaths = () => currentTranslationPaths;

server.decorate('server', 'getTranslationsFilePaths', getTranslationsFilePaths);

if (kbnServer.newPlatform.setup.plugins.usageCollection) {
const { usageCollection } = kbnServer.newPlatform.setup.plugins as {
usageCollection: UsageCollectionSetup;
};
registerLocalizationUsageCollector(usageCollection, {
getLocale: () => config.get('i18n.locale') as string,
getTranslationsFilePaths,
});
}
}
58 changes: 1 addition & 57 deletions src/legacy/server/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,60 +17,4 @@
* under the License.
*/

import { i18n, i18nLoader } from '@kbn/i18n';
import { basename } from 'path';
import { Server } from 'hapi';
import { fromRoot } from '../../../core/server/utils';
import type { UsageCollectionSetup } from '../../../plugins/usage_collection/server';
import { getTranslationPaths } from './get_translations_path';
import { I18N_RC } from './constants';
import KbnServer, { KibanaConfig } from '../kbn_server';
import { registerLocalizationUsageCollector } from './localization';

export async function i18nMixin(kbnServer: KbnServer, server: Server, config: KibanaConfig) {
const locale = config.get('i18n.locale') as string;

const translationPaths = await Promise.all([
getTranslationPaths({
cwd: fromRoot('.'),
glob: `*/${I18N_RC}`,
}),
...(config.get('plugins.paths') as string[]).map((cwd) =>
getTranslationPaths({ cwd, glob: I18N_RC })
),
...(config.get('plugins.scanDirs') as string[]).map((cwd) =>
getTranslationPaths({ cwd, glob: `*/${I18N_RC}` })
),
getTranslationPaths({
cwd: fromRoot('../kibana-extra'),
glob: `*/${I18N_RC}`,
}),
]);

const currentTranslationPaths = ([] as string[])
.concat(...translationPaths)
.filter((translationPath) => basename(translationPath, '.json') === locale);
i18nLoader.registerTranslationFiles(currentTranslationPaths);

const translations = await i18nLoader.getTranslationsByLocale(locale);
i18n.init(
Object.freeze({
locale,
...translations,
})
);

const getTranslationsFilePaths = () => currentTranslationPaths;

server.decorate('server', 'getTranslationsFilePaths', getTranslationsFilePaths);

if (kbnServer.newPlatform.setup.plugins.usageCollection) {
const { usageCollection } = kbnServer.newPlatform.setup.plugins as {
usageCollection: UsageCollectionSetup;
};
registerLocalizationUsageCollector(usageCollection, {
getLocale: () => config.get('i18n.locale') as string,
getTranslationsFilePaths,
});
}
}
export { i18nMixin } from './i18n_mixin';

0 comments on commit fa9e55f

Please sign in to comment.