Sviluppo di un sito multilingua... e 3

di Stefano Mostarda

Dopo il db, ecco come ho esteso i controlli base del Framework. Prima di tutto ho definito 3 interfacce, una base e 2 che ereditano da questa:

public interface ILocalizable{ 
  string LocalizationCode{ 
    get; 
    set; 
  } 
} 
public interface ISingleLocalizable : ILocalizable{ 
  string LocalizationText{ 
    get; 
    set; 
  } 
} 
public interface IMultipleLocalizable : ILocalizable{ 
  string[] LocalizationText{ 
    get; 
    set; 
  } 
} 
<p />public class GNLiteral : System.Web.UI.WebControls.Literal, ISingleLocalizable{ 
  public string LocalizationText{ 
    get { return Text; } 
    set { Text = (value == null) ? "???" : value; } 
  } 
  public string LocalizationCode{ 
    get { 
      if (ViewState["IDLOC"] != null){ 
        return Convert.ToString(ViewState["IDLOC"]); 
      } 
      return String.Empty;  
    } 
    set { ViewState["IDLOC"] = value; } 
  } 
}

La seconda interfaccia serve per i controlli di testo semplici come label, literal e button che visualizzano solo una stringa.

La terza interfaccia serve per controlli che visualizzano più testi come ListBox, DropDownList, CheckboxList ecc.

public class GNDropDownList : System.Web.UI.WebControls.DropDownList, IMultipleLocalizable{ 
  public string[] LocalizationText{ 
    get {  
      string[] result = new string[Items.Count]; 
      for (int i=0; i<items.count; />        result[i] = Items[i].Text; 
      } 
      return result;  
    } 
    set {  
      for (int i=0; i<items.count; />        if (value == null || value.Length <= i) 
          Items[i].Text = "???"; 
        else 
          Items[i].Text = value[i]; 
      } 
    } 
  } 
  public string LocalizationCode{ 
    get { 
      if (ViewState["IDLOC"] != null){ 
        return Convert.ToString(ViewState["IDLOC"]); 
      } 
    return String.Empty; 
    } 
    set { ViewState["IDLOC"] = value; } 
  } 
}

Le interfacce vengono utilizzate per astrarre il nome della proprietà che contiene il testo da tradurre, lasciando al controllo l'implementazione. Per esempio, se per un Button la proprietà da tradurre è "Text", per un Validator è "ErrorMessage".

Nel prossimo post, concluderò mostrando il metodo che traduce tutti gli oggetti della pagina.

Stay tuned...

Commenti
TrackBack scrive:
14/09/2005 ore 17.57

Aggiungi un nuovo commento »»»
Per inserire un commento, devi registrarti alla nostra community.

Nella stessa categoria
I più letti del mese
TagCloud
BLOG INFO
  • 130 post, 93 commenti, 20 trackback
  • Feed blog e contenuti tecnici: RSS
  • Feed blog: RSS Atom
IN EVIDENZA