Internationalization and localization tools


Locale-Sensitive JavaScript Method

string.slice(beginIndex);
string.slice(beginIndex, endIndex);
array.slice(beginIndex);
array.slice(beginIndex, endIndex);

Internationalization (I18n) Method Overview

This method extracts from either a string or an array to create a new string or array. The 0-based beginIndex specifies the starting string character or array element; endIndex is a non-inclusive value that specifies one past the last string character or array element to include in the extraction; when endIndex is not specified, the rest of the string or array elements are included.

For example:

var str = "One Two Three";
var arr = ["One", "Two", "Three"];
var strResult1 = str.slice(4, 7);
var strResult2 = str.slice(4);
var arrResult1 = arr.slice(1, 2);
var arrResult2 = arr.slice(1);


Method returns:

strResult1: "Two"
strResult2: "Two Three"
arrResult1: ["Two"]
arrResult2: ["Two", "Three"]


For more information on String slice, click here (w3schools) and here (MDN).

For more information on Array slice, click here (w3schools) and here (MDN).

I18n Issues

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

  • The strings may need to be translated.
  • The beginIndex and endIndex values may need to vary based on locale.
  • In the case of string slicing, this method does not support characters outside the Basic Multilingual Plane (BMP) which require two 16-bit values to define a single character.

Suggested Replacement

If the string or array of strings are user-facing, they will need be translated by retrieving them from a locale-sensitive resource file.

Indexing into strings or arrays that are locale-sensitive may require different values.

In the case of calling slice on a string that may have characters outside the BMP (i.e. two 16-bit values required for one character), you may need to use a 3rd party JavaScript library to correctly index into the string (for example, the punycode.js library, which is bundled with Node.js).


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