问题
I am developing in Visual Studio 2013 (C#) and am searching for a way to make the selection of languages for a windows form easier for localization.
In the designer you have the possibility to select the language you want under "Design"→"Language" and Visual Studio does the rest by creating the necessary localization files if you need a new localized form.
The problem is: Currently I only work with two different languages, and every time when I switch between those localized forms it shows me a list of all possible languages, where a wrong click can cause visual studio to create localized version of a language I don't want, which is just annoying and clutters the project with more files.
Does anyone know a way to limit the languages shown in the designer options to a desired amount and selection?
回答1:
Language property is a design-time only property which doesn't belong to Form
class. It's an extended property added using an extender provider to design-time of form. It is of type of CultureInfo
and uses a TypeConverter
which shows all available cultures.
As a workaround , you can have a BaseForm
containing a property like FormLanguage
and in the get
, return value of Language
property and in the set
, set the value of Language
property. Then inherit all your forms from this BaseForm
. This way, it's enough to change FormLanguage
property.
Also create a custom type converter for CultureInfo
which shows only those culture which you want, then to change language, it's enough to change FormLanguage
property.

Here is the code I used as workaround. Don't forget to inherit your forms from this BaseClass
.
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
public partial class BaseForm : Form
{
[TypeConverter(typeof(MyCultureInfoConverter))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public CultureInfo FormLanguage
{
get
{
return TypeDescriptor.GetProperties(this)["Language"]
.GetValue(this) as CultureInfo;
}
set
{
TypeDescriptor.GetProperties(this)["Language"].SetValue(this, value);
}
}
}
public class MyCultureInfoConverter : CultureInfoConverter
{
public override StandardValuesCollection
GetStandardValues(ITypeDescriptorContext context)
{
var values = CultureInfo.GetCultures(CultureTypes.SpecificCultures |
CultureTypes.NeutralCultures)
.Where(x => x.Name == "fa-IR" || x.Name == "en-US").ToList();
values.Insert(0, CultureInfo.InvariantCulture);
return new StandardValuesCollection(values);
}
}
When you select fa-IR
from FormLanguage
then the Language
becomes Persian
automatically because of the code which we wrote in setter of FormLanguage
. You can add any other language which you need in MyCultureInfoConverter
.
来源:https://stackoverflow.com/questions/36932205/visual-studio-designer-limit-property-grid-to-show-some-specific-languages-for