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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@cordova-ohos/cordova-plugin-globalization

v1.11.0

Published

Cordova globalization Plugin

Downloads

120

Readme

cordova-plugin-globalization 全球化插件

一款 Cordova 插件,提供对设备全球化功能的访问权限,帮助开发者根据设备的区域设置获取地区、语言、日期、时间及数字格式化信息。

重要提示: 现代浏览器和 Web 环境已经原生支持国际化功能,可以直接使用现代浏览器提供的国际化功能,如果您是老的Android项目移植到OHOS,保持接口统一,仍可以使用该插件,如果您是新项目,不建议使用该插件。

功能特性

  • 获取设备当前的区域和语言设置

  • 根据区域规则格式化日期、时间、数字和货币

  • 从区域特定字符串解析日期和数字

  • 获取设备的时区信息

  • 支持通用和区域特定的区域标识符

安装方法

通过 Cordova CLI 或 PhoneGap CLI 安装插件。安装前请确保已创建 Cordova 项目。

Cordova 命令行安装

# 安装hcordova
npm install -g hcordova


# 基础安装(稳定版)
hcordova plugin add cordova-plugin-globalization

# 指定平台安装
hcordova plugin add cordova-plugin-globalization --platform ohos

# 从 GitCode 安装(开发版)
hcordova plugin add https://gitcode.com/OpenHarmony-Cordova/cordova-plugin-globalization.git --platform ohos

卸载方法

# 全平台卸载
hcordova plugin remove cordova-plugin-globalization

#指定OHOS卸载
hcordova plugin remove cordova-plugin-globalization --platform ohos

核心 API

所有 API 方法均为异步执行,使用成功回调和错误回调处理结果。插件通过 navigator.globalization 对象暴露接口。

1. 获取区域信息

获取设备当前的区域和语言设置信息。

//两个API相同的功能
navigator.globalization.getLocaleName(
    function (local) {
        console.log(local.value);
    },
    function () {
      alert('Error getting language\n');
    }
);

navigator.globalization.getPreferredLanguage(
    function (language) {
        console.log(language.value);
    },
    function () {
      alert('Error getting language\n');
    }
);

成功回调参数

  • value: 字符串 - 区域标识符(例如:"en_US"、"zh_CN"、"ja_JP")

错误回调参数

  • code: 数字 - 错误代码(参见 错误代码

  • message: 字符串 - 人类可读的错误信息

2. 格式化日期

将 Date 对象格式化为区域特定的字符串。

navigator.globalization.dateToString(
    new Date(),
    function (date) { 
      console.log(date.value); 
    },
    function () {
       alert('Error getting dateString\n'); 
    },
    { formatLength: 'short', selector: 'date and time' }
);

参数说明

  • date: Date 类型 - 待格式化的日期

  • options: 对象(可选) formatLength: 字符串 - "short"(短)、"medium"(中)、"long"(长)、"full"(完整)(默认:"medium")

  • selector: 字符串 - "date"(日期)、"time"(时间)、"date and time"(日期和时间)(默认:"date and time")

  • locale: 字符串 - 要使用的特定区域(覆盖设备区域)

成功回调参数

  • value: 字符串 - 格式化后的日期字符串(例如:en_US 区域为 "12/4/2025",zh_CN 区域为 "2025-12-04")

3. 解析日期

将区域特定的日期字符串解析为 Date 对象。


 navigator.globalization.stringToDate(
    '2012/7/7',
    function (date) {
      alert('month:' + date.month +
        ' day:'  + date.day   +
        ' year:' + date.year  + '\n');
    },
    function () {alert('Error getting date\n');},
    {selector: 'date'}
);

4. 获取日期模式


navigator.globalization.getDatePattern(
    function (date) { 
      alert('pattern: ' + date.pattern + '\n'); 
    },
    function () { alert('Error getting pattern\n'); },
    { formatLength: 'short', selector: 'date and time' }
);

4. 获取月名称


navigator.globalization.getDateNames(
    function (names) {
        for (var i = 0; i < names.value.length; i++) {
            alert('month: ' + names.value[i] + '\n');
        }
    },
    function () { alert('Error getting names\n'); },
    { type: 'wide', item: 'months' }
);

5. 获取是否夏令时


navigator.globalization.isDayLightSavingsTime(
    new Date(),
    function (date) {alert('dst: ' + date.dst + '\n');},
    function () {alert('Error getting names\n');}
);

6. 获取每周的第一天


navigator.globalization.getFirstDayOfWeek(
    function (day) {alert('day: ' + day.value + '\n');},
    function () {alert('Error getting day\n');}
);

7. 格式化数字

将数字格式化为区域特定的字符串。

navigator.globalization.numberToString(
    3.1415926,
    function (number) {alert('number: ' + number.value + '\n');},
    function () {alert('Error getting number\n');},
    {type:'decimal'}
);

8. 获取数字模式

navigator.globalization.getNumberPattern(
    function (pattern) {alert('pattern: '  + pattern.pattern  + '\n' +
        'symbol: '   + pattern.symbol   + '\n' +
        'fraction: ' + pattern.fraction + '\n' +
        'rounding: ' + pattern.rounding + '\n' +
        'positive: ' + pattern.positive + '\n' +
        'negative: ' + pattern.negative + '\n' +
        'decimal: '  + pattern.decimal  + '\n' +
        'grouping: ' + pattern.grouping);},
    function () {alert('Error getting pattern\n');},
    {type:'decimal'}
);

9. 字符串转数字

将字符串格式化为区域特定的数字。

navigator.globalization.stringToNumber(
    '1234.56',
    function (number) {alert('number: ' + number.value + '\n');},
    function () {alert('Error getting number\n');},
    {type:'decimal'}
);

10. 获取货币模式

 navigator.globalization.getCurrencyPattern(
    'CNY',
    function (pattern) {
        alert('pattern: '  + pattern.pattern  + '\n' +
            'code: '     + pattern.code     + '\n' +
            'fraction: ' + pattern.fraction + '\n' +
            'rounding: ' + pattern.rounding + '\n' +
            'decimal: '  + pattern.decimal  + '\n' +
            'grouping: ' + pattern.grouping);
    },
    function () { alert('Error getting pattern\n'); }
);

许可证

本插件基于 Apache 许可证 2.0 版 发布。

联系方式

  1. OHOShttps://gitcode.com/OpenHarmony-Cordova/cordova-plugin-globalization

  2. Android、ios插件说明https://www.npmjs.com/package/cordova-plugin-globalization