From 2ebee4c024db7839cbbd4ad2f798f8e3195b7dd3 Mon Sep 17 00:00:00 2001 From: Denys Vuika Date: Mon, 14 Jul 2025 08:06:53 -0400 Subject: [PATCH] AAE-36615 new provideI18N api (#11007) --- lib/core/src/lib/translation/provide-i18n.ts | 65 ++++++++++++++++++++ lib/core/src/lib/translation/public-api.ts | 1 + 2 files changed, 66 insertions(+) create mode 100644 lib/core/src/lib/translation/provide-i18n.ts diff --git a/lib/core/src/lib/translation/provide-i18n.ts b/lib/core/src/lib/translation/provide-i18n.ts new file mode 100644 index 0000000000..4b2075ccc1 --- /dev/null +++ b/lib/core/src/lib/translation/provide-i18n.ts @@ -0,0 +1,65 @@ +/*! + * @license + * Copyright © 2005-2025 Hyland Software, Inc. and its affiliates. All rights reserved. + * + * 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 { EnvironmentProviders, Provider } from '@angular/core'; +import { provideTranslateService, TranslateLoader } from '@ngx-translate/core'; +import { TranslateLoaderService } from './translate-loader.service'; +import { HttpClient } from '@angular/common/http'; +import { provideTranslations } from './translation.service'; + +export interface ProvideI18NConfig { + /** + * The default language to use for translations. + * If not provided, it defaults to 'en'. + */ + defaultLanguage?: string; + /** + * An array of assets to be used for i18n, where each asset is a tuple containing an identifier and a path. + * Example: [['en', '/assets/i18n/en.json'], ['fr', '/assets/i18n/fr.json']] + */ + assets?: [string, string][]; +} + +/** + * Provides the i18n service. + * This function is used to provide the i18n service in the application. + * It is recommended to use this function in the top-level `AppModule` to ensure that the i18n service is available throughout the application. + * + * @param config - Configuration for the i18n service. + * @param config.assets - An array of assets to be used for i18n, where each asset is a tuple containing an identifier and a path. + * @returns An array of providers for the i18n service. + */ +export function provideI18N(config?: ProvideI18NConfig): (Provider | EnvironmentProviders)[] { + const result: (Provider | EnvironmentProviders)[] = [ + provideTranslateService({ + loader: { + provide: TranslateLoader, + useExisting: TranslateLoaderService, + deps: [HttpClient] + }, + defaultLanguage: config?.defaultLanguage || 'en' + }) + ]; + + if (config?.assets) { + config.assets.forEach(([id, path]) => { + result.push(provideTranslations(id, path)); + }); + } + + return result; +} diff --git a/lib/core/src/lib/translation/public-api.ts b/lib/core/src/lib/translation/public-api.ts index 161b12c3b1..a5553b453c 100644 --- a/lib/core/src/lib/translation/public-api.ts +++ b/lib/core/src/lib/translation/public-api.ts @@ -17,3 +17,4 @@ export * from './translation.service'; export * from './translate-loader.service'; +export * from './provide-i18n';