Internationalization and localization tools


Locale-Sensitive JavaScript Method

string.charCodeAt(index)

Internationalization (I18n) Method Overview

This method returns the Unicode value of the character at the 0-based index of the string, or NaN - which you check for by calling isNaN(value) - if index is out of range.

For example:

var str = "Hello";
var result = str.charCodeAt(1);


Method returns:

result: 101 (for "e")

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

I18n Issues

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

  • This is a string that may have non-English characters; therefore any fixed indexing into the string may result in an unexpected character code.
  • This is a string that may have characters outside the Basic Multilingual Plane (BMP), and so requires 2 characters (called the High Surrogate character and the Low Surrogate character) to form the complete character code.

Suggested Replacement

Use the charCodeAt method when the string will contain only ASCII (0-127 code points) characters; i.e. a programmatic string. If you are searching for a specific locale-independent character in a translated string, call indexOf which returns the index of the character. If you must call charCodeAt on a non-English string (for example, you are looking for a specific translated character in a translated string), then make sure that you handle the fact that a translated string will have different characters in different locations and that you may need to support characters outside the Basic Multilingual Plane.


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