Internationalization and localization tools


Locale-Sensitive JavaScript NodeJS Method

buf.includes(value[, byteOffset][, encoding]);

Internationalization (I18n) Method Overview

The buf.includes method checks to see if the buffer contains a given string/buffer/number.


For Example:

const buf = Buffer.from('this is a buffer', 'utf8');

buf.includes('this', 'utf8');
  // returns true
buf.includes('this', 'utf16);
  // returns false
buf.includes('is', 'ascii'); // ascii values are the same in utf8
  // returns true
buf.includes(97); // ascii for 'a'
  // returns true
buf.includes(Buffer.from('a buffer', 'utf8'));
  // returns true
buf.includes(Buffer.from('a buffer', 'utf16'));
  // returns false
buf.includes(Buffer.from('a buffer example', 'utf8'));
  // returns false

buf.includes('is', 6, 'utf8');
  // returns false.

const unicodeBuf = Buffer.from('ūñícódè buffer', 'utf8');
unicodeBuf.includes('buffer', 10, 'utf8');
  // 'ūñícódè ' is represented by more than 10 bytes
  // returns true
				

Click here for additional details.

I18n Issues

If the buffer contains a string and a byteOffset, the byteOffset may be based upon the number of string characters passed. However, different unicode characters may require varying amounts of bytes to encode. If some of the characters passed require more bytes to encode, then the size will be incorrect.

If the buffer contains a string, then the encoding must be correct for the context.

Suggested Replacement

If providing a byteOffset for a string buffer, calculate the bytelength of the desired sections relevant to the encoding used. Do not assume the bytelength will be directly proportional to the number of string characters.

If the buffer contains a string, check that an encoding is passed. Confirm that the encoding is correct for the application setup.

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