Internationalization and localization tools


Locale-Sensitive JavaScript Method

str.startsWith(searchString); str.startsWith(searchString, index);

Internationalization (I18n) Method Overview

This method determines if the string begins with searchString, returning true if found, else false if searchString isn't found at the beginning of the string. To change the starting point of the search, use the 0-based index parameter.



For example:

var str = "Hello Goodbye";
var result1 = str.startsWith("Hello");
var result2 = str.startsWith("Goodbye", 6);


Method returns:

result1: true
result2: true


Click here (MDN) for additional details.

Note: This method is new, part of the ECMAScript 6 proposal, and so may not be supported by all browsers. Until approved, it is also subject to change.

I18n Issues

Whether or not calling startsWith 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 searchString parameter may need to be translated.
  • In addition to the string possibly needing translation, the order of the words may be different and may "start with" a different string.
  • The index parameter assumes characters are 16-bit values; it does not take into account characters outside the Basic Multilingual Plane (BMP) which require two 16-bit values.

Suggested Replacement

Use the startsWith 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 searchString; retrieving it from the locale-sensitive resource file.

If you are using the index parameter, then you may need to use a 3rd party library (such as punycode.js) if the string that is being accessed could have characters greater than 16-bits (i.e. outside the Basic Multilingual Plane of Unicode characters).


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