@aligov/global-locale

locale information manager

Usage no npm install needed!

<script type="module">
  import aligovGlobalLocale from 'https://cdn.skypack.dev/@aligov/global-locale';
</script>

README

@aligov/global-locale

主要特征

  • 提供获取和设置地区/语言/货币/时区等 locale 信息能力
  • 遵循国际化开发规约
  • 抹平不同容器(web/weex/windVane/小程序)之间的差异

安装

tnpm install @aligov/global-locale --save

API

getLocale

获取当前 locale 信息

import locale from '@aligov/global-locale';

const { region,lang, currency, tz} = locale.getLocale();

console.log(region,lang, currency, tz); // CN, zh-CN, CNY, Asia/Shanghai

注意: 所有的locale 信息的 set 操作, 在 web 环境下生效, weex 和 windVane 跟随应用的设置.

setRegion

locale cookie 存储默认 domain 为 location.hostname, path 为'/', 时间为一个月

设置地区, 地区编码见https://yuque.antfin-inc.com/ioc-1/specification/region

import locale from '@aligov/global-locale';
locale.setRegion('CN'); 

setLang

设置语言, 语言编码见https://yuque.antfin-inc.com/ioc-1/specification/language

import locale from '@aligov/global-locale';
locale.setLang('zh-CN');

setCurrency

设置货币, 货币编码见https://yuque.antfin-inc.com/ioc-1/specification/currency

import locale from '@aligov/global-locale';
locale.setCurrency('CNY');

setTimeZone

设置时区, 时区编码见https://yuque.antfin-inc.com/ioc-1/specification/time-zone

```js
import locale from '@aligov/global-locale';
locale.setTimeZone('CNY');

setLocale

设置自定义的 locale 信息

import locale from '@aligov/global-locale';
locale.setLocale('site', 'rus');

const localeInfo = locale.getLocale();
console.log(localeInfo.site); // rus

提供标准的cookie读写函数

import { Cookie } from '@aligov/global-locale';

/**
 * 获取 cookie 的值
 * @param {string} key , the key of cookie , if not set, will return all the cookie
 */
Cookie.get('x-hng')  ==> //region=CN&lang=zh-CN&currency=CNY

/**
 * 设置 cookie 的值
 * @param {string} key
 * @param {string} value
 * @param {object} attributes 
 */
Cookie.set('x-hng','region=CN&lang=zh-CN&currency=CNY', {
  maxAge: 24*30*60*60, //最好设置 cookie 的过期时间, 不然 cookie 的过期时间就是 session 时间, 也就是关闭窗口的时间
  path: '/', // domain 为当前页面 domain
}) 

/**
 * 移除 cookie 
 * @param {string} key
 */ 
Cookie.remove('x-hng')

自定义 Locale 信息,

我们提供用户自定义的方式来处理 locale 信息.

const myLocale = locale.create({
  key: 'my-name', // lolale 存储的 cookie 的 key 或者 在 windvane / weex 等环境下 locale 存放的位置, 默认为 x-hng
  domain: '.lazada.vn', // cookie domain, 默认为  location.hostname 
  path: '/' , // cookie path, 默认为'/', 
  maxAge: 30*24*60*60 // cookie 过期时间, 秒为单位, 默认时长为 180 天.
})