tags:

views:

869

answers:

5

I have the following CSS fragment:

INPUT{ font-family: Raavi; font-size: 14px;}

Which works fine when the textbox contains some Punjabi script like this: ਪੰਜਾਬੀ

But the user might enter English instead, and I would rather use the Verdana font with a different size, since the English letters in the Raavi font are real funky and the size is wrong.

So my question could be stated as:

  • Is there any type of conditional font-family and size selection within CSS based on the input
  • Is there anyway for CSS to know about the input language?

So I could create the following PSEUDO_CSS:

INPUT{ EN-font-family: Verdana; EN-font-size: 12px; PA-font-family; Raavi; EN-font-size: 14px;}

or

INPUT.EN{ font-family: Verdana; font-size: 12px;}
INPUT.PA{ font-family: Raavi; font-size: 14px;}
+4  A: 

A pure CSS solution might be as easy as:

input[lang=en] {
  font-family:Verdana;
  font-size:12px;
}

input[lang=pa] {
  font-family:Raavi;
  font-size:14px;
}

But it's still up to you to set the lang attribute of the input element.

Unfortunately, as with most fancy CSS features, attribute selectors are not 100% working across the array of browsers today. Your best bet in my opinion is to use a class per language and assign it to the input element.

Update:

Per your request, here's an example of a naive way to do it with vanilla JavaScript. There are certainly improvements to be made, but this "works".

<style type="text/css">
  .lang-en {
    font-family:Verdana;
    font-size:12px;
  }

  .lang-pa {
    font-family:Raavi;
    font-size:14px;
  }
</style>

<form>
  <input type="text" onkeyup="assignLanguage(this);" />
</form>

<script type="text/javascript">
  function assignLanguage(inputElement) {
    var firstGlyph = inputElement.value.charCodeAt(0);

    if((firstGlyph >= 65 && firstGlyph <= 90) || (firstGlyph >= 97 && firstGlyph <= 122)) {
      inputElement.setAttribute('lang', 'en');
      inputElement.setAttribute('xml:lang', 'en');
      inputElement.setAttribute('class', 'lang-en');
    } else {
      inputElement.setAttribute('lang', 'pa');
      inputElement.setAttribute('xml:lang', 'pa');
      inputElement.setAttribute('class', 'lang-pa');
    }
  }
</script>

This example fires after a character has been typed. It then checks if it falls between a range considered "English" and assigns attributes accordingly. It sets the lang, xml:lang, and class attributes.

Zack Mulgrew
My problem is that I cannot predict in advance what language will be entered into a single input element
Noah
In that case I recommend a JavaScript solution. You could check the input for a character range and then attach the appropriate language class to the element. The drawback is that the font change may be noticeable. This sounds like a sticky situation, indeed.
Zack Mulgrew
Can you modify your answer to include a JS fragment which will do this. Assume that if the first character is <255 it is English, otherwise Punjabi. Then I'll accept your answer
Noah
On the server side, you could read the browser's Accept-Language header to make an educated guess as to what's coming.
David Kolar
No because its a display issue, rather than a server processing issue
Noah
A: 

How could CSS know about the input language?

I'm afraid the only solution is to find a unicode font which looks pretty for both character sets. Which is far from perfect if your remote reader has not installed it. Maybe Arial Unicode MS.

mouviciel
Yeah, that really is my question: "Is there anyway for CSS to know about the input language?"
Noah
The answer is: No, unfortunately. There is no CSS-only solution.
mouviciel
+3  A: 
input { font-family: Verdana, Raavi, sans-serif; font-size: 14px;}

This should work for your purposes:

  • If the text is English, both fonts should contain the glyphs, and Verdana will be preferred
  • If the text is Punjabi, Verdana should not contain the glyphs, so the browser should fall back to Raavi

I'm not positive if all browsers will behave correctly, but that's what they should do according to the CSS spec.

Miles
See Elijah's answer below. It turns out he was right, and I need to change the font-size as well. I've modified the question to reflect this. Any ideas?
Noah
I like this answer. After reading it I went and checked the CSS2 spec and was delighted to read this functionality outlined there. I never knew this was how it was *supposed* to work ... ;)
Zack Mulgrew
A: 

The only reliable solution for now is to list the fonts in the desired order, as Miles indicated.

Hopefully the (correct) solution indicated by Zack might be properly supported by more browsers.

But even then it will be your responsibility to tag the various sections with the proper lang attribute.

Nothing can reliably detect the language of any text.

Mihai Nita
+2  A: 

It is common practice when maintaining multi-lingual websites to use separate CSS files for each language. This is desirable because you will need to adjust more than the font. You will often need to adjust spacing to match the length of strings in the language. Also, you may need to adjust some of the basic formatting of the page in order to make it more natural to users of the language.

The robust answer is to internationalize and not to just settle for a different font because eventually you will find that font selection will be insufficient.

Elijah
Your are right, because I see now that I also want to adjust the font-size, not just the font. How can I get multiple definitions with just one input box class
Noah