Developer forum

Forum » Templates » Language menu
Thomas Schroll
Reply

Hi

I have setup a language menu on a solution. When the user clicks on another language they are redirected to the equivalent page on the other lanuage. It is working great, but I have a small problem when the page does not exist on the other language. Then dw will not output the language in the menu. I would like to have all languages in the loop and perhaps an empty PageID if the page does not exist.

Does anybody know a way to get all available lanuages without hard coding them, perhaps by using razor?

Regards Thomas


Replies

 
Mikkel Ricky
Reply

I assume that you're using the Languages loop, and under that assumption you have found a bug with that loop. We'll take a look at that.

It's a bit tricky to get all available languages using Razor, but it can be done like this (using a Razor helper):

@helper DisplayLanguages()
{
    // Find the master area
    var masterAreaId = Pageview.Area.ID;
    if ((int)Pageview.Area.get_Value("AreaMasterAreaId") > 0)
    {
        masterAreaId = (int)Pageview.Area.get_Value("AreaMasterAreaId");
    }

    // Get a list of Dynamicweb.Frontend.Area with the master area first and followed by child areas (sorted by AreaSort)
    var areas = new SortedDictionary< int, Dynamicweb.Frontend.Area >() { { 0, (Dynamicweb.Frontend.Area)Pageview.AreaCollection[masterAreaId] } };
    foreach (System.Collections.DictionaryEntry e in Pageview.AreaCollection)
    {
        var area = (Dynamicweb.Frontend.Area)e.Value;
        if ((int)(area).get_Value("AreaMasterAreaId") == masterAreaId)
        {
            areas.Add((int)area.get_Value("AreaSort"), area);
        }
    }

    if (areas.Count > 0)
    {
        <ol class="websites">
            @foreach (var e in areas)
            {
                var area = e.Value;
                if (area.ID == Pageview.Area.ID)
                {
                    <li class="current"><a href='/Default.aspx?Id=@area.get_Value("AreaFirstPage")'>@area.get_Value("AreaName") @area.ID</a></li>
                }
                else
                {
                    <li><a href='/Default.aspx?Id=@area.get_Value("AreaFirstPage")'>@area.get_Value("AreaName") @area.ID</a></li>
                }
            }
        </ol>
    }
}

Then you can use this helper anywhere in your template:

<div class="page">
  ...
  @DisplayLanguages()
  ...
</div>

We have to find an easier way to do this, but for now you can use the stuff shown above.

Best regards,
Mikkel

 
Thomas Schroll
Reply

Hi Mikkel

Thanks. I have a problem with AreaSort. For every language area the AreaSort value is 902. Do you know what I am doing wrong. Below is the code, where I also have inserted an is Active check and inserted a counting variable for the key instead of AreaSort.

I would also like to link to the corresponding page on the other areas. So I need the PageID not just the AreaFirstPage. Is that possible?

@helper DisplayLanguages(){

  // Find the master area
  var masterAreaId = Pageview.Area.ID;
  if ((int)Pageview.Area.get_Value("AreaMasterAreaId") > 0) {
    masterAreaId = (int)Pageview.Area.get_Value("AreaMasterAreaId");
  }
 
  // Get a list of Dynamicweb.Frontend.Area with the master area first and followed by child areas (sorted by AreaSort)
  var areas = new SortedDictionary< int, Dynamicweb.Frontend.Area >() { { 0, (Dynamicweb.Frontend.Area)Pageview.AreaCollection[masterAreaId] } };
    int i = 1;
    foreach (System.Collections.DictionaryEntry e in Pageview.AreaCollection) {
        var area = (Dynamicweb.Frontend.Area)e.Value;
        if ((int)(area).get_Value("AreaMasterAreaId") == masterAreaId && (bool)(area).get_Value("AreaActive")) {
            int Key = (int)area.get_Value("AreaSort");
            areas.Add(i, area);
            i++;
        }
    }
 
  if (areas.Count > 0) {
        <ul class="websites">
            @foreach (var e in areas) {
                var area = e.Value;
                if (area.ID == Pageview.Area.ID) {
                    <li class="current"><a href='/Default.aspx?Id=@area.get_Value("AreaFirstPage")'>@area.get_Value("AreaName")</a></li>
                } else {
                    <li><a href='/Default.aspx?Id=@area.get_Value("AreaFirstPage")'>@area.get_Value("AreaName")</a></li>
                }
            }
        </ul>
  }
} 

Regards Thomas

 
Mikkel Ricky
Reply

Hmm, that's weird … What are the values of AreaSort in the Database (on the Area table)?

We have added a new loop, WebsiteLanguages, for rendering a list of websites (language selector) in the latest release of Dynamicweb 8.4.0:

@foreach (var item in GetLoop("WebsiteLanguages")) {
    <pre>@string.Join("\n", item.Values)</pre>
}

Best regards,
Mikkel

 
Thomas Schroll
Reply

Hi Mikkel

The Master AreaSort has the value 901 and the sublanguages all have the values 902. I am running 8.4.0.10 and I have no "WebsiteLanguages" loop, only "Languages". In the release notes for 8.4.1 (http://developer.dynamicweb-cms.com/releases/dynamicweb-8-4-1.aspx) it says that the loop is added in the 8.4.1 version. Unfortunately I have to find a solution before the next release.

Regards Thomas

 
Mikkel Ricky
Reply

Hmm … That's weird. Maybe you should try to re-sort the sublanguages to update the sort key (AreaSort) on them …

 
Thomas Schroll
Reply

Hi Mikkel

That worked, thanks. 

Regards Thomas

 

You must be logged in to post in the forum