core-plugin-api: split AppTranslationApi into AppLanguageApi and TranslationApi

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-09-14 15:33:48 +02:00
parent a76aaf87bf
commit 1e84508592
4 changed files with 63 additions and 24 deletions
@@ -136,22 +136,6 @@ class ResourceLoader {
}
}
/** @alpha */
export interface TranslationOptions {
/* no options supported for now */
}
export type TranslationSnapshot<TMessages extends { [key in string]: string }> =
| { ready: false }
| {
ready: true;
t<TKey extends keyof TMessages>(
key: TKey,
options?: TranslationOptions,
): TMessages[TKey];
};
/** @alpha */
export class AppTranslationApiImpl implements AppTranslationApi {
static create(options?: ExperimentalI18n) {
@@ -14,20 +14,23 @@
* limitations under the License.
*/
import { type i18n } from 'i18next';
import { TranslationRef } from '../../translation';
import { ApiRef, createApiRef } from '@backstage/core-plugin-api';
import { Observable } from '@backstage/types';
/** @alpha */
export type AppTranslationApi = {
getI18n(): i18n;
export type AppLanguageApi = {
getAvailableLanguages(): { languages: string[] };
addResource(resource: TranslationRef): void;
setLanguage(language?: string): void;
getLanguage(): { language: string };
language$(): Observable<{ language: string }>;
};
/**
* @alpha
*/
export const appTranslationApiRef: ApiRef<AppTranslationApi> = createApiRef({
id: 'core.translation',
export const appLanguageApiRef: ApiRef<AppLanguageApi> = createApiRef({
id: 'core.applanguage',
});
@@ -0,0 +1,51 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed 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 { ApiRef, createApiRef } from '@backstage/core-plugin-api';
import { Observable } from '@backstage/types';
import { TranslationRef } from '../../translation';
/** @alpha */
export interface TranslationOptions {
/* no options supported for now */
}
export type TranslationFunction<TMessages extends { [key in string]: string }> =
<TKey extends keyof TMessages>(
key: TKey,
options?: TranslationOptions,
) => TMessages[TKey];
export type TranslationSnapshot<TMessages extends { [key in string]: string }> =
{ ready: false } | { ready: true; t: TranslationFunction<TMessages> };
/** @alpha */
export type TranslationApi = {
getTranslation<TMessages extends { [key in string]: string }>(
translationRef: TranslationRef<string, TMessages>,
): TranslationSnapshot<TMessages>;
translation$<TMessages extends { [key in string]: string }>(
translationRef: TranslationRef<string, TMessages>,
): Observable<TranslationSnapshot<TMessages>>;
};
/**
* @alpha
*/
export const translationApiRef: ApiRef<TranslationApi> = createApiRef({
id: 'core.translation',
});
@@ -13,4 +13,5 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from './AppTranslationApi';
export * from './TranslationApi';
export * from './AppLanguageApi';