17.1.0 β€’ Published 4 months ago

ngx-translate-db v17.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

ngx-translate-db

A robust, offline-first translation library for Angular applications with IndexedDB support.

Features

  • πŸ”„ Dynamic language switching
  • πŸ’Ύ Offline-first with IndexedDB storage
  • πŸš€ Reactive translations using Observables
  • πŸ” Type-safe translation keys
  • πŸ“± Memory efficient with automatic cleanup
  • 🎯 Zero dependencies (except Angular core)
  • βœ… Angular supports: (17)

Installation

npm install ngx-translate-db

Quick Start

1. Configure in app.config.ts

import { ApplicationConfig } from '@angular/core';
import { provideTranslate } from 'ngx-translate-db';

export const appConfig: ApplicationConfig = {
  providers: [
    // ... other providers
    provideTranslate({
      projectId: 'your-project-id',
      endpoint: 'https://your-api-endpoint',
      defaultLang: 'en',
      apiKey: 'optional-api-key',
      acceptedLanguages: ['en', 'fr', 'it', 'de', '...'],
    }),
  ],
};

2. Use in Components

import { Component } from '@angular/core';
import { TranslatePipe, TranslateService } from 'ngx-translate-db';

@Component({
  selector: 'app-root',
  template: `
    <div>
      <!-- Using the pipe -->
      <p>{{ 'WELCOME_MESSAGE' | appTranslate | async }}</p>
      
      <!-- Language switcher -->
      <button (click)="changeLanguage('en')">English</button>
      <button (click)="changeLanguage('fr')">FranΓ§ais</button>
    </div>
  `,
  imports: [TranslatePipe],
  standalone: true
})
export class AppComponent {
  constructor(private translate: TranslateService) {}

  changeLanguage(lang: string): void {
    this.translate.setLanguage(lang);
  }
}

3. Using the Translation Pipe

The appTranslate pipe is designed to be used with Angular's async pipe:

<!-- Basic usage -->
{{ 'TRANSLATION_KEY' | appTranslate | async }}

<!-- With variables -->
{{ dynamicKey | appTranslate | async }}

<!-- In attributes -->
<div [title]="'TOOLTIP_KEY' | appTranslate | async">

{{ 'LABEL_HELLO' | appTranslate: { name: 'DevFlow' } | async }}

API Reference

TranslateService

The core service for handling translations.

class TranslateService {
  // Get current language
  get currentLanguage(): string;
  
  // Get list of supported languages
  get supportedLanguages(): string[];
  
  // Change language
  async setLanguage(lang: string): Promise<void>;
  
  // Get translation synchronously
  instant(key: string): string;
  
  // Get translation asynchronously
  async translate(key: string): Promise<string>;
  
  // Observable for language changes
  get onLangChange(): Observable<string>;
}

Configuration Options

interface TranslationConfig {
  projectId: string;                // Your project identifier
  endpoint: string;                 // API endpoint for fetching translations
  defaultLang: string;              // Default language to use
  apiKey?: string;                  // Optional API key for authentication
  acceptedLanguages: string[];      // List of supported languages
}

Memory Management

The library is designed to be memory efficient:

  • Uses Angular's async pipe for automatic subscription cleanup
  • No memory leaks as subscriptions are properly managed
  • Efficient change detection through Observable pattern
  • IndexedDB for offline storage without memory overhead

Performance Considerations

  • Translations are cached in IndexedDB
  • Change detection only triggers when needed
  • Lazy loading of translations
  • Efficient pipe implementation with minimal overhead

Browser Support

  • All modern browsers with IndexedDB support
  • Fallback mechanism for older browsers
  • Tested on latest Chrome, Firefox, Safari, and Edge

Contributing

Contributions are welcome! Please read our contributing guidelines before submitting pull requests.

License

MIT License - see LICENSE file for details

17.1.0

4 months ago

17.0.9

4 months ago

17.0.8

4 months ago

17.0.7

4 months ago

17.0.6

4 months ago

17.0.5

4 months ago

17.0.4

4 months ago

17.0.3

4 months ago

17.0.2

4 months ago

17.0.1

4 months ago

17.0.0

4 months ago

1.5.0

4 months ago

1.4.9

4 months ago

1.4.8

4 months ago

1.4.7

4 months ago

1.4.6

4 months ago

1.4.5

4 months ago

1.4.4

4 months ago

1.4.3

4 months ago

1.4.2

4 months ago

1.4.1

4 months ago

1.4.0

4 months ago

1.3.9

4 months ago

1.3.8

4 months ago

1.3.7

4 months ago

1.3.6

4 months ago

1.3.5

4 months ago

1.3.4

4 months ago

1.3.3

4 months ago

1.3.2

4 months ago

1.3.1

4 months ago

1.3.0

4 months ago

1.2.9

4 months ago

1.2.8

4 months ago

1.2.7

4 months ago

1.2.6

4 months ago

1.2.5

4 months ago

1.2.4

4 months ago

1.2.3

4 months ago

1.2.2

4 months ago

1.2.1

4 months ago

1.2.0

4 months ago

1.1.9

4 months ago

1.1.8

4 months ago

1.1.7

4 months ago

1.1.6

4 months ago

1.1.5

4 months ago

1.1.4

4 months ago

1.1.3

4 months ago

1.1.2

4 months ago

1.1.1

4 months ago

1.1.0

4 months ago

1.0.9

4 months ago

1.0.8

4 months ago

1.0.7

4 months ago

1.0.6

4 months ago

1.0.5

4 months ago

1.0.4

4 months ago

1.0.3

4 months ago

1.0.1

4 months ago

1.0.0

4 months ago