Monday, April 11, 2011

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 (?).

From stackoverflow
  • 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.

  • 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.

    Michael Borgwardt : Very clever hack!
    Annan : Yep, just the fonts and that's just the kind of trick I was looking for, thanks :)

0 comments:

Post a Comment