Is it posible to change the locale from an Ionic app?

I know the Globalization Native Plugin allows for example, to show a date based on the locale of the device. But in order to change the way a date is shown I have to exit the app, change the device language setting and then get back into the app. Is there a way I can change the device language from inside the app OR change the locale of an app based on a simple variable?

Yes, its possible change the language of the app with no need to exit the app. You need the translate module from here and here.

Once you get it installed you just need add this conf:

      loader: {
        provide: TranslateLoader,
        useFactory: httpLoaderFactory,
        deps: [Http]

Add this module on the app.module.ts and this other function:

export function httpLoaderFactory(http: Http) {
  return new TranslateHttpLoader(http, './assets/i18n/', '.json');

So now you have on assets/i18n/en the english translations.

Now you can change the locale just with this method:

import { TranslateService } from '@ngx-translate/core';
public translate: TranslateService

Hope this help you :slight_smile:

1 Like

If it’s only about the date, a pipe like this might be the easier solution. If you want to translate text, @fdom’s answer is the way to go.

Well, then you can make your own date pipe based on your locale :slight_smile:

Do I have any other option? I have 15 different locales to convert to (dates, currency and other locale stuff)

I dont think so… You can make some pipes (datePipe, currencyPipe, otherPipe) :frowning:

Btw you have some built-in pipes from Angular here so you can check if you can use some of them instead of making you one.