alfresco-ng2-components/docs/translation.service.md

1.3 KiB

Translation service

Supports localisation.

Details

Registering translation sources

In order to enable localisation support you will need to create a /resources/i18n/en.json file and register its parent i18n folder with your component or application module.

For example:

import { TRANSLATION_PROVIDER } from '@alfresco/adf-core';

@NgModule({
    ...
    providers: [
        ...
        {
            provide: TRANSLATION_PROVIDER,
            multi: true,
            useValue: {
                name: 'ng2-alfresco-core',
                source: 'assets/ng2-alfresco-core'
            }
        }
    ]
})

Note: the source property points to the web application root, please ensure you have webpack settings to copy all the i18n files at compile time.

index.html
assets/ng2-alfresco-core/i18n/en.json
...

You can register as many entries as you would like.

Switching languages

Depending on your application, you may want to have buttons or dropdown menus to allow language selection for the end users.

You can use TranslationService to switch languages from your code based on input events of your choice:

class MyComponent {
    constructor(private translateService: TranslationService) {
    }

    onLanguageClicked(lang: string) {
        this.translateService.use(lang || 'en');
    }
}