Files
grafana/public/app/features/plugins/importer/addTranslationsToI18n.ts
Hugo Häggmark 5b82e05697 Plugins: replaces various plugin imports with pluginImporter (#108002)
* Plugins: renames plugin_loader

* Wip

* chore: adds pluginImporter

* chore: some small refactors

* chore: better typings

* chore: merge functions

* chore: create a generic plugin cache

* chore: adds comments

* chore: change to const

* chore: remove unused change

* chore: put everything behind feature toggle

* chore: rename test file and props

* chore: adds sync cache as well

* chore: fix the typings

* chore: fix broken unit test

* chore: small rename

* chore: adds tests

* chore: updates after PR feedback

* chore: updates after PR feedback
2025-07-16 06:42:28 +02:00

50 lines
1.3 KiB
TypeScript

import { addResourceBundle } from '@grafana/i18n/internal';
import { SystemJS } from '../loader/systemjs';
import { resolveModulePath } from '../loader/utils';
interface AddTranslationsToI18nOptions {
resolvedLanguage: string;
fallbackLanguage: string;
pluginId: string;
translations: Record<string, string>;
}
export async function addTranslationsToI18n({
resolvedLanguage,
fallbackLanguage,
pluginId,
translations,
}: AddTranslationsToI18nOptions): Promise<void> {
const resolvedPath = translations[resolvedLanguage];
const fallbackPath = translations[fallbackLanguage];
const path = resolvedPath ?? fallbackPath;
if (!path) {
console.warn(`Could not find any translation for plugin ${pluginId}`, { resolvedLanguage, fallbackLanguage });
return;
}
try {
const module = await SystemJS.import(resolveModulePath(path));
if (!module.default) {
console.warn(`Could not find default export for plugin ${pluginId}`, {
resolvedLanguage,
fallbackLanguage,
path,
});
return;
}
const language = resolvedPath ? resolvedLanguage : fallbackLanguage;
addResourceBundle(language, pluginId, module.default);
} catch (error) {
console.warn(`Could not load translation for plugin ${pluginId}`, {
resolvedLanguage,
fallbackLanguage,
error,
path,
});
}
}