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

@larscom/ngx-translate-module-loader

v3.2.0

Published

Highly configurable and flexible translations loader for ngx-translate. Fetch multiple translations, each translation file gets it's own namespace by default

Downloads

24,913

Readme

@larscom/ngx-translate-module-loader

npm-version npm license codecov

Highly configurable and flexible translations loader for @ngx-translate/core. Fetch multiple translations (http only) and configure them to your needs. Each translation file has it's own namespace out of the box so the key/value pairs do not conflict with each other.

View on StackBlitz

Dependencies

@larscom/ngx-translate-module-loader depends on @ngx-translate/core and Angular.

Installation

npm install @larscom/ngx-translate-module-loader

Choose the version corresponding to your Angular version

| @angular/core | @larscom/ngx-translate-module-loader | | ------------- | ------------------------------------ | | >= 12 | >= 3.0.0 | | < 12 | <= 2.2.0 |

Usage

Create an exported moduleHttpLoaderFactory function

import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { HttpClientModule, HttpClient } from '@angular/common/http'
import { TranslateModule, TranslateLoader } from '@ngx-translate/core'
import { ModuleTranslateLoader, IModuleTranslationOptions } from '@larscom/ngx-translate-module-loader'
import { AppComponent } from './app'

export function moduleHttpLoaderFactory(http: HttpClient) {
  const baseTranslateUrl = './assets/i18n'

  const options: IModuleTranslationOptions = {
    modules: [
      // final url: ./assets/i18n/en.json
      { baseTranslateUrl },
      // final url: ./assets/i18n/feature1/en.json
      { baseTranslateUrl, moduleName: 'feature1' },
      // final url: ./assets/i18n/feature2/en.json
      { baseTranslateUrl, moduleName: 'feature2' }
    ]
  }

  return new ModuleTranslateLoader(http, options)
}

@NgModule({
  imports: [
    BrowserModule,
    HttpClientModule,
    TranslateModule.forRoot({
      loader: {
        provide: TranslateLoader,
        useFactory: moduleHttpLoaderFactory,
        deps: [HttpClient]
      }
    })
  ],
  bootstrap: [AppComponent]
})
export class AppModule {
  constructor(readonly translate: TranslateService) {
    translate.setDefaultLang('en')
  }
}

Namespacing

By default, each translation file gets it's own namespace based on the moduleName, what does it mean?

For example with these options:

export function moduleHttpLoaderFactory(http: HttpClient) {
  const baseTranslateUrl = './assets/i18n'

  const options: IModuleTranslationOptions = {
    modules: [
      // no moduleName/namespace
      { baseTranslateUrl },
      // namespace: FEATURE1
      { baseTranslateUrl, moduleName: 'feature1' },
      // namespace: FEATURE2
      { baseTranslateUrl, moduleName: 'feature2' }
    ]
  }
  return new ModuleTranslateLoader(http, options)
}

Lets say each module in the above example resolves to the following JSON:

{
  "KEY": "VALUE"
}

The final translation you are working with would be:

{
  "KEY": "VALUE",
  "FEATURE1": {
    "KEY": "VALUE"
  },
  "FEATURE2": {
    "KEY": "VALUE"
  }
}

Even though all JSON files from those modules are the same, they don't conflict because they are not on the same level after they get merged.

Configuration

The configuration is very flexible, you can even define custom templates for fetching translations.

interface IModuleTranslationOptions {
  /**
   * The translation module configurations
   */
  modules: IModuleTranslation[]
  /**
   * By default, each module gets its own namespace so it doesn't conflict with other modules
   */
  disableNamespace?: boolean
  /**
   * By default, namespaces are uppercase
   */
  lowercaseNamespace?: boolean
  /**
   * By default, it'll perform a deepmerge when merging translation files
   */
  deepMerge?: boolean
  /**
   * Set a version to prevent the browser from caching the translation files.
   * Each translation will get a query parameter with the version number
   * @example 'en.json?v=123'
   */
  version?: string | number
  /**
   * Function that gets executed if an error occurred while retrieving a translation file
   * @param error the error that occurred
   * @param path the path to the location file
   */
  translateError?: (error: any, path: string) => void
  /**
   * Custom translate merge function after retrieving all translation files
   * @param translations the resolved translation files
   */
  translateMerger?: (translations: Translation[]) => Translation
  /**
   * Provide custom headers at 'root' level, which means this headers gets added to every request
   * unless you specify headers at 'module' level.
   * @see modules
   */
  headers?: HttpHeaders
}
interface IModuleTranslation {
  /**
   * The module name
   *
   * For example: shared
   * @description omit moduleName if you have a translate file at baseTranslateUrl level
   * @see baseTranslateUrl
   */
  moduleName?: string
  /**
   * The base translate URL
   *
   * For example: ./assets/i18n
   * @description the final url will then be: ./assets/i18n/shared if the moduleName is shared
   * @see moduleName
   */
  baseTranslateUrl: string
  /**
   * By default, it uses the moduleName as namespace
   * @see moduleName
   *
   * Use this property if you want to override the default namespace
   */
  namespace?: string
  /**
   * Custom translation map function after retrieving a translation file
   * @param translation the resolved translation file
   */
  translateMap?: (translation: Translation) => Translation
  /**
   * Custom path template for fetching translations
   * @example
   * '{baseTranslateUrl}/{moduleName}/{language}'
   * or
   * @example
   * '{baseTranslateUrl}/{language}'
   *
   * It depends whether you have a moduleName defined
   * @see moduleName
   */
  pathTemplate?: string
  /**
   * Provide custom headers at 'module' level. These headers only apply to this module.
   */
  headers?: HttpHeaders
}

Custom templates for fetching translations

By default, translations gets fetched by using the following template:

'{baseTranslateUrl}/{moduleName}/{language}' e.g. ./assets/feature1/en.json

You can override this option if you wish to do so:

const options: IModuleTranslationOptions = {
  modules: [
    // resolves to: ./assets/my-path/en.json
    { baseTranslateUrl, pathTemplate: '{baseTranslateUrl}/my-path/{language}' },
    // resolves to: ./assets/my-path/en/feature1.json
    { baseTranslateUrl, moduleName: 'feature1', pathTemplate: '{baseTranslateUrl}/my-path/{language}/{moduleName}' },
    // resolves to: ./assets/my-path/en/feature2.json
    { baseTranslateUrl, moduleName: 'feature2', pathTemplate: '{baseTranslateUrl}/my-path/{language}/{moduleName}' }
  ]
}

Custom headers

  const options: IModuleTranslationOptions = {
    // global headers, applied to every request, unless you specify headers at 'module' level
    headers: new HttpHeaders().set('Header-Name', 'Header value')
    modules: [
      { baseTranslateUrl },
      // headers only applied to this module
      { baseTranslateUrl, moduleName: 'feature1', headers: new HttpHeaders().set('Header-Name', 'Header value') },
      { baseTranslateUrl, moduleName: 'feature2' }
    ]
  };