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

@awesome-cordova-library/globalization

v1.0.2

Published

This plugin obtains information and performs operations specific to the user's locale, language, and timezone. Note the difference between locale and language: locale controls how numbers, dates, and times are displayed for a region, while language determ

Downloads

10

Readme


id: plugin-globalization title: Globalization tags:

  • cordova
  • capacitor
  • ionic
  • javascript
  • typescript
  • plugin
  • mobile
  • globalization

Globalization

This plugin obtains information and performs operations specific to the user's locale, language, and timezone. Note the difference between locale and language: locale controls how numbers, dates, and times are displayed for a region, while language determines what language text appears as, independently of locale settings.

Online documentation

Github documentation

Installation

Cordova

cordova plugin add cordova-plugin-globalization
npm install @awesome-cordova-library/globalization

Capacitor / Ionic

npm install cordova-plugin-globalization
npm install @awesome-cordova-library/globalization
npx cap sync

Vanilla

Declaration

class Globalization {
  static getPreferredLanguage(
    successCallback: (language: { value: string }) => void,
    errorCallback: () => void
  ): void;
  static getPreferredLanguageWeb(): string;
  static getLocaleName(
    successCallback: (language: { value: string }) => void,
    errorCallback: () => void
  ): void;
  static dateToString(
    date: Date,
    successCallback: (date: { value: string }) => void,
    errorCallback: () => void
  ): void;
  static getCurrencyPattern(
    currencyCode: string,
    successCallback: (pattern: {
      pattern: string;
      code: string;
      fraction: number;
      rounding: number;
      decimal: string;
      grouping: string;
    }) => void,
    errorCallback: () => void
  ): void;
  static getDateNames(
    successCallback: (
      names: {
        value: string;
      }[]
    ) => void,
    errorCallback: () => void,
    options?: {
      type: "narrow" | "wide";
      item: "months" | "days";
    }
  ): void;
  static getDatePattern(
    successCallback: (date: {
      pattern: string;
      timeaone: string;
      iana_timezone: string;
      utc_offset: string;
      dst_offset: string;
    }) => void,
    errorCallback: () => void,
    options?: {
      formatLength: "short" | "medium" | "long" | "full";
      selector: "date" | "time";
    }
  ): void;
  static getFirstDayOfWeek(
    successCallback: (day: { value: number }) => void,
    errorCallback: () => void
  ): void;
  static getNumberPattern(
    successCallback: (pattern: {
      pattern: string;
      symbol: string;
      fraction: number;
      rounding: number;
      positive: string;
      negative: string;
      decimal: string;
      grouping: string;
    }) => void,
    errorCallback: () => void,
    options?: {
      type: "decimal" | "percent" | "currency";
    }
  ): void;
  static isDayLightSavingsTime(
    date: Date,
    successCallback: (date: { dst: boolean }) => void,
    errorCallback: () => void
  ): void;
  static numberToString(
    n: number,
    successCallback: (number: { value: string }) => void,
    errorCallback: () => void,
    options?: {
      type: "decimal" | "percent" | "currency";
    }
  ): void;
  static stringToDate(
    dateString: string,
    successCallback: (date: {
      year: number;
      month: number;
      day: number;
      hour: number;
      minute: number;
      second: number;
      millisecond: number;
    }) => void,
    errorCallback: () => void,
    options?: {
      formatLength: "short" | "medium" | "long" | "full";
      selector: "date" | "time" | "date and time";
    }
  ): void;
  static stringToNumber(
    s: string,
    successCallback: (n: { value: number }) => void,
    errorCallback: () => void,
    options?: {
      type: "decimal" | "percent" | "currency";
    }
  ): void;
}

Usages

import Globalization from "@awesome-cordova-library/globalization";

Globalization.getPreferredLanguage(
  function (language) {
    alert("language: " + language.value + "\n");
  },
  function () {
    alert("Error getting language\n");
  }
);

Globalization.getPreferredLanguageWeb();

Globalization.getLocaleName(
  function (locale) {
    alert("locale: " + locale.value + "\n");
  },
  function () {
    alert("Error getting locale\n");
  }
);

Globalization.dateToString(
  new Date(),
  function (date) {
    alert("date: " + date.value + "\n");
  },
  function () {
    alert("Error getting dateString\n");
  },
  { formatLength: "short", selector: "date and time" }
);

Globalization.getCurrencyPattern(
  "USD",
  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");
  }
);

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" }
);

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

Globalization.getFirstDayOfWeek(
  function (day) {
    alert("day: " + day.value + "\n");
  },
  function () {
    alert("Error getting day\n");
  }
);

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" }
);

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

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

Globalization.stringToDate(
  "9/25/2012",
  function (date) {
    alert(
      "month:" + date.month + " day:" + date.day + " year:" + date.year + "\n"
    );
  },
  function () {
    alert("Error getting date\n");
  },
  { selector: "date" }
);

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

React

Declaration

const useGlobalization: () => {
  getPreferredLanguage: () => Promise<{
    value: string;
  }>;
  getPreferredLanguageWeb: () => string;
  getLocaleName: () => Promise<{
    value: string;
  }>;
  dateToString: (date: Date) => Promise<{
    value: string;
  }>;
  getCurrencyPattern: (currencyCode: string) => Promise<{
    pattern: string;
    code: string;
    fraction: number;
    rounding: number;
    decimal: string;
    grouping: string;
  }>;
  getDateNames: (options?: {
    type: "narrow" | "wide";
    item: "months" | "days";
  }) => Promise<
    {
      value: string;
    }[]
  >;
  getDatePattern: (options?: {
    formatLength: "short" | "medium" | "long" | "full";
    selector: "date" | "time";
  }) => Promise<{
    pattern: string;
    timeaone: string;
    iana_timezone: string;
    utc_offset: string;
    dst_offset: string;
  }>;
  getFirstDayOfWeek: () => Promise<{
    value: number;
  }>;
  getNumberPattern: (options?: {
    type: "decimal" | "percent" | "currency";
  }) => Promise<{
    pattern: string;
    symbol: string;
    fraction: number;
    rounding: number;
    positive: string;
    negative: string;
    decimal: string;
    grouping: string;
  }>;
  isDayLightSavingsTime: (date: Date) => Promise<{
    dst: boolean;
  }>;
  numberToString: (
    n: number,
    options?: {
      type: "decimal" | "percent" | "currency";
    }
  ) => Promise<{
    value: string;
  }>;
  stringToDate: (
    dateString: string,
    options?: {
      formatLength: "short" | "medium" | "long" | "full";
      selector: "date" | "time" | "date and time";
    }
  ) => Promise<{
    year: number;
    month: number;
    day: number;
    hour: number;
    minute: number;
    second: number;
    millisecond: number;
  }>;
  stringToNumber: (
    s: string,
    options?: {
      type: "decimal" | "percent" | "currency";
    }
  ) => Promise<{
    value: number;
  }>;
};

Usages

import { useEffect } from 'react';
import useGlobalization from '@awesome-cordova-library/globalization/lib/react';

function App() {
  const { getPreferredLanguage, ..... } = useGlobalization();

  useEffect(() => {
    getPreferredLanguage().then((language) => {
      console.log(`Preferred language ${language.value}`);
    })

  }, []);

  return <div />;
}