Angular 10+ localizable for enums
npm install angular-enum-listtypescript
import { AngularEnumsListModule } from 'angular-enum-list';
@NgModule({
bootstrap: [ AppComponent ],
declarations: [
AppComponent
],
import: [
AngularEnumsListModule.forRoot('enums') // first parameter is localization namespace name,
// second parameter is separator, default ':'
]
})
export class AppModule {}
`
You can configurate name of localize namespace for global context and separator which break namespace and path from localize key
Usage
$3
Use "enumList" pipe to get the array with translation keys:
{{ item.name | i18next }}
Pipe has one required parameter "dictName". It's name of dictionary in localization file.
Other params are optional. You can add the folowing parametrs:
nameSpace
`html
{{ item.name | i18next }}
`
You can specify nameSpace parameter for particularly pipes.
ignored
`html
{{ item.name | i18next }}
`
Use "enumKey" to get the key of your enum field with all localization path.
`html
{{ 'keyOfMyEnum' | enumKey: { dictName: 'list', currentEnum: _enums.myEnum, nameSpace: "my-enums"} | i18nextCap}}
`
Pipe has two required parameters: "dictName" and "currentEnum".
"dictName" is the name of dictionary in localization file.
"currentEnum" is original enum, where we search the key.
in the end you get result string looks like:
`html
{{ my-enum:list.keyOfMyEnum | i18nextCap }}
`
nameSpace
`html
{{ item.name | i18next }}
`
Dictionary
Your own dictionary must be looks like:
ru.enums.json
`typescript
{
"SexKind": {
"Undefined": "Не выбрано",
"Male": "Мужской",
"Female": "Женский"
}
}
`
For example you can use this list in native select in html-file:
`html
formControlName="SexKind"
[(ngModel)]="model.SexKind">
[ngValue]='status.id'>
{{ status.name | i18nextCap }}
`
"enums" is public variable which contains enums you need to use in your template:
`typescript
public enums = { SexKind, RaceKind };
``