Improve Chinese text on Firefox

Change lang="zh" attribute to lang="zh-cn" so Firefox can style it correctly

Autor
bellisk
Installationen heute
0
Installationen gesamt
7
Bewertungen
0 0 0
Version
2024-10-26-04
Erstellt am
26.10.2024
Letzte Aktualisierung
26.10.2024
Größe
987 Bytes
Lizenz
MIT
Wird angewandt auf
Alle Seiten

Enable Firefox to use the correct font-family for text with lang="zh".

Firefox allows configuring the font-family to use for text in different languages. Unfortunately, for Chinese text, this only applies to HTML elements with lang="zh-cn", lang="zh-hk" or lang="zh-tw". Text whose language has been marked with lang="zh" takes a default, serif font that cannot be changed.

This script changes lang="zh" to lang="zh-cn" for all elements on a page, to allow Firefox to recognise that the configured font for Simplified Chinese should be used.

If you have the same font families set for Simplified Chinese, Traditional Chinese (Hong Kong) and Traditional Chinese (Taiwan), which is the default configuration, then it doesn't matter which one of these is used. Setting the lang attribute to zh-cn will correctly style all Chinese text. If you want to use different font families for each writing system, then you might want to play around a bit with the code in this script.

Demo

Without this script: Chinese text on Wikipedia is displayed in serif font, despite the page font-family being sans-serif.

Infobox from Wikipedia, mixed Chinese (serif font) and English (sans-serif font) text

With this script: Chinese text is displayed in sans-serif font, matching the page's font-family.

Infobox from Wikipedia, mixed Chinese and English text, all in sans-serif font