Internationalization and localization tools


Locale-Sensitive JavaScript Method

date.toLocaleTimeString();
date.toLocaleTimeString(locales);
date.toLocaleTimeString(locales, options);

Internationalization (I18n) Method Overview

The toLocaleTimeString method returns the time part of the date object in either the system's locale or the specified locale passed into the method. The options parameter can be used to set the time zone.

For example:

var date = new Date();
var options = {timeZone: "America/New_York", timeZoneName: "long"};
var result1 = date.toLocaleTimeString("en-US", options);
var result2 = date.toLocaleTimeString("de-DE", options);

Results are:

result1: 7:55:31 PM Eastern Daylight Time
result2: 19:55:31 Nordamerikanische Ostküsten-Sommerzeit

Click here (w3schools) and here (MDN) for additional details.

I18n Issues

Whether or not calling toLocaleTimeString is an i18n issue is dependent on how it is being used in the application. Some possible issues are:

  • There is no locales parameter passed into the method, which means the system's locale will be used to format the time.
  • There is no timeZone option passed into the method, which means the system's local time zone will be used to format the time.
  • You want a fixed time format, regardless the locale.
  • A large set of dates are being formatted to time strings and you want to improve the performance.

Suggested Replacement

Make sure that you pass in the application's locale so that the time will be formatted correctly. In addition, ensure that the timeZone option is set to the correct time zone and that the timeZoneName option is included if you want to display the time zone.

If you want a fixed date format, regardless the locale, you could call toTimeString, which will format the time using U.S. English. Call toISOString to format the date in a locale-independent way; the resulting ISO Standard date/time string is understandable in all locales.

To improve performance, you may want to call Intl.DateTimeFormat constructor, which returns a locale-sensitive format object that you can then repeatedly call its format method.


Globalyzer will detect this method and report it as an i18n issue. If you have determined that the call is being handled correctly, you can use Globalyzer's Ignore Comment functionality to ensure that it isn't picked up in a subsequent scan.



Locale-Sensitive JavaScript Methods

 

Lingoport internationalization and localization services and software