mirror of
https://github.com/grafana/grafana.git
synced 2025-08-03 05:08:36 +08:00

* 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
50 lines
1.3 KiB
TypeScript
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,
|
|
});
|
|
}
|
|
}
|