ngx-translate
is an Angular module designed to provide multilingual support through the use of pipes, directives, and a service for managing translations.
To install the module, run:
waw add ngx-translate
First, import the TranslateModule
in your Angular application:
import { TranslateModule } from 'ngx-translate';
@NgModule({
imports: [TranslateModule],
})
export class AppModule {}
The translate
pipe can be used in templates to translate keys into their corresponding language strings:
<p>{{ 'home.title' | translate }}</p>
The translate
directive automatically translates the inner content of an element:
<h1 translate>home.title</h1>
The TranslateService
provides methods for managing translations, setting languages, and retrieving translated strings.
To translate a string programmatically, use the TranslateService
:
import { TranslateService } from 'ngx-translate';
@Component({
selector: 'app-home',
templateUrl: './home.component.html'
})
export class HomeComponent {
constructor(private translateService: TranslateService) {}
getTranslatedText() {
return this.translateService.translate('home.title');
}
}
You can set or switch the current language using the TranslateService
:
this.translateService.set_language({ code: 'fr', name: 'French', origin: 'French' });
You can download all translations as a JSON file:
this.translateService.download_json();
MIT License. See LICENSE file for details.