scriptsharp icon indicating copy to clipboard operation
scriptsharp copied to clipboard

Globalization with jQuery.Globalize

Open andekande opened this issue 11 years ago • 1 comments

Since the Issues #331 and #257 remain unresolved, I'd like to bring up the topic again.

MOTIVATION:

The advantage of the jQuery solution:

  • You can define simple Messages that get translated according to User's culture (errors, popups, ...) - if the translation is missing a fallback to default culture is done.
  • You can actually parse numbers and dates a User inputs by considering his culture.
  • You can format strings to look like a date or number considering the User's culture.

_Only the latter is supported by Script# right now!_

jQuery Globalize comes with following useful methods: Globalize.localize Globalize.format Globalize.parseInt Globalize.parseFloat Globalize.parseDate

In order to stimulate discussion, here is what I think how jQuery.Globalize would make sense to .Net Devs:

PARSING:

What I'd like to be able to do is calling the Parse methods of the accompanying type, which means to call Globalize.parse... under the hood:

  • Int32.Parse("value", [CultureInfo]);
  • Double.Parse
  • DateTime.Parse
  • DateTime.ParseExact

The optional [CultureInfo] parameter would require jQuery.Globalize, if it is omitted and the .js isn't loaded it would fall back to native parsing. If the .js is loaded the jQuery.Globalize default Culture is considered (done so automatically)

If parsing was unsuccesful (NaN returned) throw an exception Accompany each variant by a Try method, which would return false upon parse failure (NaN) Double.TryParse("value", [CultureInfo]);

FORMATTING:

Since Script# already takes the .Net way it should be the default for: String.Format([CultureInfo], "{0:0.0}", "value"); With jQuery.Globalize in place one could force theirs output and formatting parameters by calling Globalize.Format explicitely.

CultureInfo File:

jQuery.Globalize needs CultureInfo in this format: https://github.com/jquery/globalize/blob/master/lib/cultures/globalize.culture.de-DE.js

Script# needs CultureInfo in this format: https://github.com/nikhilk/scriptsharp/blob/cc/src/Core/Scripts/Runtime/Culture.js

For easier maintainability, Script# should adapt the structure and refactor the built in formatting accordingly.

andekande avatar May 21 '13 11:05 andekande

In your list of jQuery advantages, in addition to locale-sensitive formatting/globalization, script# also does support localization (your first bullet point). The localization is based on standard .net resx files. This is done by the compiler and not by some framework.

If you have a Foo.resx file with strings A and B, and add a Foo.fr.resx with say the French version of A, two scripts will be generated - a culture neutral one and a French one. The French one will have resources merged, i.e. French A and culture-neutral B.

I agree it might be useful to have parsing also.

However, I don't think the script# compiler, or mscorlib can bind to/take dependency on jQuery.Globalize. Those are meant to be independent from any particular framework, and also usable in any scripting environment. It would be interesting to see what is the minimal footprint implementation to include that contains the generally useful (i.e. covers most common scenarios) into ss.js to complete the picture.

nikhilk avatar May 22 '13 21:05 nikhilk