7.0.0 • Published 1 year ago

translate-object-loader v7.0.0

Weekly downloads
14
License
MIT
Repository
-
Last release
1 year ago

TranslateObjectLoader

Simple loader for ngx-translate that uses an object library.

Maintenance license npm

GitHub Workflow Status Codecov Known Vulnerabilities

Why?

This loader is useful if you have multiple sub-modules in your Angular application which each have their own translation definitions and you want to have them all managed separately.

Supported versions

Angular

AngularSupported
< 6.0.0not tested
>= 6.0.0 <= 10.0.0yes (2.x.x)
11.x.xyes (3.x.x)
12.x.xyes (4.x.x)
13.x.xyes (5.x.x)
14.x.xyes (6.x.x)
15.x.xyes (7.x.x)

ngx-translate

ngx-translateSupported
< 10.0.0not tested
>= 10.0.0 <= 13.0.0yes (>= 2.x.x)
14.x.xyes (>=5.x.x)

Installation

npm install --save translate-object-loader

Usage

You can also check out the demo project if you just want to see the code.

Follow the instructions at @ngx-translate/core on how to install ngx-translate itself.

Then simply import the loader and provide it to the TranslateModule.

import { TranslateLoader, TranslateModule } from '@ngx-translate/core';
import { TranslateObjectLoader } from 'translate-object-loader';

@NgModule({
    imports: [
        BrowserModule,
        TranslateModule.forRoot({
            loader:
              provide: TranslateLoader,
              useClass: TranslateObjectLoader,
            }
        })
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

Now, adding your translation definition is pretty simple. The only thing to keep in mind is that the top level objects of your definition should always be a language code because we select the language based on that.

Also, the last definition you import will always overwrite any keys that might already be defined in previous definitions.

// app.module.ts

import { Library } from 'translate-object-loader';
import { firstModuleTranslations } from 'first-module';
import { secondModuleTranslations } from 'second-module';

// Import each separately
Library.add(firstModuleTranslations);
Library.add(secondModuleTranslations);

// Or import multiple at the same time
Library.add(firstModuleTranslations, secondModuleTranslations);
// first-module.ts

import { TranslationDefinition } from 'translate-object-loader';

export const firstModuleTranslations: TranslationDefinition = {
  de: {
    key1: 'Schlüssel1',
  },
  en: {
    key1: 'Key1',
  },
};
// second-module.ts

import { TranslationDefinition } from 'translate-object-loader';

export const secondModuleTranslations: TranslationDefinition = {
  de: {
    key2: 'Schlüssel2',
  },
  en: {
    key1: 'Overwriting',
    key2: 'Key2',
  },
};
7.0.0

1 year ago

6.0.0

1 year ago

5.0.0

2 years ago

4.0.0

3 years ago

3.0.0

3 years ago

2.0.3

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

1.0.0

5 years ago