forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[i18n] add get_kibana_translation_paths tests (elastic#81624)
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
f3e6671
commit fa9e55f
Showing
5 changed files
with
164 additions
and
57 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/legacy/server/i18n/get_kibana_translation_paths.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}` }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`, | ||
}), | ||
]); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters