Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to detect East Asian language support?

I'm working on a javascript memorization game that requires that japanese characters to be displayed. Is there a cross browser way(s) of detecting support for asian languages, or japanese specifically?

window.navigator.language will be set to the language of the browser but misses out people who don't set japanese as their browsers language (?).

like image 460
AnnanFay Avatar asked Mar 03 '09 03:03

AnnanFay


2 Answers

Is there a cross browser way(s) of detecting support for asian languages, or japanese specifically?

Do you just mean “is there a Japanese font installed”? Because the only other aspect of “support for Asian languages” that modern OSs can have configured is to do with whether character encodings like Shift-JIS are known (aka code page 932 on Windows). But you don't generally need that anyway since you can just use UTF-8 which is much cleaner and supported everywhere.

In general you can't directly tell whether a font for a particular character range is installed or not, but there are roundabout ways. Like for example measuring the rendered size of on-page text:

var half= document.createElement('span');
var full= document.createElement('span');
half.appendChild(document.createTextNode('\uFF71'));
full.appendChild(document.createTextNode('\u30A2'));
document.body.insertBefore(half, document.body.firstChild);
document.body.insertBefore(full, document.body.firstChild);
var havejapanesefont= half.offsetWidth!=full.offsetWidth;
document.body.removeChild(half);
document.body.removeChild(full);

This temporarily creates elements containing half-width and full-width variants of the katakana ‘a’. If a Japanese font is available we would expect them to be different widths. If no such font exists, they will most likely be rendered as similar ‘unrenderable character’ glyphs with the same width.

like image 119
bobince Avatar answered Nov 10 '22 11:11

bobince


Your server might be able to check the Accept-Charset header, if it's one that doesn't include japanese characters, redirect to a warning page. Unfortunately, as this thread indicates, the headers are not available to javascript.

like image 24
sblundy Avatar answered Nov 10 '22 10:11

sblundy