开发者

Winforms: Enabling Localization by default (enforcing a project/solution policy)

Is there an easy way to set the Localizable property to true for newly created usercontrols / forms? The scope of the setting should ideally be a solution or a project.

开发者_StackOverflow社区

In other words I want to say that this project/solution should be localizable, and then if I add a new form or control VS should automatically set the property to true.

Edit:

Although custom templates are possible, in a larger team they might not be always used. So it's more about enforcing a policy, ensuring that the team members do not ommit to set the property for the projects/solutions where it is a requirement that all forms/controls containing text resources should be localizable.

Note: Team Foundation Server is not an Option.


Not sure if it is worth the effort for a property that is so easy to change and so easy to see that it has the wrong value. But you can create your own item template.

For example: Project + Add User Control. Set its Localizable property to True. File + Export Template, select Item template. Next. Check the control you added. Next. Check all references, omit only the ones you'll never need. Next. Give it is good template name (say: "Localizable User Control").

You'll now have an item template available for future projects that has the property set. Repeat as necessary for other item templates, like a Form.


It's possible to write a unit test that uses reflection to determine whether a form/user control has been marked as localizable. Specifically, if a type has been marked as localizable, there will be an embedded resource file associated with the Type and that file will contain a ">>$this.Name" value. Here's some sample code:

    private void CheckLocalizability()
    {
        try
        {
            Assembly activeAssembly = Assembly.GetAssembly(this.GetType());
            Type[] types = activeAssembly.GetTypes();
            foreach (Type type in types)
            {
                if (TypeIsInheritedFrom(type, "UserControl") || TypeIsInheritedFrom(type, "Form"))
                {
                    bool localizable = false;
                    System.IO.Stream resourceStream = activeAssembly.GetManifestResourceStream(string.Format("{0}.resources", type.FullName));
                    if (resourceStream != null)
                    {
                        System.Resources.ResourceReader resourceReader = new System.Resources.ResourceReader(resourceStream);
                        foreach (DictionaryEntry dictionaryEntry in resourceReader)
                        {
                            if (dictionaryEntry.Key.ToString().Equals(">>$this.Name", StringComparison.InvariantCultureIgnoreCase))
                            {
                                localizable = true;
                                break;
                            }
                        }
                    }
                    if (!localizable)
                    {
                        Debug.Assert(false, string.Format("{0} is not marked localizable.", type.FullName));
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Debug.Assert(false, string.Format("Exception occurred: Unable to check localization settings.  {0}", ex.Message));
        }
    }

    private bool TypeIsInheritedFrom(Type type, string baseType)
    {
        while (type != null)
        {
            if (type.Name.Equals(baseType, StringComparison.InvariantCultureIgnoreCase))
                return true;
            type = type.BaseType;
        }

        return false;
    }

Please let me know if this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