13

I want to get the android device language from the app developed using Ionic3, Cordova and Angular4.

How can I get it?

David
  • 7,387
  • 3
  • 22
  • 39
Gnik
  • 7,120
  • 20
  • 79
  • 129

6 Answers6

13

window.navigator.language worked for me with no plugin. I didn't check to see how the results compare to the Globalization plugin, but I got results like: Android: "en-us", "es-us" and on iOS: "en-US" and "es-XL"

also see https://stackoverflow.com/a/4079798/431296 -window.navigator.userLanguage wasn't defined on either Android or iOS, so I didn't include it

Stan Kurdziel
  • 5,476
  • 1
  • 43
  • 42
  • For those of you who are wondering, "window.navigator.language" is part of the ECMA Internationalization API which is what the Ionic team recommends using instead of the Globalization Plugin. See Filip Floryan's answer to this question for more details: https://stackoverflow.com/a/53831546/3513260 – Fearnbuster Jan 19 '22 at 14:35
12

You can use cordova-plugin-globalization which also offers an ionic-native wrapper. It offers a lot of useful methods, but you are probably looking for getPreferredLanguage() or getLocaleName().

Installation:

ionic cordova plugin add cordova-plugin-globalization
npm install --save @ionic-native/globalization

Example:

import { Globalization } from '@ionic-native/globalization';
constructor(private globalization: Globalization) { 
  this.globalization.getPreferredLanguage()
    .then(res => console.log(res))
    .catch(e => console.log(e));
}
David
  • 7,387
  • 3
  • 22
  • 39
6

Follow up to David's answer, the Globalization API is deprecated.

With the ECMA Internationalization API now supported on iOS, Android and Windows devices, this plugin is not required any more. * Migrating from this plugin to the ECMA Internationalization API is explained in this Cordova blog post.

Filip Floryan
  • 145
  • 2
  • 6
  • 1
    To save some people a little bit of digging: the blog post mentioned in this reply recommends using "window.navigator.language" (although they use it without the leading "window"). This is what Stan Kurdziel was referring to in his answer to this question: https://stackoverflow.com/a/50457743/3513260 – Fearnbuster Jan 19 '22 at 14:40
3

If you're using ngx-translate,

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

constructor(private translate: TranslateService) {
  console.log(this.translate.getBrowserLang());
}

works fine on the devices and in the browser. It's returning "en" or "de".

moritz.vieli
  • 1,747
  • 1
  • 14
  • 17
1

For ionic 5 with capacitor:

npm install @capacitor/device
npx cap sync

in code:

import { Device } from '@capacitor/device';
console.log((await Device.getLanguageCode()).value); // en-US

https://capacitorjs.com/docs/apis/device

mixalbl4
  • 3,507
  • 1
  • 30
  • 44
0

Ionic 4

In Ionic we call it Globalization performs operations specific to the user's locale, language, and timezone.

Installation : Terminal

ionic cordova plugin add cordova-plugin-globalization
npm install @ionic-native/globalization

Usages: Any Component or service file.

import { Globalization } from '@ionic-native/globalization/ngx';
constructor(private global: Globalization) { }
...


this.global.getPreferredLanguage()
  .then(res => console.log(res))
  .catch(e => console.log(e));