Locale-Sensitive JavaScript Method
str.lastIndexOf(searchString);
str.lastIndexOf(searchString, startIndex);
Internationalization (I18n) Method Overview
This method searches for the last 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 last occurrence of a space in the string:
var str = "Page 5 of 10";
var result = str.lastIndexOf(" ");
Method returns:
result: 9
Click here (w3schools) and
here (MDN) for additional details.
I18n Issues
Whether or not calling lastIndexOf 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 lastIndexOf 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
|