- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在搜索如何更改具有 Localizable
的表单的语言属性设置为 true。
https://msdn.microsoft.com/en-us/library/system.threading.thread.currentuiculture(v=vs.110).aspx
这个是设置窗体的语言,但是这个需要在我们实例化窗体之前设置。在此事件之后无法调用。
搜索资料,我看到了以下问题:https://stackoverflow.com/a/11738932/3286975但是,正如评论所说,我在 TabControl 和 MenuStrip 中有控件,因此它们不受影响。
我试图通过获取表单的所有控件来修改它,但没有运气。
在此菜单中,我调用了以下回调:
private void englishToolStripMenuItem_Click_1(object sender, EventArgs e)
{
string lang = (string) ((ToolStripMenuItem) sender).Tag;
base.Culture = CultureInfo.CreateSpecificCulture(lang);
}
private void spanishToolStripMenuItem_Click(object sender, EventArgs e)
{
string lang = (string) ((ToolStripMenuItem) sender).Tag;
base.Culture = CultureInfo.CreateSpecificCulture(lang);
}
我通过使用标签来改变文化。
当我点击它时没有任何反应。此外,我对上述答案中的 ApplyResources 方法进行了一些修改。
private void ApplyResources(Control parent, CultureInfo culture)
{
this.resManager.ApplyResources(parent, parent.Name, culture);
foreach (Control ctl in parent.IterateAllChildren())
{
//this.ApplyResources(ctl, culture);
this.resManager.ApplyResources(ctl, ctl.Name, culture);
}
}
其中 IterateAllChildren 如下:https://stackoverflow.com/a/16725020/3286975
此外,我尝试使用 (System.LINQ):Controls.OfType<Label>()
(因为我有一个标签来测试这个)运气不好......
但是当我选择西类牙语时,没有任何文本发生变化。
所以也许,我对 children 感到失望。或者可能通过调用方法 CreateCulture
,我不知道。
提前致谢!
编辑:
我已经测试过通过 Culture Info 获取我的表单的资源管理器,它每次都返回默认值:
ResourceSet resourceSet = new ResourceManager(typeof(frmCredentials)).GetResourceSet(new CultureInfo(lang), true, true);
foreach (DictionaryEntry entry in resourceSet)
{
string resourceKey = entry.Key.ToString();
object resource = entry.Value; //resourceSet.GetString(resourceKey);
if (resource.GetType().Equals(typeof(string)))
Console.WriteLine("Key: {0}\nValue: {1}\n\n", resourceKey, (string) resource);
}
在哪里new CultureInfo(lang)
,我也测试过:new CultureInfo("es")
& Thread.CurrentThread.CurrentCulture
(CurrentUICulture) 没有运气。就像它从不存在或被替换,但在我的设计和文件资源管理器中我可以看到这些文件。
编辑 2:
可能是因为我正在使用 ILMerge 将所有 dll 合并到一个唯一的 dll 中。我正在审查这个:Single-assembly multi-language Windows Forms deployment (ILMerge and satellite assemblies / localization) - possible?
回复EDIT2:
是的,删除ILMerge问题解决了,我给出的第一个解决方案解决了这个问题。但由于某种原因,西类牙语被用作默认语言,当我尝试从中获取资源集时,它没有返回任何内容。
此外,我已将 Localizable 属性设置为 false,并且它没有创建带有值的默认 resx 文件。我不知道这是否是一个好习惯。
我会尝试一些新的东西...
最佳答案
MVVM(模型 - View - ViewModel)方法有一些好处,对您的情况很有用。
为您将用于本地化的语言创建新的资源文件。使用表单自己的资源文件可能有点棘手,因为每次您在设计器中进行更改时它都会重新生成 - 所以我认为自己的资源文件将更容易维护,甚至与其他表单甚至项目共享。
LocalizationValues.resx // (default english), set Access Modifier to "Internal" or "Public"
"Title": "Title"
"Description": "Description"
LocalizationValues.es.resx
"Title": "Título"
"Description": "Descripción"
Visual Studio 生成静态类 LocalizationValues
,并将属性作为 .resx 文件的键。所以“Title”可以作为 LocalizationValues.Title
创建代表您在本地化中使用的所有文本的“viewmodel”类。类应实现 INotifyPropertyChanged
接口(interface)。
public class LocalizationViewModel : INotifyPropertyChanged
{
public string Title
{
get
{
return LocalizationValues.Title;
}
}
public string Description
{
get
{
return LocalizationValues.Description;
}
}
public void SetLanguage(string language)
{
var culture = new CultureInfo(language);
Thread.CurrentThread.CurrentUICulture = culture;
// This is important,
// By raising PropertyChanged you notify Form to update bounded controls
NotifyAllPropertyChanged();
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyAllPropertyChanged()
{
// Passing empty string as propertyName
// will indicate that all properties of viewmodel have changed
NotifyPropertyChanged(string.Empty);
}
protected void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
然后在Form中将viewmodel绑定(bind)到控件上
public partial class YourForm : Form
{
private LocalizationViewModel _viewmodel;
public YourForm()
{
InitializeComponent();
_viewmodel = new LocalizationViewModel();
// Bound controls to correspondent viewmodel's properties
LblTitle.DataBindings.Add("Text", _viewmodel, "Title", true);
LblDescription.DataBindings.Add("Text", _viewmodel, "Description", true);
}
// Menu buttons to change language
private void SpanishToolStripMenuItem_Click(object sender, EventArgs e)
{
_viewmodel.SetLanguage("es");
}
private void EnglishToolStripMenuItem_Click(object sender, EventArgs e)
{
_viewmodel.SetLanguage("en");
}
}
上述方法将提供比仅更新控件更多的好处。您可以清楚地分离应用程序的各个部分,这些部分可以相互独立地进行测试。
关于c# - 在 WinForms 中以可本地化的形式在运行时更改 CurrentUICulture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46007638/
在 .NET 应用程序中,您似乎可以通过以下方式获取或设置当前 (UI) 区域性 System.Threading.Thread.CurrentThread.CurrentUICulture 或通过
Windows 7 区域和语言对话框中的各种设置为 CurrentCulture 对象的属性提供值。然而,WPF控件似乎改用CurrentUICulture,导致完全无法尊重用户的偏好。 例如,在我的
我正在使用 CurrentUICulture 进行本地化。在不同的线程中查询 UI Culture 之前,这工作得很好。所以我的问题是如何修复 CurrentUICulture 以便在我创建新线程时它
我正在尝试更改我的 WPF 应用程序在点击事件中使用的语言,但它没有改变。 private void menuItemGerman_Click(object sender, RoutedEventAr
我有一个 asp.net mvc 应用程序,我希望用户能够更改语言。我提供了一系列带有小标志的链接,让用户选择语言。所有这些链接的目标是我的“仪表板”页面,其中 Controller 有以下代码: [
我的 MVC 项目中有以下两个文件: 我像这样在我的 global.asax 中设置文化: Thread.CurrentThread.CurrentUICulture = new CultureInf
我一直在搜索如何更改具有 Localizable 的表单的语言属性设置为 true。 https://msdn.microsoft.com/en-us/library/system.threading
据我了解,默认情况下,报表查看器的语言由 "CultureInfo.InstalledUICulture" 设置(不是那么简单)。这意味着,如果我的计算机来自巴西,那么它将使用巴西的文化来格式化数字。
我正在尝试找出程序的语言并为该语言更改我的字符串 CultureInfo culture = new CultureInfo("en"); CultureInfo currentCulture = T
我正在使用 C# 开发 SL5 应用程序,我希望将其国际化。我发现以下设置 UI 文化: var culture = new CultureInfo(Thread.CurrentThread.Curr
我试图在运行时设置应用程序的 CurrentUICulture。但是,我的表单中的菜单项保持不变。除了更改 CurrentUICulture 之外,我还需要做一些额外的事情吗? 最佳答案 聚会又迟到了
我有一个使用 .NET 3.5 框架的基于 WinForms 的应用程序。该应用程序具有某些资源的本地化版本。 当我在运行 Windows XP 副本的机器上运行该应用程序时,该副本最初安装时使用 e
我正在阅读一个资源,它说: CurrentUICulture must be set at the startup of a application. 对于 ASP.NET 网页,我应该在哪里适本地设
我正在用两种语言开发一个多语言网站,从阿拉伯语和英语开始 我已经在 IE 和 FireFox 中将语言设置为阿拉伯语 (ar-ae),但它总是给我英语版本,我无法解决这个问题 网站结构 Default
所以,我有一些本地化的资源文件。它们是可以格式化的字符串。 例如: MyResource.resx Title: "MyApp - Ver: {0}" 然后我这样返回它: public String
我正在一个小型测试 WPF 应用程序中进行本地化,并且想要英语和法语。我创建了 Resources.en-CA.resx 和 Resources.fr-CA.resx。 当我的测试应用程序启动时,我有
我正在尝试让我的应用程序支持多种语言。例如,我制作了一些附属程序集,现在我想测试应用程序在法国机器上运行时的外观。 在[控制面板->区域和语言选项]中,我可以在区域选项选项卡中选择法语(法国),在高级
CurrentCulture有什么区别, InvariantCulture , CurrentUICulture和 InstalledUICulture来自 System.Globalization.
我使用 .NET 3.5(网站项目)在 ASP.NET 中编写了页面。 我想使用 jQuery 添加一些特定于文化的验证器到我的页面。 我受伤了关于使用 Globalize 插件的很好的教程:http
我正在处理一个 ASP.NET Web API 项目,在该项目中我遇到了一个问题,其中子项 Task异步运行的 s 不继承父线程的当前文化;即,即使在设置了 Thread.CurrentThread.
我是一名优秀的程序员,十分优秀!