简体   繁体   English

获取 Ionic 应用程序的当前语言

[英]Get current language of the Ionic App

I am developing an Ionic 3 App based on WooCommerce database using rest API.我正在使用 rest API 开发基于 WooCommerce 数据库的 Ionic 3 应用程序。

I use Polylang Plugin to translate my WorrdPress website, and because this plugin does not support rest API, I decided to use "include" parameter when I query categories, in order to get them in one language.我使用 Polylang Plugin 来翻译我的 WordPress 网站,由于该插件不支持 rest API,我决定在查询类别时使用“include”参数,以便以一种语言获取它们。

Ex:前任:

this.http.get(this.config.setUrl('GET', '/wp-json/wc/v2/products/categories?', {
  per_page: 120, include: '151,152,929'
})

Is there a way to check the current language of the app, and based on that to query certain categories?有没有办法检查应用程序的当前语言,并基于它来查询某些类别?

Something like:就像是:

if ( currentLang = 'en' ){
this.http.get(this.config.setUrl('GET', '/wp-json/wc/v2/products/categories?', {
per_page: 120, include: '151,152,929'
}else ... 

Thank you in advance!先感谢您!

You have to use Ionic native Globalization plugin for get current language,您必须使用Ionic 本地全球化插件来获取当前语言,

import { Globalization } from '@ionic-native/globalization';

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

Thank you.谢谢你。

You can do that using ngx-translate你可以使用ngx-translate

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

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

Hint:暗示:
Ionic native Globalization plugin is deprecated离子原生全球化插件已被弃用
https://github.com/apache/cordova-plugin-globalization#deprecation-notice https://github.com/apache/cordova-plugin-globalization#deprecation-notice

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM