@basis-ng/primitives 0.0.1-alpha.86 → 0.0.1-alpha.87

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, inject, RendererFactory2, PLATFORM_ID, Injectable, input, ElementRef, Component, TemplateRef, Directive, computed, output, contentChildren, effect, viewChild, contentChild, linkedSignal, model, forwardRef, HostListener, afterNextRender, afterRenderEffect, ViewEncapsulation, EventEmitter } from '@angular/core';
2
+ import { signal, inject, RendererFactory2, PLATFORM_ID, Injectable, input, ElementRef, Component, TemplateRef, Directive, computed, output, contentChildren, effect, viewChild, contentChild, linkedSignal, model, forwardRef, HostListener, afterNextRender, afterRenderEffect, ViewEncapsulation, EventEmitter, ChangeDetectorRef, Pipe } from '@angular/core';
3
3
  import { isPlatformBrowser, NgStyle, NgClass, CommonModule, NgTemplateOutlet } from '@angular/common';
4
4
  import { NgModel, NG_VALUE_ACCESSOR, ControlContainer } from '@angular/forms';
5
5
  import * as i1 from '@angular/cdk/listbox';
@@ -16,6 +16,7 @@ import * as i1$3 from '@angular/cdk/menu';
16
16
  import { CdkMenu, CdkMenuItem, CdkMenuGroup, CdkMenuItemCheckbox, CdkMenuItemRadio, CdkMenuTrigger } from '@angular/cdk/menu';
17
17
  import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';
18
18
  import { CdkDialogContainer, DialogRef, Dialog } from '@angular/cdk/dialog';
19
+ import { HttpClient } from '@angular/common/http';
19
20
 
20
21
  class ThemeService {
21
22
  /**
@@ -3763,6 +3764,117 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.2.3", ngImpor
3763
3764
  }]
3764
3765
  }] });
3765
3766
 
3767
+ /**
3768
+ * Service for managing translations and language switching.
3769
+ *
3770
+ * @remarks
3771
+ * - Loads language files via HTTP.
3772
+ * - Provides reactive signals for language and dictionary.
3773
+ * - Use `translate(key)` to get the translated string for the current language.
3774
+ *
3775
+ * @example
3776
+ * ```typescript
3777
+ * translationService.setLanguage('es');
3778
+ * translationService.translate('home.title');
3779
+ * ```
3780
+ */
3781
+ class TranslationService {
3782
+ /** Current language code. */
3783
+ _language = signal('en', ...(ngDevMode ? [{ debugName: "_language" }] : []));
3784
+ /** Dictionary of translations. */
3785
+ _dictionary = signal({}, ...(ngDevMode ? [{ debugName: "_dictionary" }] : []));
3786
+ /** Reactive signal for current language. */
3787
+ language = computed(() => this._language(), ...(ngDevMode ? [{ debugName: "language" }] : []));
3788
+ /** Reactive signal for current dictionary. */
3789
+ dictionary = computed(() => this._dictionary(), ...(ngDevMode ? [{ debugName: "dictionary" }] : []));
3790
+ /** HttpClient for loading language files. */
3791
+ http = inject(HttpClient);
3792
+ /**
3793
+ * Sets the current language and loads its dictionary.
3794
+ * @param lang - Language code (e.g., 'en', 'es').
3795
+ */
3796
+ setLanguage(lang) {
3797
+ this._language.set(lang);
3798
+ this.loadLanguage(lang);
3799
+ }
3800
+ /**
3801
+ * Sets the translation dictionary directly.
3802
+ * @param dict - Translation dictionary object.
3803
+ */
3804
+ setDictionary(dict) {
3805
+ this._dictionary.set(dict);
3806
+ }
3807
+ /**
3808
+ * Translates a key using the current language.
3809
+ * @param key - Dot-separated translation key (e.g., 'home.title').
3810
+ * @returns Translated string or the key if not found.
3811
+ */
3812
+ translate(key) {
3813
+ const lang = this._language();
3814
+ const dict = this._dictionary();
3815
+ const value = this.resolveKey(dict[lang], key);
3816
+ return value ?? key;
3817
+ }
3818
+ /**
3819
+ * Resolves a dot-separated key in an object.
3820
+ * @param obj - Object to resolve key in.
3821
+ * @param key - Dot-separated key string.
3822
+ * @returns Value or undefined if not found.
3823
+ */
3824
+ resolveKey(obj, key) {
3825
+ if (!obj)
3826
+ return undefined;
3827
+ return key
3828
+ .split('.')
3829
+ .reduce((acc, part) => (acc && acc[part] !== undefined ? acc[part] : undefined), obj);
3830
+ }
3831
+ /**
3832
+ * Loads a language file via HTTP and updates the dictionary.
3833
+ * @param lang - Language code to load.
3834
+ */
3835
+ loadLanguage(lang) {
3836
+ this.http.get(`/lang/${lang}.json`).subscribe({
3837
+ next: translations => {
3838
+ const dict = { ...this._dictionary() };
3839
+ dict[lang] = translations;
3840
+ this._dictionary.set(dict);
3841
+ },
3842
+ error: () => {
3843
+ // If loading fails, do not update the dictionary
3844
+ },
3845
+ });
3846
+ }
3847
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.2.3", ngImport: i0, type: TranslationService, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
3848
+ static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.2.3", ngImport: i0, type: TranslationService, providedIn: 'root' });
3849
+ }
3850
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.2.3", ngImport: i0, type: TranslationService, decorators: [{
3851
+ type: Injectable,
3852
+ args: [{ providedIn: 'root' }]
3853
+ }] });
3854
+
3855
+ class TranslatePipe {
3856
+ translationService = inject(TranslationService);
3857
+ cdr = inject(ChangeDetectorRef);
3858
+ lastLang;
3859
+ transform(key) {
3860
+ const lang = this.translationService.language();
3861
+ if (lang !== this.lastLang) {
3862
+ this.lastLang = lang;
3863
+ this.cdr.markForCheck();
3864
+ }
3865
+ return this.translationService.translate(key);
3866
+ }
3867
+ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.2.3", ngImport: i0, type: TranslatePipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe });
3868
+ static ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "20.2.3", ngImport: i0, type: TranslatePipe, isStandalone: true, name: "translate", pure: false });
3869
+ }
3870
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.2.3", ngImport: i0, type: TranslatePipe, decorators: [{
3871
+ type: Pipe,
3872
+ args: [{
3873
+ name: 'translate',
3874
+ pure: false,
3875
+ }]
3876
+ }] });
3877
+
3766
3878
  /*
3767
3879
  * Public API Surface of basis-ng
3768
3880
  */
