@@ -8,52 +8,47 @@ import {
88
99import * as Rdf from '../data/rdf/rdfModel' ;
1010
11+ /**
12+ * Default (fallback) translation bundle `en` language.
13+ *
14+ * @category Constants
15+ */
1116export const DefaultTranslationBundle : TranslationBundle = DefaultBundle ;
1217
1318/**
1419 * Default built-in implementation for i18n strings interpolation and other
1520 * methods from {@link Translation} interface.
21+ *
22+ * @category Core
1623 */
17- export class DefaultTranslation implements Translation {
24+ export class DefaultTranslation < K extends string = TranslationKey > implements Translation < K > {
1825 protected readonly bundles : ReadonlyArray < Partial < TranslationBundle > > ;
1926
2027 private readonly _selectLabel : LabelLanguageSelector ;
2128
2229 constructor ( options ?: {
2330 /**
24- * Additional translation bundles for UI text strings in the workspace
31+ * Translation bundles for UI text strings in the workspace
2532 * in order from higher to lower priority.
2633 *
2734 * @default []
28- * @see {@link useDefaultTranslation }
35+ * @see {@link DefaultTranslationBundle }
2936 */
3037 bundles ?: ReadonlyArray < Partial < TranslationBundle > > ;
31- /**
32- * If set, disables translation fallback which (with default `en` language).
33- *
34- * @default true
35- * @see {@link translations }
36- */
37- useDefaultBundle ?: boolean ;
3838 /**
3939 * Overrides how a single label gets selected from multiple of them based on target language.
4040 */
4141 selectLabel ?: LabelLanguageSelector ;
4242 } ) {
4343 const {
4444 bundles = [ ] ,
45- useDefaultBundle = true ,
4645 selectLabel = defaultSelectLabel ,
4746 } = options ?? { } ;
48- const translationBundles : Partial < TranslationBundle > [ ] = [ ...bundles ] ;
49- if ( useDefaultBundle ) {
50- translationBundles . push ( DefaultTranslationBundle ) ;
51- }
52- this . bundles = translationBundles ;
47+ this . bundles = bundles ;
5348 this . _selectLabel = selectLabel ;
5449 }
5550
56- private getString ( key : TranslationKey ) : string | undefined {
51+ private getString ( key : K ) : string | undefined {
5752 const dotIndex = key . indexOf ( '.' ) ;
5853 if ( ! ( dotIndex > 0 && dotIndex < key . length ) ) {
5954 throw new Error ( `Reactodia: Invalid translation key: ${ key } ` ) ;
@@ -69,12 +64,12 @@ export class DefaultTranslation implements Translation {
6964 return undefined ;
7065 }
7166
72- text ( key : TranslationKey , placeholders ?: Record < string , string | number | boolean > ) : string {
67+ text ( key : K , placeholders ?: Record < string , string | number | boolean > ) : string {
7368 return this . textOptional ( key , placeholders ) ?? key ;
7469 }
7570
7671 textOptional (
77- key : TranslationKey ,
72+ key : K ,
7873 placeholders ?: Record < string , string | number | boolean >
7974 ) : string | undefined {
8075 const template = this . getString ( key ) ;
@@ -84,7 +79,7 @@ export class DefaultTranslation implements Translation {
8479 return formatPlaceholders ( template , placeholders ) ;
8580 }
8681
87- template ( key : TranslationKey , parts : Record < string , React . ReactNode > ) : React . ReactNode {
82+ template ( key : K , parts : Record < string , React . ReactNode > ) : React . ReactNode {
8883 const template = this . getString ( key ) ?? key ;
8984 return templatePlaceholders ( template , parts ) ;
9085 }
0 commit comments