Conditionally displaying sIFR text for different languages using content negotiation with Django
I'm using sIFR on the new Singularity teaser site (not least because Mark Wubben is speaking at the conference, mind you) and ran into an issue today with extended characters (such as extended Western characters, Chinese, Japanese, etc.) not displaying properly as they weren't embedded into the Flash text field.
Getting extended Western characters working is not too difficult as you can embed most of them in the sIFR SWF without increasing the size of the SWF too much. The current size of my GillSans SWF currently on the site is 32KB and includes the following character sets from Flash (note that some of these contain overlaps):
- Basic Latin
- Latin I
- Latin Extended A
- Latin Extended B
Embedding fonts for other sets such as Hebrew, however, balloons the size of the SWF to unacceptable sizes.
The workaround I implemented was to use content negotiation to switch sIFR off for various languages. The code (which you can put inside your request handler or in a decorator or middleware method):
use_sifr = True if 'HTTP_ACCEPT_LANGUAGE' in request.META: language = request.META['HTTP_ACCEPT_LANGUAGE'] remove_sifr_for = ('ar', 'zh', 'he', 'ja', 'el', 'ko', 'pa', 'th') for lang_test in remove_sifr_for: if lang_test in language: use_sifr = False context['use_sifr'] = use_sifr
Then, based on the setting of the use_sifr template variable, I conditionally include the JS for sIFR:
You can easily test this out in Firefox through the Preferences panel (⌘ ,) → General → Languages → Choose... and adding one of the languages for which sIFR is switched off (such as Chinese).