这里有一些关于 Rick Strahl 博客的想法:
数据库
本地化 JavaScript 本地化
我更喜欢在 UserSetting 表中使用单个开关,通过调用存储过程来使用它......这里有一些代码
CREATE TABLE [dbo].[Lang_en_US_Msg](
[MsgId] [int] IDENTITY(1,1) NOT NULL,
[MsgKey] [varchar](200) NOT NULL,
[MsgTxt] [varchar](2000) NOT NULL,
[MsgDescription] [varchar](2000) NOT NULL,
CONSTRAINT [PK_Lang_US-us__Msg] PRIMARY KEY CLUSTERED
(
[MsgId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[User](
[UserId] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [varchar](50) NOT NULL,
[MiddleName] [varchar](50) NULL,
[LastName] [varchar](50) NULL,
[DomainName] [varchar](50) NULL,
CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED
(
[UserId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE TABLE [dbo].[UserSetting](
[UserSettingId] [int] IDENTITY(1,1) NOT NULL,
[UserId] [int] NOT NULL,
[CultureInfo] [varchar](50) NOT NULL,
[GuiLanguage] [varchar](10) NOT NULL,
CONSTRAINT [PK_UserSetting] PRIMARY KEY CLUSTERED
(
[UserSettingId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
去
ALTER TABLE [dbo].[UserSetting] ADD CONSTRAINT [DF_UserSetting_CultureInfo] DEFAULT ('fi-FI') FOR [CultureInfo]
GO
CREATE TABLE [dbo].[Lang_fi_FI_Msg](
[MsgId] [int] IDENTITY(1,1) NOT NULL,
[MsgKey] [varchar](200) NOT NULL,
[MsgTxt] [varchar](2000) NOT NULL,
[MsgDescription] [varchar](2000) NOT NULL,
[DbSysNameForExpansion] [varchar](50) NULL,
CONSTRAINT [PK_Lang_Fi-fi__Msg] PRIMARY KEY CLUSTERED
(
[MsgId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
CREATE PROCEDURE [dbo].[procGui_GetPageMsgs]
@domainUser varchar(50) , -- the domain_user performing the action
@msgOut varchar(4000) OUT, -- the (error) msg to be shown to the user
@debugMsgOut varchar(4000) OUT , -- this variable holds the debug msg to be shown if debug level is enabled
@ret int OUT -- the variable indicating success or failure
AS
BEGIN -- proc start
SET NOCOUNT ON;
declare @procedureName varchar(200)
declare @procStep varchar(4000)
set @procedureName = ( SELECT OBJECT_NAME(@@PROCID))
set @msgOut = ' '
set @debugMsgOut = ' '
set @procStep = ' '
BEGIN TRY --begin try
set @ret = 1 --assume false from the beginning
--===============================================================
--debug set @procStep=@procStep + 'GETTING THE GUI LANGUAGE FOR THIS USER '
--===============================================================
declare @guiLanguage nvarchar(10)
if ( @domainUser is null)
set @guiLanguage = (select Val from AppSetting where Name='guiLanguage')
else
set @guiLanguage = (select GuiLanguage from UserSetting us join [User] u on u.UserId = us.UserId where u.DomainName=@domainUser)
set @guiLanguage = REPLACE ( @guiLanguage , '-' , '_' ) ;
--===============================================================
set @procStep=@procStep + ' BUILDING THE SQL QUERY '
--===============================================================
DECLARE @sqlQuery AS nvarchar(2000)
SET @sqlQuery = 'SELECT MsgKey , MsgTxt FROM dbo.lang_' + @guiLanguage + '_Msg'
--===============================================================
set @procStep=@procStep + 'EXECUTING THE SQL QUERY'
--===============================================================
print @sqlQuery
exec sp_executesql @sqlQuery
set @debugMsgOut = @procStep
set @ret = @@ERROR
END TRY --end try
BEGIN CATCH
PRINT 'In CATCH block.
Error number: ' + CAST(ERROR_NUMBER() AS varchar(10)) + '
Error message: ' + ERROR_MESSAGE() + '
Error severity: ' + CAST(ERROR_SEVERITY() AS varchar(10)) + '
Error state: ' + CAST(ERROR_STATE() AS varchar(10)) + '
XACT_STATE: ' + CAST(XACT_STATE() AS varchar(10));
set @msgOut = 'Failed to execute ' + @sqlQuery
set @debugMsgOut = ' Error number: ' + CAST(ERROR_NUMBER() AS varchar(10)) +
'Error message: ' + ERROR_MESSAGE() + 'Error severity: ' + CAST(ERROR_SEVERITY() AS varchar(10)) +
'Error state: ' + CAST(ERROR_STATE() AS varchar(10)) + 'XACT_STATE: ' + CAST(XACT_STATE() AS varchar(10))
--record the error in the database
--debug
--EXEC [dbo].[procUtils_DebugDb]
-- @DomainUser = @domainUser,
-- @debugmsg = @debugMsgOut,
-- @ret = 1,
-- @procedureName = @procedureName ,
-- @procedureStep = @procStep
-- set @ret = 1
END CATCH
return @ret
END --procedure end