2010-03-19 36 views
1

有沒有簡單的方法將Localizable屬性設置爲true以創建新的usercontrols /窗體?理想情況下,設置的範圍應該是解決方案或項目。Winforms:默認啓用本地化(強制執行項目/解決方案策略)

換句話說,我想說這個項目/解決方案應該是可本地化的,然後如果我添加一個新的窗體或控件VS應該自動將該屬性設置爲true。

編輯:

雖然自定義模板是可能的,在一個更大的團隊,他們可能不會總是使用。因此,更重要的是執行策略,確保團隊成員不要忽略爲項目/解決方案設置屬性,因爲所有包含文本資源的表單/控件都應該是可本地化的。

注意: Team Foundation Server不是選件。

回答

2

不知道是否值得爲一個易於更改且容易看到它具有錯誤價值的屬性付出努力。但是你可以創建自己的項目模板。

例如:項目+添加用戶控件。將其Localizable屬性設置爲True。文件+導出模板,選擇項目模板。下一個。檢查你添加的控件。下一個。檢查所有參考文獻,只省略那些你永遠不需要的參考文獻。下一個。給它是很好的模板名稱(比如:「Localizable User Control」)。

您現在將擁有可用於未來項目的項目模板,該項目具有屬性設置。根據需要重複其他項目模板,如窗體。

+0

對不起,在OP中不清楚。模板是這樣做的一種方式,但是,他們沒有執行本地化屬性需要爲真的策略。 – AxelEckenberger

+2

Hmya,政策執行的選項在這裏非常有限。 Visual Studio讓程序員負責,而不是老闆。您可以編輯標準模板。 –

2

可以編寫一個使用反射來確定表單/用戶控件是否已被標記爲可本地化的單元測試。具體而言,如果某個類型已被標記爲可本地化,則會有一個嵌入的資源文件與該類型關聯,並且該文件將包含「>> $ this.Name」值。這裏有一些示例代碼:

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; 
    } 

請讓我知道這是否有幫助。

相關問題