From 1e845085920a2ea2757e7352cf4bd75249ed338d Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Thu, 14 Sep 2023 15:33:48 +0200 Subject: [PATCH] core-plugin-api: split AppTranslationApi into AppLanguageApi and TranslationApi Signed-off-by: Patrik Oldsberg --- .../AppTranslationApi/AppTranslationImpl.ts | 16 ------ ...AppTranslationApi.ts => AppLanguageApi.ts} | 17 ++++--- .../src/apis/definitions/TranslationApi.ts | 51 +++++++++++++++++++ .../src/apis/definitions/alpha.ts | 3 +- 4 files changed, 63 insertions(+), 24 deletions(-) rename packages/core-plugin-api/src/apis/definitions/{AppTranslationApi.ts => AppLanguageApi.ts} (66%) create mode 100644 packages/core-plugin-api/src/apis/definitions/TranslationApi.ts diff --git a/packages/core-app-api/src/apis/implementations/AppTranslationApi/AppTranslationImpl.ts b/packages/core-app-api/src/apis/implementations/AppTranslationApi/AppTranslationImpl.ts index 67b82d3998..018e363140 100644 --- a/packages/core-app-api/src/apis/implementations/AppTranslationApi/AppTranslationImpl.ts +++ b/packages/core-app-api/src/apis/implementations/AppTranslationApi/AppTranslationImpl.ts @@ -136,22 +136,6 @@ class ResourceLoader { } } -/** @alpha */ -export interface TranslationOptions { - /* no options supported for now */ -} - -export type TranslationSnapshot = - - | { ready: false } - | { - ready: true; - t( - key: TKey, - options?: TranslationOptions, - ): TMessages[TKey]; - }; - /** @alpha */ export class AppTranslationApiImpl implements AppTranslationApi { static create(options?: ExperimentalI18n) { diff --git a/packages/core-plugin-api/src/apis/definitions/AppTranslationApi.ts b/packages/core-plugin-api/src/apis/definitions/AppLanguageApi.ts similarity index 66% rename from packages/core-plugin-api/src/apis/definitions/AppTranslationApi.ts rename to packages/core-plugin-api/src/apis/definitions/AppLanguageApi.ts index 44f85c0c89..1207e84755 100644 --- a/packages/core-plugin-api/src/apis/definitions/AppTranslationApi.ts +++ b/packages/core-plugin-api/src/apis/definitions/AppLanguageApi.ts @@ -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 = createApiRef({ - id: 'core.translation', +export const appLanguageApiRef: ApiRef = createApiRef({ + id: 'core.applanguage', }); diff --git a/packages/core-plugin-api/src/apis/definitions/TranslationApi.ts b/packages/core-plugin-api/src/apis/definitions/TranslationApi.ts new file mode 100644 index 0000000000..10a9c86b11 --- /dev/null +++ b/packages/core-plugin-api/src/apis/definitions/TranslationApi.ts @@ -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 = + ( + key: TKey, + options?: TranslationOptions, + ) => TMessages[TKey]; + +export type TranslationSnapshot = + { ready: false } | { ready: true; t: TranslationFunction }; + +/** @alpha */ +export type TranslationApi = { + getTranslation( + translationRef: TranslationRef, + ): TranslationSnapshot; + + translation$( + translationRef: TranslationRef, + ): Observable>; +}; + +/** + * @alpha + */ +export const translationApiRef: ApiRef = createApiRef({ + id: 'core.translation', +}); diff --git a/packages/core-plugin-api/src/apis/definitions/alpha.ts b/packages/core-plugin-api/src/apis/definitions/alpha.ts index 691af96e32..fa28aef46a 100644 --- a/packages/core-plugin-api/src/apis/definitions/alpha.ts +++ b/packages/core-plugin-api/src/apis/definitions/alpha.ts @@ -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';