Database Localization - Lookup lists - smarter way

后端 未结 3 1271
感动是毒
感动是毒 2021-01-01 06:24

I\'m looking to add some lookup lists in the database, but I want them to be easy localizable (SQL 2005, ADO.NET)

This would include:

  • Easy Management
相关标签:
3条回答
  • 2021-01-01 06:49

    If you structure your data like this:

    MessageToken    DisplayText       LangCode
    firewood        Fire wood         en
    firewood        Bois de chauffage fr
    

    When you make your query, just supply the default languageId (if blank) or the supplied languageId. Use a standard list of tokens for the messages.

    Select DisplayText from (some table) where MessageToken = 'firewood' and LangId = 'en'
    
    0 讨论(0)
  • 2021-01-01 06:50

    After studying the problem in detail I have found the following:

    1. I could use the SET CONTEXT_INFO, but I would have to inject some SQL to solve the problem.

    2. The best option would be not to store localized data in the look-up tables. Instead, store some identification strings, and use custom localization logic in the application to match the strings to localized data. For the .NET framework it would be implemented by using resources, with a custom resource provider if I want to retreive localized information from a databse.

    Thank you for your answers.

    0 讨论(0)
  • 2021-01-01 06:51

    Since there are no user-defined global variables in SQL Server, you'll have to use one of two approaches:

    1. Tables - temporary or permanent. Example with permanent tables: http://weblogs.sqlteam.com/mladenp/archive/2007/04/23/60185.aspx.
    2. SET CONTEXT_INFO: http://msdn.microsoft.com/en-us/library/ms187768.aspx. Context_info lets you associate 128 binary bytes to a session/connection. It works but be careful. If you get in the habit of using it, you run the risk of accidentally overwriting it in another context. There's only one per session/connection.

    Example context_info t-sql:

    declare @languagein varchar(30), @contextin varbinary(128),
        @languageout varchar(30), @contextout varbinary(128)
    
    select @languagein = 'ro-RO'
    select @contextin = cast(@languagein as varbinary(128))
    set context_info @contextin
    
    --do whatever you like here: queries, stored procs. 
    --context_info stays 'ro-RO' for the duration of the session/connection
    
    select @contextout = context_info()
    set @languageout = replace(cast(@contextout as varchar(30)),0x00, '')
    print @languageout
    

    Another technique I've used in localization is a three part coalesce to insure a result. Check for language-region first, then language, then a default. Based on your query:

    SELECT COALESCE(langregion.LookupValue, lang.LookupValue, fallback.LookupValue) LookupVal
    FROM LookupTable1 fallback
    LEFT OUTER JOIN LookupTable1 lang 
        ON lang.ID = fallback.ID AND lang.Lang = @language
    LEFT OUTER JOIN LookupTable1 langregion 
        ON langregion.ID = fallback.ID AND langregion.Lang = @languagewithregion
    WHERE fallback.ID = @Lookup_ID
    AND fallback.Lang = @defaultlanguage
    
    0 讨论(0)
提交回复
热议问题