Internationalization and localization tools


Locale-Sensitive JavaScript Method

str.indexOf(searchString);
str.indexOf(searchString, startIndex);

Internationalization (I18n) Method Overview

This method searches for the first occurrence of searchString in the string, starting at the beginning of the string or at startIndex if it passed in, returning the 0-based index of the found string, or -1 if the string isn't found.

The following example searches for the first space in the string:

var str = "Page 1 of 10";
var result1 = str.indexOf(" ");
var result2 = str.indexOf(" ", 5); // start search at the "1"


Method returns:

result1: 4
result2: 6


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

I18n Issues

Whether or not calling indexOf is an i18n issue is dependent on how it is being used in the application. If the string that is to be searched is to be displayed to the user, then this may be a problem because:

  • The string being searched may need to be translated.
  • The searchSearch parameter may need to be translated.
  • Searching for spaces as word breaks is not valid for all locales.

Suggested Replacement

Use the indexOf method when the strings are programmatic strings that do not require translation.

If the string is to be displayed to the user, then you may need to first retrieve the string from a resource file based on locale, and then do the same thing with the searchString; retrieving it from the locale-sensitive resource file.

Searching for characters that may not exist in translated strings (such as spaces) will need to be refactored.


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