7

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

  • I will provide you a better answer when I find time. For now, I just wanted to make sure you have seen https://github.com/jquery/globalize/tree/master/examples – Rafael Xavier Jun 30 '15 at 15:44
  • i have download and run the sample but it doesn't run plain javascript sample and also in that sample static data can be loaded but **i want to load the dynamic culture JSON data** , i want to know how to load the culture data dynamically..?? – GopalaKrishnan subramanian Jul 01 '15 at 04:19
  • Here https://github.com/jquery/globalize/blob/master/doc/cldr.md#how-do-i-load-cldr-data-into-globalize, it's presented various ways of loading CLDR data (including dynamic ways). – Rafael Xavier Jul 01 '15 at 11:52
  • Hi Rafael,thanks for your update.. the above mentioned sample works only when i host the sample in IIS otherwise not working... please help to me resolve this issue. – GopalaKrishnan subramanian Aug 06 '15 at 12:43
  • i want to use this dynamic loading in my localization widgets for example datepicker like that .. please help me to achieve this. – GopalaKrishnan subramanian Aug 06 '15 at 12:44
  • Can you demonstrate what you currently have in a http://gist.github.com/ or http://jsfiddle.net/? By the way, jQuery UI has a work-in-progress branch to update for Globalize 1.x https://github.com/jquery/jquery-ui/tree/datepicker-globalize-1.x – Rafael Xavier Aug 06 '15 at 17:54

1 Answers1

3

Fast and recommended way to get started:

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.

Community
  • 1
  • 1
Rafael Xavier
  • 2,840
  • 27
  • 34
  • 1
    If #1 is really as easy as it looks, you should consider adding/replacing the "Getting Started" section of your docs with it. I've spent a day trying to understand what the docs are saying and ended up reverting to 0.x version of globalize because its been too much of a time sink. – StingyJack Oct 28 '15 at 20:59
  • That's indeed a great idea. Would you like to submit a PR with such changes? Thanks – Rafael Xavier Oct 29 '15 at 06:22
  • I'll dig around and see what that means (outside of this package, I dont use github and dont have an account). – StingyJack Oct 29 '15 at 12:45
  • Hi @StingyJack, I've made some changes to the Getting Started section and included a quick getting-started-example there. Just let me know your thoughts. Thanks https://github.com/jquery/globalize#getting-started – Rafael Xavier Dec 03 '15 at 12:45