@@ -3772,5 +3884,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.2.3", ngImpor
3772
3884
  * Generated bundle index. Do not edit.
3773
3885
  */
3774
3886
 
3775
- export { AlertComponent, BadgeComponent, ButtonComponent, ButtonGroupComponent, CardComponent, CardContentComponent, CardDescriptionComponent, CardFooterComponent, CardHeaderComponent, CardSubtitleComponent, CardTitleComponent, Checkbox, ColorPickerComponent, ComboboxComponent, CommandComponent, CommandOptionsComponent, DialogDirective, DialogService, DrawerComponent, IconComponent, InViewportDirective, InViewportService, InputComponent, InputGroupComponent, LazyContentDirective, MenuComponent, MenuGroupComponent, MenuItemCheckboxComponent, MenuItemComponent, MenuItemRadioComponent, MenuLabelComponent, MenuTriggerDirective, OptionComponent, OtpComponent, OtpDigitDirective, OverlayDirective, OverlayTriggerDirective, RangeComponent, ResponsiveService, RowComponent, RowItemComponent, SelectComponent, SelectOptionsComponent, SheetComponent, SpinnerComponent, SwitchComponent, TabComponent, TableComponent, TabsComponent, TextareaComponent, ThemeService, TooltipDirective, TreeComponent, TreeNodeComponent };
3887
+ export { AlertComponent, BadgeComponent, ButtonComponent, ButtonGroupComponent, CardComponent, CardContentComponent, CardDescriptionComponent, CardFooterComponent, CardHeaderComponent, CardSubtitleComponent, CardTitleComponent, Checkbox, ColorPickerComponent, ComboboxComponent, CommandComponent, CommandOptionsComponent, DialogDirective, DialogService, DrawerComponent, IconComponent, InViewportDirective, InViewportService, InputComponent, InputGroupComponent, LazyContentDirective, MenuComponent, MenuGroupComponent, MenuItemCheckboxComponent, MenuItemComponent, MenuItemRadioComponent, MenuLabelComponent, MenuTriggerDirective, OptionComponent, OtpComponent, OtpDigitDirective, OverlayDirective, OverlayTriggerDirective, RangeComponent, ResponsiveService, RowComponent, RowItemComponent, SelectComponent, SelectOptionsComponent, SheetComponent, SpinnerComponent, SwitchComponent, TabComponent, TableComponent, TabsComponent, TextareaComponent, ThemeService, TooltipDirective, TranslatePipe, TranslationService, TreeComponent, TreeNodeComponent };
3776
3888
  //# sourceMappingURL=basis-ng-primitives.mjs.map