0

Is there some way to simulate a different locale in JavaScript? Preferably without altering code (like a tool, perhaps part of a debugging tool letting me alter it). I am especially thinking of conversions between strings and floats.

If I want to test a particular language, is it enough to install a browser using it? Or does it also depend on OS, or are browsers not consistent.

Olav
  • 1,758
  • 4
  • 27
  • 49

1 Answers1

0

Current versions of the major browsers integrate the ECMAScript Internationalization API within their JavaScript implementations.

This API allows you to convert dates, times and numbers into strings and offers some functions for language sensitive string comparision. It is available through the Intl object.

E.g. it allows to format the float number 10.2 as 10,20 € using the following code:

new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(10.2)

Though the first version of this API is limited to output formatting. It doesn't provide functions for normalization of language specific input.

For locale specific numerical input you can use the <input> tag like this:

<input type="number" step="0.01" lang="de">

Where the value of the lang attribute defines the language and how the value within the input is displayed and interpreted. Though note that this is not supported by all major browsers. Mainly Internet Explorer up to version 11 doesn't handle the input as expected.

Sebastian Zartner
  • 18,808
  • 10
  • 90
  • 132