c# - 本地化 - 在 wpf 应用程序中更改语言
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37793952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# - localization - changing language in wpf app
提问by Nichten
I am making some wpf application and I need option for changing languages. I have folder named Resorces in my solution, where I store all my resx file - actually language.resx and language.en-EN.resx. My XAML looks like:
我正在制作一些 wpf 应用程序,我需要更改语言的选项。我的解决方案中有名为 Resorces 的文件夹,我在其中存储了所有 resx 文件 - 实际上是 language.resx 和 language.en-EN.resx。我的 XAML 看起来像:
<Window x:Class="CoinCatalogue.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CoinCatalogue"
xmlns:resx="clr-namespace:CoinCatalogue.Resources"
mc:Ignorable="d"
Title="{x:Static resx:language.WindowName}" Height="480" Width="640">
<Grid>
<Menu x:Name="menu" HorizontalAlignment="Left" Height="auto" VerticalAlignment="Top" Width="517">
<MenuItem x:Name="File" Header="{x:Static resx:language.File}">
<MenuItem x:Name="Open" Header="{x:Static resx:language.Open}"/>
</MenuItem>
</Menu>
</Grid>
Everything is ok - I have access to string in app. And in my main class I am trying to change culture with line:
一切正常 - 我可以访问应用程序中的字符串。在我的主要课程中,我试图通过以下方式改变文化:
language.Culture = new System.Globalization.CultureInfo("en-EN");
But nothings happen to my app. This line is before InitializeComponent();.
但是我的应用程序没有任何反应。这一行在 InitializeComponent(); 之前。
What am I doing wrong? I am using Visual Studio 2015. /is it a good idea to change app language in this way?
我究竟做错了什么?我正在使用 Visual Studio 2015。/以这种方式更改应用程序语言是个好主意吗?
回答by Gary Wright
I think how you are changing the culture is the issue.
我认为你如何改变文化是问题所在。
I just put a quick sample together where I had the following resx files:
我只是将一个快速示例放在一起,其中包含以下 resx 文件:
Resources.en-US.resx
Resources.fr-FR.resx
Resources.resx
Resources.en-US.resx
资源.fr-FR.resx
资源.resx
If I run the app without attempting to change the culture, I get the strings from the Resources.en-US.resxfile as that is my default. If I add this code before InitializeComponentmy strings come from the Resources.fr-FR.resxfile:
如果我在不尝试更改文化的情况下运行该应用程序Resources.en-US.resx,则会从文件中获取字符串,因为这是我的默认设置。如果在InitializeComponent我的字符串来自Resources.fr-FR.resx文件之前添加此代码:
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("fr-FR");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("fr-FR");

