import * as _angular_core from '@angular/core'; import { EnvironmentProviders, Provider } from '@angular/core'; import { HttpInterceptorFn } from '@angular/common/http'; import { Observable } from 'rxjs'; import * as _odx_angular_utils from '@odx/angular/utils'; type LanguageLoaderFn = () => Observable | Promise | string | undefined; type LocalizationFeature = EnvironmentProviders & { ɵodxBrand: '@odx/angular/localization::LocalizationFeature'; }; /** * @internal * Creates a feature provider for the localization module. * * @param {Provider[] | EnvironmentProviders} provider - The providers to be used for the localization feature. * @returns {LocalizationFeature} The localization feature provider. */ declare function makeLocalizationFeature(provider: Provider[] | EnvironmentProviders): LocalizationFeature; /** * @internal * * Provides Angular locale integration by synchronizing `LOCALE_ID` with the active * localization language and registering locale data for built-in Angular pipes. * * Use `angularLocale: true` in `provideLocalization` config instead of calling this directly. */ declare function withAngularLocale(): LocalizationFeature; declare const HTTP_LANGUAGE_HEADER = "Accept-Language"; /** * Configuration object for the HTTP language header interceptor. It specifies which URLs * the interceptor should apply to and allows customizing the name of the header used to transmit * the current language setting. * * @property {Array} allowedUrls - An array of strings or RegExp patterns * indicating the URLs to which the language header should be added. * @property {string} [headerName=HTTP_LANGUAGE_HEADER] - Optional. The name of the HTTP header * through which the current language setting will be transmitted. Defaults to 'Accept-Language'. */ interface HttpLanguageHeaderInterceptorConfig { allowedUrls: Array; headerName?: string; } /** * Creates an HTTP interceptor function that adds a language header to outgoing HTTP requests, * but only for URLs matching the provided criteria. This allows the server to respond with * resources localized in the user's current language. * * @param {HttpLanguageHeaderInterceptorConfig} config - The configuration for the interceptor, * including the list of allowed URLs and optionally a custom header name. * @returns {HttpInterceptorFn} An Angular HTTP interceptor function that conditionally adds * the current language as an HTTP header based on the request URL. * * @example * ```ts * // Use in HTTP client module setup * import { HttpClientModule } from '@angular/common/http'; * import { httpLanguageHeaderInterceptor, HTTP_LANGUAGE_HEADER } from '@odx/angular/localization'; * * @NgModule({ * imports: [HttpClientModule], * providers: [ * provideHttpClient(withInterceptors([ * httpLanguageHeaderInterceptor({ allowedUrls: ['/api'], headerName: HTTP_LANGUAGE_HEADER }) * ])) * ] * }) * export class AppModule {} * ``` */ declare function httpLanguageHeaderInterceptor({ allowedUrls, headerName }: HttpLanguageHeaderInterceptorConfig): HttpInterceptorFn; /** * Provides a localization feature that automatically adds a language header to HTTP requests * for specified URLs, using the current active language. This function simplifies setting up * the HTTP language header interceptor by wrapping it into a LocalizationFeature, making it easy * to integrate with the application's localization infrastructure. * * @param {HttpLanguageHeaderInterceptorConfig} config - The configuration for the language header * interceptor, specifying which URLs should include the language header and optionally defining a * custom header name. * @returns {LocalizationFeature} A localization feature object that can be used to configure * the application's localization settings to include automatic language header addition for HTTP requests. * * @example * ```ts * // Example usage with the localization provider * import { provideLocalization, LocalizationModule, withHttpLanguageHeader } from '@odx/angular/localization'; * * @NgModule({ * imports: [LocalizationModule], * providers: [ * provideLocalization({}, withHttpLanguageHeader({ allowedUrls: ['/api'] })) * ] * }) * export class AppModule {} * ``` */ declare function withHttpLanguageHeader(config: HttpLanguageHeaderInterceptorConfig): LocalizationFeature; declare const HTTP_LANGUAGE_QUERY = "language"; /** * Defines the configuration options for the HTTP interceptor that appends the current * language setting as a query parameter to outgoing requests. This configuration specifies * which requests should have the language parameter appended based on their URLs. * * @property {Array} allowedUrls - URLs (or patterns matching URLs) to which * the language query parameter should be appended. * @property {string} [queryParamName=HTTP_LANGUAGE_QUERY] - The name of the query parameter * used to transmit the current language. Defaults to 'language'. */ interface HttpLanguageQueryParamInterceptorConfig { allowedUrls: Array; queryParamName?: string; } /** * Creates an HTTP interceptor function that appends the current language as a query parameter * to outgoing HTTP requests that match specified URLs. This interceptor enables dynamic content * localization by informing the backend of the user's preferred language. * * @param {HttpLanguageQueryParamInterceptorConfig} config - Configuration for the interceptor, * including allowed URLs and an optional custom name for the query parameter. * @returns {HttpInterceptorFn} The interceptor function ready to be applied to the HTTP client. * * @example * ```ts * // Applying the interceptor in an Angular module * import { HttpClientModule } from '@angular/common/http'; * import { withHttpLanguageQueryParam } from '@odx/angular/localization'; * * @NgModule({ * imports: [HttpClientModule], * providers: [ * provideHttpClient(withInterceptors([ * withHttpLanguageQueryParam({ allowedUrls: ['/api/resources'] }) * ])) * ] * }) * export class ApiModule {} * ``` */ declare function withHttpLanguageQueryParamInterceptor({ allowedUrls, queryParamName }: HttpLanguageQueryParamInterceptorConfig): HttpInterceptorFn; /** * Registers the HTTP interceptor that appends the current language as a query parameter to * outgoing requests for configured URLs as a localization feature. This utility function * simplifies the setup by bundling the interceptor into a feature that can be easily integrated * with localization configurations. * * @param {HttpLanguageQueryParamInterceptorConfig} config - Configuration for appending the * language query parameter, including allowed URLs and an optional parameter name. * @returns {LocalizationFeature} A localization feature encapsulating the interceptor, ready * for integration with the application's localization setup. * * @example * ```ts * // Integrating the localization feature in an Angular module * import { provideLocalization } from '@angular/core'; * import { withHttpLanguageQueryParam } from '@odx/angular/localization'; * * @NgModule({ * providers: [ * provideLocalization({}, withHttpLanguageQueryParam({ allowedUrls: ['/api'] })) * ] * }) * export class AppModule {} * ``` */ declare function withHttpLanguageQueryParam(config: HttpLanguageQueryParamInterceptorConfig): LocalizationFeature; /** * Loads the browser's current language setting using the `WindowRef` service. * This function is intended to be used as a dynamic language loader within a localization configuration, * allowing applications to automatically adapt to the user's preferred language as specified in browser settings. * * The `WindowRef` service abstracts window-related operations, making it easier to interact with global * browser properties and methods in a testable and platform-agnostic way. This function makes use of * `WindowRef` to access the navigator's language setting in a safe and Angular-friendly manner. * * @returns {string} The language code (e.g., 'en', 'fr') of the browser's currently configured language. * This value can then be used to set the active language in a localization configuration. * * @example * ```ts * import { provideLocalization, browserLanguageLoader } from '@odx/angular/localization'; * * provideLocalization({ * activeLanguageLoader: browserLanguageLoader, * availableLanguages: ['en', 'es', 'fr'], * }); * ``` */ declare const browserLanguageLoader: LanguageLoaderFn; declare const DEFAULT_AVAILABLE_LANGUAGES: string[]; /** * Defines the structure for the localization configuration used throughout the application. * It allows specifying available languages, and includes an optional loader * function for dynamically determining the active language. * * @property {LanguageLoaderFn} [activeLanguageLoader] Optional function for loading the * active language dynamically, useful for scenarios where the application's language * needs to be determined based on user settings, browser settings, or external data. * @property {boolean} [angularLocale=false] Enables Angular locale integration (`LOCALE_ID` * and locale data registration) based on the resolved active language. * @property {string[]} availableLanguages An array of language codes representing the * languages available in the application. */ interface LocalizationConfig { activeLanguageLoader?: LanguageLoaderFn; angularLocale?: boolean; availableLanguages: string[]; } /** * Utilizes `createConfigTokens` to generate configuration tokens and related utilities for the * localization settings within the application. It defines a structured and type-safe approach * for managing localization configurations, such as available languages, and potentially * a dynamic loader for the active language. The generated tokens and utilities facilitate accessing * and providing these configurations throughout the application. * * @const {InjectionToken} LocalizationConfig - An InjectionToken for accessing * the localization configuration within Angular's dependency injection framework. Use this token to inject * localization configurations into services, components, or other tokens. * * @const {LocalizationConfig} LocalizationDefaultConfig - The default configuration values for localization, * based on the provided settings. Includes the default available languages. * * @const {Function} injectLocalizationConfig - A utility function that returns the injected value of * `LocalizationConfig` token. Use this function to access the current localization configuration in * a type-safe manner. * * @const {Function} provideLocalizationConfig - A utility function to provide the `LocalizationConfig` token * with specific values. Use this function to configure and override default localization settings within * an Angular module or component. * * @example * ```ts * // Providing localization settings in an Angular module * @NgModule({ * providers: [ * provideLocalizationConfig({ * availableLanguages: ['en', 'es', 'fr'], * }) * ] * }) * export class AppModule { } * * // Injecting and using the localization configuration in a service * @Injectable({ providedIn: 'root' }) * export class MyLocalizationService { * constructor(@Inject(LocalizationConfig) private config: LocalizationConfig) { * console.log('Available languages:', config.availableLanguages); * } * } * ``` */ declare const LocalizationConfig: _angular_core.InjectionToken>; declare const LocalizationDefaultConfig: LocalizationConfig; declare const injectLocalizationConfig: () => LocalizationConfig; declare const provideLocalizationConfig: >>(config: _odx_angular_utils.ConfigProvider, D>) => _angular_core.FactoryProvider | _angular_core.ValueProvider; /** * Sets up the localization configuration for the application and initializes the localization * system with the provided configuration and features. This function is typically called * within the main module or a core module of the application to ensure localization settings * are available and active as early as possible. * * @param {Partial} config The localization configuration settings, including * available languages and optionally, a custom loader for the active language. * @param {...LocalizationFeature[]} features Additional localization features or configurations * to be registered as part of the application's environment providers. * @returns {EnvironmentProviders} A collection of providers for setting up the application's * localization environment, to be used with Angular's environment-specific injector configuration. * @example * ```ts * // Example of providing localization settings in an Angular module * @NgModule({ * imports: [...], * providers: [ * provideLocalization({ * availableLanguages: ['en', 'es', 'fr'], * activeLanguageLoader: () => fetch('/api/user/language').then(res => res.text()), * }), * ], * }) * export class AppModule {} * ``` */ declare function provideLocalization(config: Partial, ...features: LocalizationFeature[]): EnvironmentProviders; declare class LocalizationService { private readonly localizationConfig; private readonly injector; private readonly initialLoadedLanguage; private readonly localeState$; private loadActiveLanguagePromise?; /** * Represents an observable stream of the active language in the localization service. * The active language is determined by the locale state. * * @emits {string} The active language. */ readonly activeLanguage$: Observable; /** * Gets the list of available languages for localization. * * @type {string[]} The list of available languages. */ readonly availableLanguages: string[]; /** * Default language derived from the first entry in the available languages list. * * @type {string} */ readonly defaultLanguage: string; constructor(); /** * Retrieves the currently active language. * * @returns {string} Full locale name e.g. 'en-US' or just 'en' if no region info is attached. */ getActiveLanguage(): string; /** * Retrieves the active language subtag. * * @returns {string} The two-digit language subtag, e.g. 'en', 'fr'. */ getActiveLanguageSubtag(): string; /** * Sets the active language based on the provided language code. If the language is not * recognized, it logs an error and does not change the active language. * * @param {string} language The language code to set as the active language. */ setActiveLanguage(language: string): void; /** * Loads the active language for the localization service. * This method runs in an injection context and sets the active language based on the result of the active language loader. * If the active language loader fails, the default language is set as the active language. * * @returns {Promise} A promise that resolves when the active language is loaded and set. */ loadActiveLanguage(): Promise; private createActiveLanguageLoader$; static ɵfac: _angular_core.ɵɵFactoryDeclaration; static ɵprov: _angular_core.ɵɵInjectableDeclaration; } export { DEFAULT_AVAILABLE_LANGUAGES, HTTP_LANGUAGE_HEADER, HTTP_LANGUAGE_QUERY, LocalizationConfig, LocalizationDefaultConfig, LocalizationService, browserLanguageLoader, httpLanguageHeaderInterceptor, injectLocalizationConfig, makeLocalizationFeature, provideLocalization, provideLocalizationConfig, withAngularLocale, withHttpLanguageHeader, withHttpLanguageQueryParam, withHttpLanguageQueryParamInterceptor }; export type { HttpLanguageHeaderInterceptorConfig, HttpLanguageQueryParamInterceptorConfig, LanguageLoaderFn, LocalizationFeature };