nga-translate
v19.0.0
Published
Angular ngx-translate wrapper for support default translate and prefixed translations
Maintainers
Readme
ngaTranslate
A wrapper for ngx-translate library that supports default and prefixed translations.
ngaTranslate is an Angular library that provides pipes and directives to simplify text translation in Angular applications.
It is a wrapper for the popular ngx-translate library and adds useful features such as default translations and prefix-based translations.
Installation
To install ngaTranslate, run the following command in your terminal:
npm install nga-translate --saveUsage
1. Pipe
The ngaTranslate pipe can be used to translate text inside Angular templates.
It accepts up to three arguments:
- Translation key
- Default translation (string or object, used if key is not found)
- Params (object, for dynamic values)
Examples
{{ 'key' | ngaTranslate }} {{ 'key' | ngaTranslate : 'default translate' }} {{ 'key' | ngaTranslate : 'default translate' : { p1: 'value' }
}} {{ 'key' | ngaTranslate : 'default translate [{ p1 }]' : { p1: 'value' } }} {{ 'key' | ngaTranslate : { en: 'Hello [{ p1 }]', fr:
'Bonjour [{ p1 }]' } : { p1: 'value' } }}You can also use the pipe with only default values (without an explicit key):
{{ { en: 'default translate' } | ngaTranslate }} {{ { en: 'default translate [{ p1 }]' } | ngaTranslate : { p1: 'value' } }} {{ { en:
'default [{ p1 }]', fr: 'par défaut [{ p1 }]' } | ngaTranslate : { p1: 'value' } }}2. Directive
The ngaTranslate directive can be used directly on elements.
It supports both a translation key and the element’s content (as default translation).
Examples
<p ngaTranslate>Hello, world!</p>
<p ngaTranslate="key">Hello, world!</p>
<p ngaTranslate="key" [translateValues]="{ p1: 'value' }">Hello, [{ p1 }]</p>
<p ngaTranslate="key" [translateValues]="{ p1: 'value' }">{ en: 'Hello, [{ p1 }]', fr: 'Bonjour, [{ p1 }]' }</p>3. Prefix Directive
The ngaTranslatePrefix directive allows you to define a translation prefix for all child elements.
This is useful for grouping translations under a common namespace.
Examples
<div ngaTranslatePrefix="prefix">
<label ngaTranslate="df.bank">Bank</label>
<!-- Translates using key: prefix.df.bank -->
<button>{{ 'submit' | ngaTranslate : 'Submit' }}</button>
<!-- Translates using key: prefix.submit -->
<button>{{ '.root.submit' | ngaTranslate : 'Root Submit' }}</button>
<!-- Translates using key: root.submit (ignores prefix) -->
</div>Note:
If the key starts with a dot (.), the prefix will be ignored, and the translation will resolve from the root namespace.
Features
- Simplified syntax – Easier and cleaner than raw ngx-translate usage.
- Default translations – Specify fallbacks when keys are missing.
- Contextual translations – Support for parameterized translations.
- Prefix translations – Automatically prepend a prefix to all translation keys within a scope.
- Root override – Use keys starting with
.to bypass the prefix.
Contributing
We welcome contributions to ngaTranslate.
Feel free to open an issue or submit a pull request on GitHub.
License
ngaTranslate is licensed under the MIT License.
