Internationalization and localization tools


Locale-Sensitive JavaScript Method

arr.sort();
arr.sort(compareFunction);

Internationalization (I18n) Method Overview

This method sorts an array of objects in ascending order, converting the objects to strings if compareFunction is not passed and sorting based on Unicode code point values. If compareFunction is passed, then sorting is based on the function's return values.

For example:

var arr = ["One", "Two", "Three"];
var result = arr.sort();


Method returns:

result: "One", "Three", "Two"

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

I18n Issues

Whether or not calling sort is an i18n issue is dependent on how it is being used in the application. If the array of strings contain non-English characters, then this may be a problem because:

  • The array of strings may need to be translated.
  • sort is not locale aware and so the order may be incorrect.
  • If characters are outside the Basic Multilingual Plane (BMP), they require two 16-bit values, which may not result in correct ordering.

Suggested Replacement

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

For strings that may contain non-English characters, call localeCompare.


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