WPF制作多语言版本的程序

 

  1. 创建WpfApplication项目Mui_lang,窗口如"运行"的对话框。

 

Xaml 文件如下:

<Window x:Class="Mui_lang.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Run"

    ResizeMode = "NoResize" SizeToContent="WidthAndHeight"

    >

<Grid Background="#E1E0E5" Grid.IsSharedSizeScope="True">

    <Grid.RowDefinitions>

        <RowDefinition />

        <RowDefinition />

        <RowDefinition />

    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>

        <ColumnDefinition/>

        <ColumnDefinition/>

        <ColumnDefinition SharedSizeGroup="Buttons"/>

        <ColumnDefinition SharedSizeGroup="Buttons"/>

        <ColumnDefinition SharedSizeGroup="Buttons"/>

    </Grid.ColumnDefinitions>

    <Button Margin="3,10,3,15" Content="Ok" Grid.Column="2" Grid.Row="2" Padding="3,0,3,0"/>

        <Button Margin="3,10,3,15" Content="Cancel" Grid.Column="3" Grid.Row="2" Padding="3,0,3,0"/>

        <Button Margin="3,10,3,15" Content="Browse..." Grid.Column="4" Grid.Row="2" Padding="3,0,3,0"/>

    <ComboBox Margin="10,0,0,0" IsSynchronizedWithCurrentItem="True" Grid.Column="1" Grid.ColumnSpan="4" Grid.Row="1" HorizontalAlignment="Left" Width="288"/>

    <Image Margin="10,0,0,0" Height="25" Source="runicon.jpg" Stretch="None" HorizontalAlignment="Left"/>

    <TextBlock Margin="10,0,0,0" Grid.Column="1" Grid.ColumnSpan="4" Text="Type the name of a program, folder, document, or Internet resource, and Windows will open it for you." TextWrapping="Wrap" Width="288" VerticalAlignment="Center" HorizontalAlignment="Left" />

    <TextBlock Margin="10,0,0,0" Grid.Row="1" Text="Open:" VerticalAlignment="Center" TextWrapping="Wrap"/>

      

 

</Grid>

</Window>

 

不要给元素添加uid属性。

 

  1. dotnet 命令行窗口使用 msbuild xaml 统一添加 uid

msbuild /t:updateuid mul_lang.csproj

    为了方便使用也可以在vs2008中添加扩展工具

3.在 .csproj 中添加

 

<UICulture>en-US</UICulture>

这时候生成项目会产生特定语言的程序集Mui_lang.resources.dll。位于Bin\Debug\en-US 目录。

 

(注:在vs2008中可以先Unload项目,然后右键单击项目会有编辑*.csproj,编辑完后在重新载入即可)

 

4.使用locbaml工具分析生成的文件。

locbaml 工具从这里下载

    http://download.microsoft.com/download/B/2/5/B25C4C6A-97FE-4014-9D4B-B39607BA9A12/wpf_samples/LocBaml.exe

下载完后自己编译一下才可以使用。

 

然后使用locbaml生成的csv文件:

    locbaml /parse en-us/Mui_lang.resources.dll /out:Mui_lang.csv

(注意路径)

 

使用Excel打开 Mui_lang.csv文件。 使用筛选功能筛选掉本地化类别为"None"的所有项。

 

然后将英文翻译成中文或其它语言,并保存。

 

5.使用 LocBaml 生成新的 .resources.dll 文件

Locbaml /generate en-US/Mui_lang.resources.dll /trans:Mui_lang.csv /out:.\ /cul:zh-CHS

运行完成后生成新的Mui_lang.resources.dll 文件,在主程序目录创建一个特定语言的文件夹例如: zh-CHS 将生成的文件复制到ZH-CHS目录下。

6.测试

    在英文操作系统下运行的效果:

    在中文操作系统下运行的效果:

7.在程序中更改资源

    App.xaml.cs 中添加如下代码即可

    protected override void OnStartup(StartupEventArgs e)

{

Thread.CurrentThread.CurrentUICulture = new CultureInfo("zh-CHS");

base.OnStartup(e);

}

 

 

参考:

http://msdn.microsoft.com/zh-cn/library/ms788718.aspx

http://msdn.microsoft.com/zh-cn/library/ms746621.aspx

http://msdn.microsoft.com/zh-cn/library/aa358535.aspx

 

 

 

代码:

转载于:https://www.cnblogs.com/xingquan/archive/2011/03/24/1994457.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值