Internationalization and localization tools


Locale-Sensitive JavaScript Method

str.localeCompare(compareString);
str.localeCompare(compareString, locales);
str.localeCompare(compareString, locales, options);

Internationalization (I18n) Method Overview

This method compares two strings, using the default locale when there are no locales passed in, else using the locales and optional options, returning 0 if the strings are equal, -1 if the base string sorts before compareString or 1 if the base string sorts after compareString.

For example:

var str = "Turkish ı";
var result = str.localeCompare("Turkish i");


Method returns:

result: -1 // The dotless ı sorts before the dotted i

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

Note: Not all browsers support the locales and options parameters.

I18n Issues

Whether or not calling localeCompare is an i18n issue is dependent on how it is being used in the application. Internationalization issues may occur because:

  • The base string may need to be translated.
  • The compareString may need to be translated.
  • localeCompare is based on default locale rather than the application locale (i.e. is missing the locales parameter).

Suggested Replacement

If strings are user-facing, they may need be translated by retrieving them from a locale-sensitive resource file.

Be sure to pass in the locales parameter to ensure sorting is based on the application locale. Include options as needed to fine-tune the collation.

To improve performance, you may want to construct the locale-sensitive Intl.Collator and then repeatedly call its compare 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