Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Globalize 1.0 and get specified culture info

How to use Globalize 1.0 in html web application .

I need to get the below information using Globalize 1.0 support

  1. How to create simple sample with Globalize 1.0 support.

  2. How to get the default currency and percentage symbol using Globalize 1.0 support and how to change the symbol dynamically

  3. How to get the Positive / Negative pattern for Currency/Percentage value of the specified culture and how to change the pattern dynamically

  4. How to get the default date format of the specified culture.

  5. How to get the default group separator and decimal separator for the specified culture

If you have any samples or an code snippet for the problem means then please share it.

if possible share the simple sample with Globalize 1.0

Thank you.....

Gobala

like image 721
GopalaKrishnan subramanian Avatar asked Jun 30 '15 05:06

GopalaKrishnan subramanian


1 Answers

Fast and recommended way to get started:

  • Application example using npm and webpack, or
  • By checking out the other examples

Now, directly to your questions:

  1. How to create simple sample with Globalize 1.0 support.

Assuming you want to play with Globalize locally, I recommend using Node.js:

npm install globalize cldr-data
node

var Globalize = require("globalize");

# Feed Globalize on CLDR data
Globalize.load(require("cldr-data").entireSupplemental());
Globalize.load(require("cldr-data").entireMainFor("en");

Globalize("en").formatNumber(Math.PI);
// > '3.142'

Globalize("en").formatNumber(Math.PI, {maximumFractionDigits: 2});
// > '3.14'

Globalize("en").formatCurrency(69900, "USD");
// > '$69,900.00'

Globalize("en").formatCurrency(69900, "EUR");
// > '€69,900.00'

Globalize("en").formatRelativeTime(-35, "second");
// > '35 seconds ago'

Did I answer to your 1st question here? Just let me know if you meant something else.

  1. How to get the default currency and percentage symbol using Globalize 1.0 support and how to change the symbol dynamically

If you don't know the currency, how do you know if the monetary value is correct and it corresponds to what's being formatted/displayed?

Specifications (UTS#35) explicitly advises against having a currency value per country. "Note: Currency values should never be interchanged without a known currency code. You never want the number 3.5 interpreted as $3.50 by one user and €3.50 by another. Locale data contains localization information for currencies, not a currency value for a country. A currency amount logically consists of a numeric value, plus an accompanying currency code (or equivalent). The currency code may be implicit in a protocol, such as where USD is implicit. But if the raw numeric value is transmitted without any context, then it has no definitive interpretation."

http://www.unicode.org/reports/tr35/tr35-numbers.html#Currencies

Note though, applications can use CLDR to deduce the currency used in a country in a certain period of time and then feed it in for currencyFormatter. See How to access culture data in globalize.js V1.0.0 for how to access CLDR data.

  1. How to get the Positive / Negative pattern for Currency/Percentage value of the specified culture and how to change the pattern dynamically

Can you give an example of the changes you want to make? Does the below example help you out?

Globalize("en").formatNumber(0.5, {style: "percent"});
// > '50%'
Globalize("en").formatNumber(-0.5, {style: "percent"});
// > '-50%'
Globalize("en").formatNumber(-0.5, {style: "percent", minimumFractionDigits: 2, maximumFractionDigits: 2});
// > '-50.00%'
Globalize("en").formatCurrency( -69900, "USD" )
'-$69,900.00'

Note Globalize will handle the appropriate locale defaults for you, for example in Arabic you have:

Globalize("ar").formatNumber(-0.5, {style: "percent"})
// > '‏-٥٠٪'
  1. How to get the default date format of the specified culture.

Please, could you provide a use case? I don't understand what you are trying to accomplish.

The default date format is numeric year, month and day, i.e., the same as Ecma-402 Intl.DateTimeFormat https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

You can override the default the way you want using date format options.

  1. How to get the default group separator and decimal separator for the specified culture

Please, could you provide a use case? I don't understand what you are trying to accomplish.

Anyway, see How to access culture data in globalize.js V1.0.0 for how to access CLDR data directly.

like image 163
Rafael Xavier Avatar answered Oct 16 '22 05:10

Rafael Xavier