Internationalization and localization tools


Locale-Sensitive JavaScript NodeJS Method

Buffer.from(str[, encoding]);
Buffer.from(arrayBuffer[, byteOffset[, length]]);

// deprecated
new Buffer(size);
new Buffer(str[, encoding]);
new Buffer(arrayBuffer[, byteOffset[, length]]);

Internationalization (I18n) Method Overview

The Buffer.from and deprecated new Buffer methods/constructors allocate new buffers while copying from provided content. A custom byte offset and length may be specified if copying from an arrayBuffer.


For example:

const buf1 = Buffer.from('this is a tést');
console.log(buf1.toString());
// prints: this is a tést
console.log(buf1.toString('ascii'));
// prints: this is a tC)st

const buf2 = Buffer.from('7468697320697320612074c3a97374', 'hex');
console.log(buf2.toString());
// prints: this is a tést


or:

const ab = new ArrayBuffer(10);
const buf = Buffer.from(ab, 0, 2);
console.log(buf.length);
// Prints: 2

The following are links for the full documentation for each method:

I18n Issues

Whether or not calling Buffer.from, or the deprecated new Buffer constructor, is an i18n issue is dependent on how it is being used in the application. If the buffer that is copied contains a string, then be aware of the following possible issues:

  • The byteOffset, length or size may assume constant character byte lengths. Some unicode characters require more bytes to encode than others.
  • No encoding, or an incorrect encoding, may be passed to Buffer.from(str[, encoding])

Suggested Replacement

If using a byteOffset, length or size on a buffer which holds a string, do not assume that the buffer size will be directly proportional to the number of string characters. You may instead need to perform some processing on the string to determine the exact byte length of the sections that you wish to exclude and/or copy.

Ensure that the correct encoding is passed to Buffer.from(str[, encoding])

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