雪漠胡杨

导航

DevExpress 控件的本地化(译)

 

本地化是一个翻译过程,就是将应用程序相应的资源文件翻译成应用程序将要支持的各种语言版本。这就使你构建的已本地化(翻译过的)应用程序可以提供完全本地化的语言界面给你的最终用户。这个主题指导你通过完整的XtraEditors库资源和本地化过程:建立资源并且打包(编译)资源(典型地植入DLL程序集)。下述两种将XtraEditors库本地化的方法

 

创建附属资源程序集


为了你需要的每一种本地化版本的应用程序,你应该建立一个新的包含被翻译成适当的目标语言的本地化资源的附属程序集。.NET框架SDK提供的Windows 窗体资源编辑器 (Winres.exe) 可以让你实现本地化Windows窗体的目的。关于如何使用这个工具的信息可以看Windows 窗体资源编辑器(Winres.exe)题。

默认的XtraEditors数据库只支持英语。为了将编辑器的UI翻译成另一种语言,你必须人工将它本地化。所有可用的XtraEditors库的资源都被储存在LocalizationRes.resx文件的字符串值里。为了创建本地资源,你必须完成下列步骤。

·                 复制LocalizationRes.resx 文件到你的应用程序目录下并且重新命名。命名使用资源命名协议(比如,命名德语的资源文件为 - "LocalizationRes.de.resx").

·                 LocalizationRes.resx文件是被放置在下面的路径:Developer Express Inc.NET\XtraEditors\Sources\DevExpress.XtraEditors\DevExpress.XtraEditors\LocalizationRes.resx

注意:当你打包你的应用程序资源时,你必须使用这个资源命名协议,即公共语言运行时所预期的那样。运行时依靠指定的特定区域性签名或者名字来识别。每一个特定区域性都被赋予了一个唯一的名字,它是由一个两位小写字母特定区域性名与一种语言名组合而成的,如果被要求,它也可以由一个两位大写字母次特定区域性名与一个国家或地区名组合构成。这个次特定区域性名跟随着特定区域性名,用一短横隔开。例如:包含zh-CN的代表用中国中文,en-US代表用美国英语,或者zh-CHS代表用中文(简体)zh-CHT代表用中文(繁体)。参看CultureInfo 以了解全部的有关特定区域性的信息

·                 更改资源。就是将资源字符串翻译成目标语言。这可以在Microsoft Visual Studio .NET中被完成。

使用Resgen.exe工具将更改过的.resx文件转换成一个.resources文件,要建立一个德国本地化的.resources文件,适用下列命令:

resgen LocalizationRes.de.resx LocalizationRes.de.resources

·                 使用程序集链接器 (Al.exe)或者一个与.NET Framework SDK一起捆绑的语言编译器嵌入.resources文件,建立一个附属资源程序集(DLL)。下面的AL.exe命令通过嵌入LocalizationRes.de.resources文件创建一个附属资源程序集:

·                 al /out:DevExpress.XtraEditors.Resources.Dll /v:0.0.0.0 /culture:de /embed:LocalizationRes.de.resources,DevExpress.XtraEditors.LocalizationRes.de.resources /keyfile:strongkey.snk

注意/v:选项为这个附属资源程序集提供版本信息。你应该手动地指定它。

注意:这个程序集应该使用和其他DevExpress~ 程序集一样的强名称文件来建立。如果你使用的是被Developer Express Inc所支持的原版的DLL,请在你方便的时候把你的.resources文件通过Support@DevExpress.com发送到Developer Express Support Team以便于他们能够准备与原始的DevExpress~ assemblies兼容的附属资源程序集并使用我们的Strong key

关于Resgen.exeAl.exe tools的更多信息,分别参看Resource File Generator (Resgen.exe) Assembly Linker (Al.exe)主题。

一旦本地化的附属资源程序集已经建立,它们必须被放在合适的位置以便于它们可以被很容易地找到并使用。否则,公共语言运行时将不能定位它们而使用缺省的资源设置。因此,你应该把它们储存在GAC (Global Assembly Cache)或者把它们放置在下面所描述的位置。

