Language Selector in ASPX page

Sandro Rizzetto asked on February 19, 2018 12:22

Do you have any sample how to create a language selector using the languagedatasource and a basic repeater in a ASPX template (master page)?

This sample is for the portal engine https://docs.kentico.com/k11/multilingual-websites/setting-up-multilingual-websites/configuring-urls-for-multilingual-websites/creating-language-selectors

but i'm very confused, general speaking, how to migrate in the ASPX template world. CultureName, URL & CultureCode are not part of the datasource, so I cannot use in the Eval("") form like a normal repeater.

Thanks Sandro

Recent Answers


Dragoljub Ilic answered on February 19, 2018 14:48

Hi Sandro,

cms:LanguageDataSource will return objects of 'CMS.DocumentEngine.DocumentCultureUrl' type. That means that all columns that you mention will be in basic repeater, because they are in datasource. Can you check:

  1. Do you get correct values from datasource. It could be empty?
  2. Did you connected LanguageDataSource and BasicRepeater properly? Maybe you get correct values from data source, but your repeater did not render them at all. You can test this by changing transformation to have some dummy text right before (or after) <%# Eval("CultureCode") %> in basic repeater.

If you want to check do have any available cultures, you can call this on master page: <%# DocumentURLProvider.GetDocumentCultureUrls(CurrentDocument, URLHelper.RemoveQuery(RequestContext.CurrentURL), "", UrlOptionsEnum.UseCultureSpecificURLs).Count %> This peace of code will return number of available cultures. If you get 0, it means that datasource is empty.

Best Regards, Dragoljub

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on February 19, 2018 17:44

Kentico comes with web site examples. You need "Dancing Goat" Web site example. Master page already contains Language Data source and repeater.

0 votesVote for this answer Mark as a Correct answer

Sandro Rizzetto answered on February 21, 2018 15:08

@Peter: Dancing Goat is in Portal Engine, I was looking one in ASPX template. Thx anyway

@Dragonljub: your code snippet returns me "3" so languages are ok (btw it miss a DocumentContext before CurrentDocument)

For sure I don't understood how to bind correctly the 2 components:

<cms:LanguageDataSource ID="dsLang" runat="server" />

<cms:BasicRepeater ID="repLang" runat="server">
    <ItemTemplate>X</ItemTemplate>
</cms:BasicRepeater>

in code behind
repLang.DataSource = dsLang;
repLang.DataBind();
Response.Write(repLang.Items.Count);    // returns 1 and it is of type LanguageDataSource and not DocumentCultureUrl

Thanks

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on February 21, 2018 15:36

Sandro, what i would check is the configuration of the Language Data Source, it has parameters such as only show the languages that the current page is configured in (ExcludeUntranslatedDocuments), vs. show all languages that the site has.

The returned object will have a CultureCode, CultureName, and URL field.

Do either of those fields show up if you call them from the ItemTemplate?

0 votesVote for this answer Mark as a Correct answer

Sandro Rizzetto answered on February 21, 2018 16:17 (last edited on February 21, 2018 16:18)

I added ExcludeUntranslatedDocuments <cms:LanguageDataSource ID="dsLang" runat="server" ExcludeUntranslatedDocuments="false" /> putting

    <cms:BasicRepeater ID="repLang" runat="server" OnItemDataBound="repLang_ItemDataBound">
    <ItemTemplate><%# Eval("Url") %></ItemTemplate>
</cms:BasicRepeater>

shows this error DataBinding: 'CMS.DocumentEngine.Web.UI.LanguageDataSource' does not contain a property with the name 'Url'.

because bound to repeater it is an object of that type, and not DocumentCultureUrl

0 votesVote for this answer Mark as a Correct answer

Yoki Satria answered on April 18, 2018 06:29 (last edited on April 18, 2018 06:39)

What you did is binding the control itself to the repeater by doing this

repLang.DataSource = dsLang;

do this instead

repLang.DataSource = dsLang.DataSource;
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.