npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

angular-translate-cache

v18.0.2

Published

angular-translate extension to facilitate language cache.

Downloads

187

Readme

angular-translate-cache

NPM version
MIT License

Based on and extended from angular-translate.
Forked from the original repository to introduce improvements by Behram Bazo.

This is a simplified version of localize-router.
If you’re already using localize-router, this extension is likely unnecessary.
The primary purpose of this extension is to facilitate language caching with angular-translate.

| Angular version | Integration branch | Library version | |:---------------:|:------------------:|:---------------:| | 8 and below | angular1-8 | ^0.0.0 | | 9 | angular9 | ^9.0.0 | | 10 | angular10 | ^10.0.0 | | 11 | angular11 | ^11.0.0 | | 12 | angular12 | ^12.0.0 | | 13 | angular13 | ^13.0.0 | | 14 | angular14 | ^14.0.0 | | 15 | angular15 | ^15.0.0 | | 16 | angular16 | ^16.0.0 | | 17 | angular17 | ^17.0.0 | | 18 | angular18 | ^18.0.0 |

Installation

To install this library, run:

$ npm install angular-translate-cache --save

Usage (Standalone Setup)

Angular now supports the standalone components model, enabling more modular design. Here’s how to set up angular-translate-cache using standalone components with Angular's new build system.

import { importProvidersFrom } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import { TranslateModule, TranslateService } from '@angular-translate/core';
import { TranslateCacheModule, TranslateCacheSettings, TranslateCacheService } from 'angular-translate-cache';
import { AppComponent } from './app/app.component';

bootstrapApplication(AppComponent, {
  providers: [
    importProvidersFrom(
      TranslateModule.forRoot(),
      TranslateCacheModule.forRoot({
        cacheService: {
          provide: TranslateCacheService,
          useFactory: (translateService: TranslateService, translateCacheSettings: TranslateCacheSettings) => {
            return new TranslateCacheService(translateService, translateCacheSettings);
          },
          deps: [TranslateService, TranslateCacheSettings],
        },
      })
    ),
  ],
});

Initializing

For standalone components, initialize the translation and caching services within the component itself:

import { Component } from '@angular/core';
import { TranslateService } from '@angular-translate/core';
import { TranslateCacheService } from 'angular-translate-cache';

@Component({
  selector: 'app-root',
  template: `<div>{{ 'HELLO' | translate }}</div>`,
  standalone: true,
  imports: [TranslateService, TranslateCacheService]
})
export class AppComponent {
  constructor(private translateService: TranslateService, private translateCacheService: TranslateCacheService) {
    this.translateService.setDefaultLang('en');
    this.translateCacheService.init();
  }
}

This setup uses the init method of TranslateCacheService to cache the selected language, falling back to the browser’s current language or a default.

Cache Configuration Options

You can customize cache settings by defining options within the TranslateCacheModule.forRoot method:

TranslateCacheModule.forRoot({
  cacheService: {
    provide: TranslateCacheService,
    useFactory: (translateService, translateCacheSettings) => {
      return new TranslateCacheService(translateService, translateCacheSettings);
    },
    deps: [TranslateService, TranslateCacheSettings],
  },
  cacheName: 'mylang',          // Default is 'lang'.
  cacheMechanism: 'Cookie',      // Default is 'LocalStorage'.
  cookieExpiry: 1,               // Default is 720 hours (1 month).
  cookieAttributes: 'SameSite=Strict; Secure' // Additional cookie attributes.
});

License

MIT © Behram Bazo