数据库本地化 - 查找列表 - 更智能的方式 [英] Database Localization - Lookup lists - smarter way

查看:22
本文介绍了数据库本地化 - 查找列表 - 更智能的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在数据库中添加一些查找列表,但我希望它们易于本地化(SQL 2005,ADO.NET)

这将包括:

  • 同时轻松管理多种语言
  • 从数据库中轻松检索值
  • 后备语言(以防所选语言缺失)

我正在考虑使用一个表来存储多语言查找列表(对不同语言使用相同的 id)并使用一个函数来返回查找列表的值 - 通过接收 ID 和语言.

其中一个陷阱是我必须手动为每个使用查找列表的查询添加语言参数.

我正在寻找一种解决方案,它可以让我将参数作为会话/全局变量"发送,或者使用 sql 查询自动发送参数,以及自行检索它的函数(或者附加参数自动,要么能够读取参数).

解决方案可能看起来像这样,但我不介意它是否不同,只要它没有将参数显式地提供给查询(伪代码):

<块引用>

<代码>1.使用方法"发送语言2.执行查询3. 获取本地化结果

澄清:

  1. 通常查询看起来像这样(记住使用查找功能):

    SELECT .., GetLookupList1(lookup_ID, language), .. FROM TABLE

GetLookupList1 是一个用户定义的函数,用于检索查找表的查找值.通过使用该功能,SQL 代码更易于阅读和维护.

函数的主体类似于:

SELECT @result = LookupValue FROM LookupTable1 WHERE ID=@Lookup_ID and Language=@lang返回@结果

  1. 我想要的是能够将函数中的语言参数删除为某种静态变量,仅可用于当前连接/语句/命令,因此查询看起来像

    SELECT .., GetLookupList1(lookup_ID), .. FROM TABLE

解决方案

由于 SQL Server 中没有用户定义的全局变量,因此您必须使用以下两种方法之一:

  1. 表格 - 临时或永久.永久表示例:http://weblogs.sqlteam.com/mladenp/archive/2007/04/23/60185.aspx.
  2. SET CONTEXT_INFO:http://msdn.microsoft.com/en-我们/图书馆/ms187768.aspx.Context_info 允许您将 128 个二进制字节与会话/连接相关联.它有效,但要小心.如果您养成使用它的习惯,您可能会在另一个上下文中意外覆盖它.每个会话/连接只有一个.

示例 context_info t-sql:

声明@languagein varchar(30), @contextin varbinary(128),@languageout varchar(30)、@contextout varbinary(128)选择@languagein = 'ro-RO'选择@contextin = cast(@languagein as varbinary(128))设置 context_info @contextin--在这里做任何你喜欢的事情:查询、存储过程.--context_info 在会话/连接期间保持ro-RO"选择@contextout = context_info()设置@languageout = replace(cast(@contextout as varchar(30)),0x00, '')打印@languageout

我在本地化中使用的另一种技术是三部分合并以确保结果.首先检查语言区域,然后是语言,然后是默认值.根据您的查询:

SELECT COALESCE(langregion.LookupValue, lang.LookupValue, fallback.LookupValue) LookupValFROM LookupTable1 后备左外连接 LookupTable1 langON lang.ID = fallback.ID AND lang.Lang = @language左外连接 LookupTable1 langregionON langregion.ID = fallback.ID AND langregion.Lang = @languagewithregionWHERE fallback.ID = @Lookup_IDAND fallback.Lang = @defaultlanguage

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 of multiple languages at the same time
  • Easy Retrieval of values from the database
  • Fallback language (in case the selected language is missing)

I was thinking about having a table that would store the multi-language lookup-list (using for different languages the same id) and use a function that would return the value of the look-up list - by receiving the ID and the Language.

One of the pitfalls would be that i have to manually add a language parameter to every query that uses the lookup list.

I'm looking into a solution that would let me send the parameter as a "session/global variable", or that would send the parameter automatically with the sql query, and the function to retrieve it by itself (either to attach the parameter automatically, either to be able to read the parameter).

The solution can look something like this, but I don't mind if it is different, as long as it doesn't give the parameter explicitly to the Query (pseudocode):

1. Send the language using "the method"
2. Execute Query
3. Get the localized results

Clarification:

  1. Normally the query would look like this (remember using the lookup function):

    SELECT .., GetLookupList1(lookup_ID, language), .. FROM TABLE

The GetLookupList1 is a user defined function that retrieves the lookup value for a lookup table. By using this function, the SQL Code is easier to read and maintain.

The body of the function would be something like:

SELECT @result = LookupValue FROM LookupTable1 WHERE ID=@Lookup_ID and Language=@lang
RETURN @result

  1. What I want is to be able to remove the language parameter from the function to some kind of a static variable, available only for the current connection/statement/command, so the query would look like

    SELECT .., GetLookupList1(lookup_ID), .. FROM TABLE

解决方案

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

这篇关于数据库本地化 - 查找列表 - 更智能的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