@cocoar/localization 0.1.0-beta.114 → 0.1.0-beta.115

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  import * as i0 from '@angular/core';
2
- import { Signal, InjectionToken, EnvironmentProviders, PipeTransform, OnDestroy, Type, Provider } from '@angular/core';
2
+ import { Signal, InjectionToken, EnvironmentProviders, PipeTransform, OnDestroy } from '@angular/core';
3
3
  import { Observable } from 'rxjs';
4
4
  import { HttpClient } from '@angular/common/http';
5
5
  import { Temporal } from '@js-temporal/polyfill';
@@ -139,17 +139,6 @@ declare class CoarLocalizationService {
139
139
  * ```
140
140
  */
141
141
  preloadLocaleData(language: string): Promise<void>;
142
- /**
143
- * Get the list of available languages configured for the application.
144
- *
145
- * @returns Array of available language codes, or empty array if not configured
146
- *
147
- * @example
148
- * ```typescript
149
- * const langs = locale.getAvailableLanguages(); // ['en', 'de', 'fr']
150
- * ```
151
- */
152
- getAvailableLanguages(): string[];
153
142
  /**
154
143
  * Get the default language configured for the application.
155
144
  *
@@ -298,10 +287,6 @@ declare class CoarHttpLocaleDataLoader extends CoarLocalizationDataLoader {
298
287
  * Configuration for the locale system.
299
288
  */
300
289
  interface CoarLocalizationConfig {
301
- /**
302
- * List of available languages in the application.
303
- */
304
- availableLanguages: string[];
305
290
  /**
306
291
  * Default language to use on initialization.
307
292
  */
@@ -318,34 +303,33 @@ declare const COAR_LOCALIZATION_CONFIG: InjectionToken<CoarLocalizationConfig>;
318
303
  */
319
304
  declare const COAR_LOCALIZATION_DATA_LOADERS: InjectionToken<CoarLocalizationDataLoader[]>;
320
305
  /**
321
- * Provides the core locale system (language management).
306
+ * Provides the core localization system (language management + L10n + i18n).
307
+ *
308
+ * Automatically includes:
309
+ * - Language management (CoarLocalizationService)
310
+ * - L10n: Browser Intl API for number/date formatting (always first source)
311
+ * - i18n: Translation system (CoarI18nService + CoarTranslationStore)
322
312
  *
323
- * This only provides the language service - no locale data sources.
324
- * Add locale data sources separately with:
325
- * - `provideCoarIntlLocalizationSource()` - Browser Intl API (recommended as first source)
326
- * - `provideCoarHttpLocalizationSource()` - Load from JSON files
327
- * - Custom sources via `COAR_LOCALIZATION_DATA_LOADERS` multi-provider
313
+ * Add optional data sources with:
314
+ * - `provideCoarL10nHttpSource()` - Load L10n overrides from JSON files
315
+ * - `provideCoarI18nHttpSource()` - Load i18n translations from JSON files
316
+ * - Custom sources via multi-providers
328
317
  *
329
318
  * Sources are executed in registration order and deep-merged.
330
319
  *
331
320
  * @example
332
321
  * ```ts
333
- * // Minimal setup (no formatting data)
322
+ * // Basic setup (Intl formatting only, no i18n loader)
334
323
  * provideCoarLocalization({
335
- * availableLanguages: ['en', 'de'],
336
324
  * defaultLanguage: 'en',
337
325
  * })
338
326
  *
339
- * // With Intl defaults
340
- * provideCoarLocalization({...}),
341
- * provideCoarIntlLocalizationSource(),
342
- *
343
- * // With Intl + HTTP overrides
344
- * provideCoarLocalization({...}),
345
- * provideCoarIntlLocalizationSource(),
346
- * provideCoarHttpLocalizationSource({
327
+ * // With L10n overrides and i18n translations
328
+ * provideCoarLocalization({ defaultLanguage: 'en' }),
329
+ * provideCoarL10nHttpSource({
347
330
  * url: (lang) => `/locales/${lang}.json`
348
331
  * }),
332
+ * provideCoarI18nHttpSource(), // Defaults to /i18n/{lang}.json
349
333
  * ```
350
334
  */
351
335
  declare function provideCoarLocalization(config: CoarLocalizationConfig): EnvironmentProviders;
@@ -418,31 +402,6 @@ declare class CoarIntlLocaleDataLoader extends CoarLocalizationDataLoader {
418
402
  private detectPercentFormat;
419
403
  }
420
404
 
421
- /**
422
- * Provides browser Intl API as a locale data source.
423
- *
424
- * This loader detects date/number formatting from the browser's Intl API
425
- * and provides complete locale data for any language without requiring JSON files.
426
- *
427
- * **Recommended as the first source** to provide complete defaults that other
428
- * sources can override.
429
- *
430
- * @example
431
- * ```ts
432
- * // Intl only (pure browser defaults)
433
- * provideCoarLocalization({ defaultLanguage: 'en', availableLanguages: ['en', 'de'] }),
434
- * provideCoarIntlLocalizationSource(),
435
- *
436
- * // Intl + HTTP overrides (recommended)
437
- * provideCoarLocalization({...}),
438
- * provideCoarIntlLocalizationSource(), // First source (complete defaults)
439
- * provideCoarHttpLocalizationSource({ // Second source (business overrides)
440
- * url: (lang) => `/locales/${lang}.json`
441
- * }),
442
- * ```
443
- */
444
- declare function provideCoarIntlLocalizationSource(): EnvironmentProviders;
445
-
446
405
  /**
447
406
  * Configuration for HTTP locale data source.
448
407
  */
@@ -450,6 +409,8 @@ interface CoarHttpLocaleSourceConfig {
450
409
  /**
451
410
  * URL generator function that returns the URL for a given language.
452
411
  *
412
+ * @default (lang) => `/locales/${lang}.json`
413
+ *
453
414
  * @param language - Language code (e.g., 'en', 'de', 'en-US')
454
415
  * @returns URL to load locale data from
455
416
  *
@@ -460,7 +421,7 @@ interface CoarHttpLocaleSourceConfig {
460
421
  * url: (lang) => `https://cdn.example.com/locales/${lang}.json`
461
422
  * ```
462
423
  */
463
- url: (language: string) => string;
424
+ url?: (language: string) => string;
464
425
  /**
465
426
  * Optional HTTP headers to include in requests.
466
427
  *
@@ -483,18 +444,16 @@ interface CoarHttpLocaleSourceConfig {
483
444
  *
484
445
  * @example
485
446
  * ```ts
486
- * // Basic usage (default URL pattern)
487
- * provideCoarHttpLocalizationSource({
488
- * url: (lang) => `/locales/${lang}.json`
489
- * })
447
+ * // Basic usage (default: /locales/{lang}.json)
448
+ * provideCoarL10nHttpSource()
490
449
  *
491
450
  * // Custom URL pattern
492
- * provideCoarHttpLocalizationSource({
451
+ * provideCoarL10nHttpSource({
493
452
  * url: (lang) => `/api/config/intl-${lang}.json`
494
453
  * })
495
454
  *
496
455
  * // With authentication
497
- * provideCoarHttpLocalizationSource({
456
+ * provideCoarL10nHttpSource({
498
457
  * url: (lang) => `/api/locales/${lang}.json`,
499
458
  * headers: {
500
459
  * 'Authorization': 'Bearer ' + getToken()
@@ -519,7 +478,7 @@ interface CoarHttpLocaleSourceConfig {
519
478
  * }
520
479
  * ```
521
480
  */
522
- declare function provideCoarHttpLocalizationSource(config: CoarHttpLocaleSourceConfig): EnvironmentProviders;
481
+ declare function provideCoarL10nHttpSource(config?: CoarHttpLocaleSourceConfig): EnvironmentProviders;
523
482
 
524
483
  /**
525
484
  * Deep merge utility for locale data.
@@ -759,19 +718,16 @@ declare class CoarI18nContext {
759
718
  * - Reactive Signal-based API
760
719
  *
761
720
  * ## Usage
762
- * Use `provideCoarI18n()` to configure - don't instantiate directly.
721
+ * Use `provideCoarLocalization()` + `provideCoarI18nHttpSource()` to configure.
763
722
  *
764
723
  * @example
765
724
  * ```ts
766
725
  * export const appConfig: ApplicationConfig = {
767
726
  * providers: [
768
727
  * provideCoarLocalization({
769
- * availableLanguages: ['en', 'de'],
770
728
  * defaultLanguage: 'en',
771
729
  * }),
772
- * provideCoarI18n({
773
- * loader: CoarHttpTranslationLoader,
774
- * }),
730
+ * provideCoarI18nHttpSource(),
775
731
  * ],
776
732
  * };
777
733
  * ```
@@ -811,30 +767,6 @@ declare class CoarI18nService implements CoarI18nProvider {
811
767
  static ɵprov: i0.ɵɵInjectableDeclaration<CoarI18nService>;
812
768
  }
813
769
 
814
- /**
815
- * Default/fallback implementation of CoarI18nProvider.
816
- *
817
- * This is a minimal passthrough implementation that returns the key unchanged,
818
- * with placeholder interpolation applied if params are provided.
819
- *
820
- * Used when the app does not provide a custom i18n provider.
821
- * The CoarI18n service wraps this and adds the tWithDefault method.
822
- *
823
- * @example
824
- * ```ts
825
- * import { provideCoarDefaultI18n } from '@cocoar/localization';
826
- *
827
- * export const appConfig: ApplicationConfig = {
828
- * providers: [provideCoarDefaultI18n()],
829
- * };
830
- * ```
831
- */
832
- declare class CoarDefaultI18n implements CoarI18nProvider {
833
- t(key: string, params?: Record<string, unknown>): string;
834
- static ɵfac: i0.ɵɵFactoryDeclaration<CoarDefaultI18n, never>;
835
- static ɵprov: i0.ɵɵInjectableDeclaration<CoarDefaultI18n>;
836
- }
837
-
838
770
  /**
839
771
  * Interpolates {placeholders} in a template string using the given params.
840
772
  *
@@ -933,17 +865,22 @@ declare class CoarTranslationStore {
933
865
  * Stores all translations for a specific language.
934
866
  *
935
867
  * Replaces any existing translations for that language.
868
+ * Automatically flattens nested objects into dot notation.
936
869
  *
937
870
  * @param language - Language code (e.g., 'en', 'de')
938
- * @param translations - Translation key-value pairs
871
+ * @param translations - Translation key-value pairs (flat or nested)
939
872
  *
940
873
  * @example
941
874
  * ```ts
942
- * // HTTP: Load entire language at once
943
- * store.setTranslations('en', { 'hello': 'Hello', 'goodbye': 'Goodbye' });
875
+ * // Flat format
876
+ * store.setTranslations('en', { 'hello': 'Hello', 'app.title': 'My App' });
877
+ *
878
+ * // Nested format (auto-flattened)
879
+ * store.setTranslations('en', { app: { title: 'My App' }, hello: 'Hello' });
880
+ * // Both produce the same result
944
881
  * ```
945
882
  */
946
- setTranslations(language: string, translations: CoarTranslations): void;
883
+ setTranslations(language: string, translations: CoarTranslations | Record<string, unknown>): void;
947
884
  /**
948
885
  * Updates a single translation key for a specific language.
949
886
  *
@@ -968,12 +905,14 @@ declare class CoarTranslationStore {
968
905
  *
969
906
  * Only updates/adds the provided keys, keeps existing keys intact.
970
907
  * Creates the language if it doesn't exist.
908
+ * Automatically flattens nested objects into dot notation.
971
909
  *
972
910
  * @param language - Language code
973
- * @param partialTranslations - Partial translation key-value pairs to merge
911
+ * @param partialTranslations - Partial translation key-value pairs to merge (flat or nested)
974
912
  *
975
913
  * @example
976
914
  * ```ts
915
+ * // Flat format
977
916
  * // Existing: { 'hello': 'Hello', 'goodbye': 'Goodbye' }
978
917
  * store.updateTranslations('en', { 'hello': 'Hi' });
979
918
  * // Result: { 'hello': 'Hi', 'goodbye': 'Goodbye' }
@@ -981,13 +920,20 @@ declare class CoarTranslationStore {
981
920
  *
982
921
  * @example
983
922
  * ```ts
923
+ * // Nested format (auto-flattened)
924
+ * store.updateTranslations('en', { app: { title: 'New Title' } });
925
+ * // Updates 'app.title' key
926
+ * ```
927
+ *
928
+ * @example
929
+ * ```ts
984
930
  * // SignalR: Batch update multiple keys
985
931
  * signalR.on('TranslationsBatchUpdated', ({ lang, updates }) => {
986
932
  * store.updateTranslations(lang, updates);
987
933
  * });
988
934
  * ```
989
935
  */
990
- updateTranslations(language: string, partialTranslations: CoarTranslations): void;
936
+ updateTranslations(language: string, partialTranslations: CoarTranslations | Record<string, unknown>): void;
991
937
  /**
992
938
  * Checks if translations for a language are loaded.
993
939
  *
@@ -1053,18 +999,13 @@ declare abstract class CoarTranslationLoader {
1053
999
  /**
1054
1000
  * HTTP-based translation loader.
1055
1001
  *
1056
- * Loads translation JSON files from a configurable base path.
1057
- *
1058
- * ## Default behavior
1059
- * - Base path: `/i18n/`
1060
- * - File pattern: `{language}.json`
1061
- * - Example: `/i18n/en.json`, `/i18n/de.json`
1002
+ * Loads translation JSON files from a configurable URL function.
1062
1003
  *
1063
- * ## Custom path
1004
+ * ## Usage
1064
1005
  * ```ts
1065
1006
  * const loader = new CoarHttpTranslationLoader();
1066
- * loader.basePath = '/assets/translations/';
1067
- * // Loads: /assets/translations/en.json
1007
+ * loader.urlFn = (lang) => `/i18n/${lang}.json`;
1008
+ * loader.headers = { 'Authorization': 'Bearer token' };
1068
1009
  * ```
1069
1010
  *
1070
1011
  * ## Expected JSON format
@@ -1078,117 +1019,102 @@ declare abstract class CoarTranslationLoader {
1078
1019
  */
1079
1020
  declare class CoarHttpTranslationLoader implements CoarTranslationLoader {
1080
1021
  private readonly http;
1081
- /** Base path for translation files */
1082
- basePath: string;
1022
+ /** URL generator function */
1023
+ urlFn: (language: string) => string;
1024
+ /** Optional HTTP headers */
1025
+ headers?: Record<string, string>;
1083
1026
  loadTranslations(language: string): Observable<CoarTranslations>;
1084
1027
  static ɵfac: i0.ɵɵFactoryDeclaration<CoarHttpTranslationLoader, never>;
1085
1028
  static ɵprov: i0.ɵɵInjectableDeclaration<CoarHttpTranslationLoader>;
1086
1029
  }
1087
1030
 
1088
1031
  /**
1089
- * Configuration for Cocoar i18n system.
1032
+ * Configuration for HTTP translation source.
1090
1033
  */
1091
- interface CoarI18nConfig {
1034
+ interface CoarI18nHttpSourceConfig {
1092
1035
  /**
1093
- * Translation loader to use.
1036
+ * URL generator function that returns the URL for a given language.
1094
1037
  *
1095
- * Provide a class that implements `CoarTranslationLoader`.
1038
+ * @default (lang) => `/i18n/${lang}.json`
1096
1039
  *
1097
- * ## Built-in loaders
1098
- * - `CoarHttpTranslationLoader` - Load from HTTP (default)
1040
+ * @param language - Language code (e.g., 'en', 'de', 'en-US')
1041
+ * @returns URL to load translations from
1099
1042
  *
1100
- * ## Custom loader
1043
+ * @example
1101
1044
  * ```ts
1102
- * @Injectable()
1103
- * class MyLoader implements CoarTranslationLoader {
1104
- * loadTranslations(lang: string): Observable<CoarTranslations> {
1105
- * // Your implementation
1106
- * }
1107
- * }
1108
- *
1109
- * provideCoarI18n({ loader: MyLoader })
1045
+ * url: (lang) => `/i18n/${lang}.json`
1046
+ * url: (lang) => `/api/translations/${lang}.json`
1047
+ * url: (lang) => `https://cdn.example.com/i18n/${lang}.json`
1110
1048
  * ```
1111
1049
  */
1112
- loader?: Type<CoarTranslationLoader>;
1050
+ url?: (language: string) => string;
1113
1051
  /**
1114
- * Base path for HTTP loader.
1115
- *
1116
- * Only used when `loader` is `CoarHttpTranslationLoader`.
1052
+ * Optional HTTP headers to include in requests.
1117
1053
  *
1118
- * @default '/i18n/'
1054
+ * @example
1055
+ * ```ts
1056
+ * headers: {
1057
+ * 'Authorization': 'Bearer token123',
1058
+ * 'X-Custom-Header': 'value'
1059
+ * }
1060
+ * ```
1119
1061
  */
1120
- basePath?: string;
1062
+ headers?: Record<string, string>;
1121
1063
  }
1122
1064
  /**
1123
- * Provides complete Cocoar i18n system.
1065
+ * Provides HTTP as a translation source.
1124
1066
  *
1125
- * Sets up translation loading, storage, and automatic language synchronization.
1067
+ * This loader fetches translation files from HTTP endpoints.
1068
+ * Typically used for standard i18n scenarios where translations are stored as JSON files.
1126
1069
  *
1127
- * ## Features
1128
- * - Automatic translation loading when language changes
1129
- * - Preloads initial language (prevents flash of untranslated content)
1130
- * - Signal-based reactive updates
1131
- * - Customizable loader (HTTP, SignalR, static, etc.)
1070
+ * **Note:** The i18n system is automatically included by `provideCoarLocalization()`.
1071
+ * This function only registers the HTTP loader for loading translations.
1132
1072
  *
1133
- * ## Basic usage
1073
+ * @example
1134
1074
  * ```ts
1135
- * import { provideCoarLocalization } from '@cocoar/localization';
1136
- * import { provideCoarI18n } from '@cocoar/localization';
1075
+ * // Basic usage (default: /i18n/{lang}.json)
1076
+ * provideCoarI18nHttpSource()
1137
1077
  *
1138
- * export const appConfig: ApplicationConfig = {
1139
- * providers: [
1140
- * provideHttpClient(),
1141
- * provideCoarLocalization({
1142
- * availableLanguages: ['en', 'de', 'fr'],
1143
- * defaultLanguage: 'en',
1144
- * }),
1145
- * provideCoarI18n(),
1146
- * ],
1147
- * };
1148
- * ```
1078
+ * // Custom path
1079
+ * provideCoarI18nHttpSource({
1080
+ * url: (lang) => `/assets/translations/${lang}.json`
1081
+ * })
1149
1082
  *
1150
- * ## Custom HTTP path
1151
- * ```ts
1152
- * provideCoarI18n({
1153
- * basePath: '/assets/translations/',
1083
+ * // With authentication
1084
+ * provideCoarI18nHttpSource({
1085
+ * url: (lang) => `/api/translations/${lang}.json`,
1086
+ * headers: {
1087
+ * 'Authorization': 'Bearer ' + getToken()
1088
+ * }
1154
1089
  * })
1155
1090
  * ```
1156
1091
  *
1157
- * ## Custom loader (e.g., SignalR)
1158
- * ```ts
1159
- * @Injectable()
1160
- * class SignalRTranslationLoader implements CoarTranslationLoader {
1161
- * loadTranslations(lang: string): Observable<CoarTranslations> {
1162
- * return this.signalR.getTranslations(lang);
1092
+ * Expected file structure:
1093
+ * ```
1094
+ * public/i18n/
1095
+ * en.json - English translations
1096
+ * de.json - German translations
1097
+ * ```
1098
+ *
1099
+ * JSON files should contain flat or nested translations:
1100
+ * ```json
1101
+ * {
1102
+ * "app": {
1103
+ * "title": "My App",
1104
+ * "button": {
1105
+ * "save": "Save",
1106
+ * "cancel": "Cancel"
1107
+ * }
1163
1108
  * }
1164
1109
  * }
1165
- *
1166
- * provideCoarI18n({
1167
- * loader: SignalRTranslationLoader,
1168
- * })
1169
1110
  * ```
1170
1111
  *
1171
1112
  * @param config - Optional configuration
1172
- * @returns Environment providers for the i18n system
1173
- */
1174
- declare function provideCoarI18n(config?: CoarI18nConfig): EnvironmentProviders;
1175
-
1176
- /**
1177
- * Provides the default i18n provider.
1178
- *
1179
- * This minimal provider returns keys unchanged with interpolation applied.
1180
- * Use this as a fallback when no translation engine is configured.
1113
+ * @returns Angular environment providers
1181
1114
  *
1182
- * @example
1183
- * ```ts
1184
- * import { provideCoarDefaultI18n } from '@cocoar/localization';
1185
- *
1186
- * export const appConfig: ApplicationConfig = {
1187
- * providers: [provideCoarDefaultI18n()],
1188
- * };
1189
- * ```
1115
+ * @see {@link CoarI18nHttpSourceConfig} for configuration options
1190
1116
  */
1191
- declare function provideCoarDefaultI18n(): Provider;
1117
+ declare function provideCoarI18nHttpSource(config?: CoarI18nHttpSourceConfig): EnvironmentProviders;
1192
1118
 
1193
- export { COAR_I18N_PROVIDER, COAR_LOCALIZATION_CONFIG, COAR_LOCALIZATION_DATA_LOADERS, CoarCurrencyPipe, CoarDatePipe, CoarDefaultI18n, CoarHttpLocaleDataLoader, CoarHttpTranslationLoader, CoarI18n, CoarI18nContext, CoarI18nPipe, CoarI18nService, CoarIntlLocaleDataLoader, CoarLocalizationDataLoader, CoarLocalizationDataStore, CoarLocalizationService, CoarNumberPipe, CoarPercentPipe, CoarTranslationLoader, CoarTranslationStore, coarInterpolate, coarIsMissingTranslation, mergeLocalizationData, provideCoarDefaultI18n, provideCoarHttpLocalizationSource, provideCoarI18n, provideCoarIntlLocalizationSource, provideCoarLocalization };
1194
- export type { CoarCurrencyFormatData, CoarDateFormatData, CoarHttpLocaleSourceConfig, CoarI18nConfig, CoarI18nProvider, CoarLocalizationConfig, CoarLocalizationData, CoarNumberFormatData, CoarPercentFormatData, CoarTranslations };
1119
+ export { COAR_I18N_PROVIDER, COAR_LOCALIZATION_CONFIG, COAR_LOCALIZATION_DATA_LOADERS, CoarCurrencyPipe, CoarDatePipe, CoarHttpLocaleDataLoader, CoarHttpTranslationLoader, CoarI18n, CoarI18nContext, CoarI18nPipe, CoarI18nService, CoarIntlLocaleDataLoader, CoarLocalizationDataLoader, CoarLocalizationDataStore, CoarLocalizationService, CoarNumberPipe, CoarPercentPipe, CoarTranslationLoader, CoarTranslationStore, coarInterpolate, coarIsMissingTranslation, mergeLocalizationData, provideCoarI18nHttpSource, provideCoarL10nHttpSource, provideCoarLocalization };
1120
+ export type { CoarCurrencyFormatData, CoarDateFormatData, CoarHttpLocaleSourceConfig, CoarI18nHttpSourceConfig, CoarI18nProvider, CoarLocalizationConfig, CoarLocalizationData, CoarNumberFormatData, CoarPercentFormatData, CoarTranslations };