npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@aligov/global-locale

v1.0.5

Published

locale information manager

Downloads

27

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 天.
})