Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > multilingual website with DropDownList selection View modes: 
User avatar
Member
Member
vjmdev - 8/11/2010 10:43:41 AM
   
multilingual website with DropDownList selection
Hi, I'd like to have a dropdown control instead of the built-in "language-selection" control in order to allow users to switch language.

I cloned the "languageselection" webpart available in kentico and used a DropDownList control instead of the Literal control and I'm able to get the cultures value in the dropdown

I added OnSelectedIndexChanged event in order to redirect to the translated website selected, although I'm having some issue creating the url.

Any suggestion?

Thanks

User avatar
Kentico Developer
Kentico Developer
kentico_ivanat - 8/13/2010 3:39:47 AM
   
RE:multilingual website with DropDownList selection
Hi,

You can create simple language dropdown list following way:

- to .ascx file add this control

<asp:DropDownList OnSelectedIndexChanged="language_change" ID="DropDownList1"
runat="server" style="height: 22px" AutoPostBack="true"/>


- to .ascx.cs add this method


protected void language_change(object sender, EventArgs e)
{
UrlHelper.Redirect(DropDownList1.SelectedValue);
}


- in protected void SetupControl() method replace only "if" part of condition
if (!DataHelper.DataSourceIsEmpty(ds) && (ds.Tables[0].Rows.Count > 1))
with following code (please leave else part as it is):

if (!DataHelper.DataSourceIsEmpty(ds) && (ds.Tables[0].Rows.Count > 1))
{
// Build current URL
string url = UrlHelper.CurrentURL;
url = UrlHelper.RemoveParameterFromUrl(url, UrlHelper.LanguageParameterName);
url = UrlHelper.RemoveParameterFromUrl(url, UrlHelper.AliasPathParameterName);


string ltlHyperlinks = "";
int count = 0;
int rows = ds.Tables[0].Rows.Count;


foreach (DataRow dr in ds.Tables[0].Rows)
{
string cultureCode = dr["CultureCode"].ToString();
string cultureShortName = dr["CultureShortName"].ToString();
if (!((HideCurrentCulture) && (String.Compare(CMSContext.CurrentDocument.DocumentCulture, cultureCode, true) == 0)))
{
// Add flag icon before the link text
ltlHyperlinks = HTMLHelper.HTMLEncode(UrlHelper.AddParameterToUrl(url, UrlHelper.LanguageParameterName, cultureCode));

// Add language to dropdown list
DropDownList1.Items.Add(new ListItem(HTMLHelper.HTMLEncode(cultureShortName), ltlHyperlinks));

// Set surrounding div css class
selectionClass = "languageSelectionWithCultures";
}
count++;
}
// Set item in DropDownList1 to Selected
DropDownList1.Items.FindByValue(UrlHelper.AddParameterToUrl(url, UrlHelper.LanguageParameterName, CookieHelper.GetValue("CMSPreferredCulture"))).Selected = true;
}




Best regards,
Ivana Tomanickova

User avatar
Member
Member
vjmdev - 8/13/2010 10:19:38 AM
   
RE:multilingual website with DropDownList selection
Great!!!

Thanks a lot for your help!!