放置程序集到GAC,可以使用由Microsoft Visual Studio .NET提供的Gacutil.exe tool。在放置进缓存之后,一个资源就可以被所有需要它的应用程序使用了。下列命令是安装LocalizerRes.de.resources.dll到全局程序集高速缓冲器:

gacutil /i:LocalizerRes.de.resources.dll

如果本地化程序集无法放置进GAC,它们应该被放置在它们自己的目录里,这是因为你的附属程序集将全部拥有相同的名称。在运行时,它们唯一地引用是使用Al.exe's /culture选项在汇编时被指定的区域性和目录位置。你必须把你的附属程序集放在特定的目录位置。下列插图显示一个目录结构和位置要求的例子,是对于没有被放置进GAC的控制台应用程序。


    下列实例代码,通过更改当前的执行线程的
currently executing thread's CurrentThread.CurrentUICulture属性,为应用程序建立了德语的地方设置。

[C#]

static void Main()

{

    System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de");

    Application.Run(new Form1());

}

[Visual Basic]

Shared Sub Main()

    System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("de")

    Application.Run(New Form1())

End Sub

[JScript] No example is available in JScript. To view a C#, Visual Basic example, click the Language Filter button in the upper-left corner of the page.

 

使用Localizer对象


XtraEditors库的运行时界面能够用Localizer的对象来本地化。只有这样做,你必须首先建立一个Localizer 继承类,然后,重写 Localizer.GetLocalizedString 方法来调整由editors提供的可视化元素的标题。最后,在必须的Localizer 继承类被建立以后,实例化并且设置它的Localizer.Active属性以便起作用。

下面的代码说明如何本地化editors'UI到德言。

[C#]

// creating the Localizer descendant

public class GermanLocalizer : Localizer {

    // overriding the GetLocalizedString method

    public override string GetLocalizedString(StringId id){

        switch(id) {

            // ...

            case StringId.PictureEditMenuCut: return "Ausschneiden";

            case StringId.PictureEditMenuCopy: return "Kopieren";

            case StringId.PictureEditMenuPaste: return "Einfugen";

            case StringId.PictureEditMenuDelete: return "Loschen";

            case StringId.PictureEditMenuLoad: return "Laden";

            case StringId.PictureEditMenuSave: return "Speichern";

            // ...

        }

        return "";

    }

}

[Visual Basic]

Public Class GermanLocalizer

    Inherits Localizer

    ' overriding the GetLocalizedString method

    Public Overrides Function GetLocalizedString(ByVal id As StringID) As String

        Select Case id

            ' ...

            Case StringId.PictureEditMenuCut : Return "Ausschneiden"

            Case StringId.PictureEditMenuCopy : Return "Kopieren"

            Case StringId.PictureEditMenuPaste : Return "Einfugen"

            Case StringId.PictureEditMenuDelete : Return "Loschen"

            Case StringId.PictureEditMenuLoad : Return "Laden"

            Case StringId.PictureEditMenuSave : Return "Speichern"

                ' ...

        End Select

        Return ""

    End Function

End Class

[JScript] No example is available in JScript. To view a C#, Visual Basic example, click the Language Filter button in the upper-left corner of the page.

一旦GermanLocalizer 类被创建,你能够通过控制Localizer.Active 属性来完成本地化工作。这下面的样例代码显示如何在窗体的Load 事件过程中完成它。

[C#]

using DevExpress.XtraEditors.Controls;

 

private void Form1_Load(object sender, System.EventArgs e) {

    GermanLocalizer gLocalizer = new GermanLocalizer();

    Localizer.Active = gLocalizer;

}

[Visual Basic]

Imports DevExpress.XtraEditors.Controls

 

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim gLocalizer As New GermanLocalizer()

    Localizer.Active = gLocalizer

End Sub

[JScript] No example is available in JScript. To view a C#, Visual Basic example, click the Language Filter button in the upper-left corner of the page.

下图显示图片编辑器的本地化上下文菜单。
   

posted on 2005-09-08 19:26  雪漠胡杨  阅读(2090)  评论(0编辑  收藏  举报