Best-practices for localizing a SQL Server (2005/2008) database

后端 未结 10 1268
执笔经年
执笔经年 2020-12-13 11:09

Question

I\'m sure many of you have been faced by the challenge of localizing a database backend to an application. If you\'ve not then I\'d be pretty confident in

10条回答
  •  暖寄归人
    2020-12-13 11:39

    Here is how I've done it. I don't use LINQ or SP for this one, because the query is too complex and is dynamically built and this is just a excerpt of the query.

    I have a products table:

    * id
    * price
    * stocklevel
    * active
    * name
    * shortdescription
    * longdescription
    

    and a products_globalization table:

    * id
    * products_id
    * name
    * shortdescription
    * longdescription
    

    As you can see the products-table contains all the globalization-columns aswell. These columns contains the default-language (thus, being able to skip doing a join when requesting the default culture - BUT I'm not sure if this is worth the trouble, I mean the join between the two tables are index-based so... - give me some feedback on this one).

    I prefer having a side-by-side table over a global-resourcetable because in certain situations you might need to do i.e. a database (MySQL) MATCH on a couple of columns, such as MATCH(name, shortdescription, longdescription) AGAINST ('Something here').

    In a normal scenario some of the product translations might be missing but I still want to show all products (not just the ones who are translated). So it's not enough to do to a join, we actually need to do a left join based on the products-table.

    Pseudo:

    string query = "";
    if(string.IsNullOrEmpty(culture)) {
       // No culture specified, no join needed.
       query = "SELECT p.price, p.name, p.shortdescription FROM products p WHERE p.price > ?Price";
    } else {
       query = "SELECT p.price, case when pg.name is null then p.name else pg.name end as 'name', case when pg.shortdescription is null then p.shortdescription else pg.shortdescription end as 'shortdescription' FROM products p"
       + " LEFT JOIN products_globalization pg ON pg.products_id = p.id AND pg.culture = ?Culture"
       + " WHERE p.price > ?Price";
    }
    

    I would go with COALESCE instead of CASE ELSE but thats besides the point.

    Well, thats my take on it. Feel free to critize my suggestion...

    Kind regards, Richard

提交回复
热议问题