Issue
I'm making an Angular project where the user has the ability to switch languages. Is it possible to make the locale dynamic?
I have seen that you can add it in the NgModule but i'm guessing it's not dynamic when i put it there? Or can i change it somehow through a service or something?
Solution
Using providers
you can change your default locale in your NgModule
.
to do this You need to import LOCALE_ID from angular/core and fetch your locale language to pass the same to providers.
import { LOCALE_ID } from '@angular/core';
@NgModule({
imports: [//your imports],
providers: [
{ provide: LOCALE_ID, useValue: "en-US" }
]
})
...
...
{
provide: LOCALE_ID,
deps: [SettingsService], //some service handling global settings
useFactory: (settingsService) => settingsService.getLanguage() //returns locale string
}
Hope this will help you.
Answered By - Amol Bhor
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.