Agent Wranglers

LOC-56Recommended

Cover every script with fonts and break words with a segmenter

For each supported language, load or fall back to a font with all its characters, and set lang on the page. Find word boundaries with a segmenter, never by splitting on spaces.

How to do it

Thai, Lao, Khmer, Chinese and Japanese put no spaces between words. Code that splits on spaces sees one long word, so word counts, excerpts and highlights break in the middle of words. Use Intl.Segmenter with granularity: 'word' in JavaScript, or ICU's BreakIterator in Java and Python. Browsers break lines in these scripts only when lang names the language. The same Han character also takes a different shape in Japanese and Chinese, and lang picks the right one. End each font stack with a family that covers the script, such as a Noto font. Cut text by visible character as Count and cut text by visible characters says.

Examples

JavaScript example

const words = [...new Intl.Segmenter('th', { granularity: 'word' }).segment('สวัสดีครับ')]
  .filter(part => part.isWordLike).map(part => part.segment);

Java example

import java.text.BreakIterator;
import java.util.Locale;

BreakIterator words = BreakIterator.getWordInstance(Locale.forLanguageTag("th"));
words.setText("สวัสดีครับ");

How to check

A unit test segments "สวัสดีครับ" with the Thai locale and gets more than one word. A browser test renders a sample sentence in every supported language at phone width, and a screenshot shows no missing-glyph boxes.

Sources

For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/localization/layout-and-scripts/scripts-fonts-and-line-breaks">Agent Wranglers localization playbook</a>. In code you write, add a comment next to it with the same link.