Symfony - Returning content in default language, no matter what.

March 10, 2008 – 10:28 am

If you have even worked on an international site using Symfony, you’ll be familiar with the ease you can switch between languages on a model level. I’m loving this, because you don’t have to worry about it in the controller or the view, meaning you can easily add i18n dimensions to pretty much any application in one central place ( the propel builder classes ).

That being said, I found the default behavior insufficient for sites that don’t have the resources to translate absolutely everything. In many cases, our clients focus their translation efforts on the most important product lines, press releases, or event postings, and are happy to default back to English for everything else.
In some cases this means translating only selected fields, and this is where it gets tricky, because Symfony will only retrieve i18n content for the current culture, so leaving fields blank in a translation won’t prompt it to retrieve the default language instead (so far), so what we need here is a way to make every i18n accessor method smart enough to grab default content if the translation returns nothing.

Enter the propel model builder classes. Thanks to the pattern-al™ ;) foresight of the Symfony creators we have a central access point to all auto generated accessor methods via the sfObjectBuilder class in your Symfony/lib/addon/propel/builder directory, which we can extend and overwrite to meet our requirements.

As a result every i18n accessor method now tests whether there is a string to return, and, if not, the object switches its culture to default and queries again, switching back afterwards.

Now, I have to qualify that what I did here might not be the prettiest solution, and I’m not quite comfortable with the amount of parent code that I had to copy into the child class to make the overridden functions work, but it works, and if anybody feels like cleaning it up, be my guest and send me a copy.

A word about performance

The overall approach I took is clearly geared towards development speed, not neccessarily runtime efficiency, so if you output a long list of i18n objects, odds are that you’ll fire off a query for each individual one. Functionality-wise that works, and speed is actually surprisingly fast, but that’s considering a rather small database and your particular server might puke. Haven’t tested on large system, but I would believe caching would alleviate this.
Also, I would imagine that propel 1.3 could alleviate this via object cache, where we could run one query to retrieve all applicable i18n objects before we output the list, and all future references to i18n content would come from cache. Haven’t tried this yet.

How to use this:

Download and unzip this SFi18nFallbackObjectBuilder class into the Symfony/lib/addon/propel/builder directory and edit the config/propel.ini file to use this file for propel.builder.object.class (instead of sfObjectBuilder).

Then all you should have to do is rebuild the model and activate it in the configuration by adding

use_fallback: true

to your i18n.yml configuration (for all or just some environments). I added this configuration switch, so that you can turn this off for admin areas, where you would otherwise not have empty fields for translations.

That’s it, have fun, and let me know how I could improved this somehow.


Filed under: Symfony — Tags: , , — by Richtermeister

6 Comments »

  1. Yr4ddq hi nice site thanks http://peace.com

    Comment by bob — May 8, 2008 @ 2:38 am

  2. Hi,

    Does this patch work in symfony 1.1 ?

    Grtz

    Toni

    Comment by Toni Van de Voorde — May 18, 2008 @ 10:26 pm

  3. Re,

    The builder works in symfony 1.1 . For the moment I only have a problem with the fetch of sfConfig::get(”sf_i18n_use_fallback”) and sfConfig::get(”sf_i18n_default_culture”) . It always returns an empty string :s

    I’m investigating this issue

    many thx for the patch you wrote.

    Toni

    Comment by Toni Van de Voorde — May 19, 2008 @ 1:51 am

  4. You’re welcome :) Glad someone is using it.

    The “sf_i18n_use_fallback” must be set in the configuration.. by default it’s not there and hence returns nothing (or empty string)..
    I now wonder if I should make it app_i18n_use_fallback… that would make more sense I guess.

    The default culture appears once you activate i18n support for your app, at least it should.

    Good luck, let me know how it works out.
    Daniel

    Comment by Richtermeister — May 19, 2008 @ 8:24 am


  5. CheapTabletsOnline.Com. Canadian Health&Care.Best quality drugs.Special Internet Prices.No prescription online pharmacy. High quality pills. Order pills online

    Buy:Nymphomax.Aricept.Lasix.Advair.Female Pink Viagra.Wellbutrin SR.Lipitor.Lipothin.Zetia.Acomplia.Seroquel.SleepWell.Cozaar.Female Cialis.Ventolin.Prozac.Buspar.Amoxicillin.Benicar.Zocor….

    Trackback by RALPH — September 5, 2010 @ 9:40 am


  6. CheapTabletsOnline.com. Canadian Health&Care.Special Internet Prices.Best quality drugs.No prescription online pharmacy. High quality drugs. Order pills online

    Buy:VPXL.Cialis Professional.Cialis Soft Tabs.Tramadol.Propecia.Viagra Soft Tabs.Soma.Super Active ED Pack.Viagra Super Force.Viagra.Zithromax.Cialis Super Active+.Maxaman.Viagra Super Active+.Viagra Professional.Levitra.Cialis….

    Trackback by TERRY — September 6, 2010 @ 6:00 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress