Developer forum

Forum » Templates » Language Switcher on Frontend

Language Switcher on Frontend

Casper Andersen
Reply

Hi there, i am fairly new to DynamicWeb so the way i am doing this might be totally wrong so sorry for that.

But basically what i want is to add a language switcher on my master template, i got the links to the 2 different home pages and it looks like this

Dynamicweb.Content.PageCollection languages = Dynamicweb.Content.Page.GetPagesBySql(String.Format("SELECT * FROM Page WHERE PageSort = {0}", 1));

This gets me the 2 home pages in a foreach loop that looks like this 

foreach (Dynamicweb.Content.Page language in languages)

but on top of that i would like to be able to add a flag on each of the masterpages, i added a file upload field in the masterpage section, but question is now, how do i access it and get hold of it ? i hear something about using PageView and i tried using this code i found in another forum post that looks like this

Dynamicweb.Frontend.PageView.GetPageviewByPageID(language.ID).get_Value("Thumbnail_Image).ToString();

but so far nothing, i can't get ANY value by using the get_Value method.

Hope someone can help me :)

Thanks in advanced!


Replies

 
Thomas Schroll
Reply

Hi Casper

It looks like a fairly complex setup. I don't know if my approach is exactly what you need, but here goes:

<ul class="nav lang-selector">
    @foreach (var language in GetLoop("Languages")) {
            string culture = language.GetString("Culture");
            if (!language.GetBoolean("IsCurrent")) {
                string languagePage = (language.GetBoolean("PageIsHidden")) ? language.GetString("FirstPageID") : language.GetString("PageID");
                <li class="flags @culture.Substring(3).ToLower()"><a href="/Default.aspx?ID=@languagePage" hreflang="@culture.Substring(0, 2)">@language.GetString("Name")</a></li>
            } else {
                <li class="flags @culture.Substring(3).ToLower() active">@language.GetString("Name")</li>
            }
        }
</ul>

I loop through all languages in GetLoop(Languages), check if the language is the current language, makes a link to non current languages if the page exist (otherwise to the first page in the area). Images are handled in css by putting a language class on the list element. You could of course do the same with an img element. This approach can only be used if you have a master layer with language layers.

Regards Thomas

 

 
Nicolai Høeg Pedersen
Reply

I agree with Thomas

Hes approach is better and does not rely on querying the database.

Nicolai

 
Casper Andersen
Reply

Thanks alot Thomas, this helped me alot and it solved my problem!

For some reason the way you wrote it did not work for the images, so what i did was i linked to the images in the admin section and the added the culture in capitals so now my code looks like this

<ul class="nav lang-selector pull-left">
                                        @foreach (var language in GetLoop("Languages"))
                                        {
                                            string culture = language.GetString("Culture");
                                            string languageImage = "/Admin/Images/Flags/flag_" + culture.Substring(3).ToLower() + ".png";
                                            if (!language.GetBoolean("IsCurrent"))
                                            {
                                                string languagePage = (language.GetBoolean("PageIsHidden")) ? language.GetString("FirstPageID") : language.GetString("PageID");
                                                <li class="pull-left flags @culture.Substring(3).ToUpper()"><a href="Default.aspx?ID=@languagePage" hreflang="@culture.Substring(3).ToUpper()"><img src="@languageImage" alt="" /></a></li>
                                            }

                                            else
                                            {
                                                string languagePage = (language.GetBoolean("PageIsHidden")) ? language.GetString("FirstPageID") : language.GetString("PageID");
                                                <li class="pull-left flags @culture.Substring(3).ToUpper() active"><a href="Default.aspx?ID=@languagePage" hreflang="@culture.Substring(3).ToUpper()"><img src="@languageImage" alt="" /></a></li>
                                            }
                                        }
                                    </ul>

 

But i do have one more question. Is it possible somehow to for an example if i am on the homepage of my site, to go to another page and get a paragraph or a tag from that page and display the information with DynamicWebs API ? And also how do i find out what i can use in my GetLoop like u used for the Language ?

Thanks alot for your help so far

 
Thomas Schroll
Reply

Hi Casper

It's great that it worked out for you.

You can use the API to get content from a paragraph, but as I understand it, it comes with a performance hit. It is rarely needed, and I think your best approach is to create another post for the specific issue and get the member's take on the particular problem.

You can use @IncludeFile("File") to get another template placed in the template. You can also use @RenderPageContent(PageID) to get static content from a page (e.g. a footer page) inserted into another page. I think you can do the same with @RenderParagraphContent(ParagraphID) just with paragraphs.

If you need to get available tags, you can use @TemplateTags(). TemplateTags() comes with a performance hit AND it outputs a text warning of the inserted TemplateTags() in frontend (even in comments). TemplateTags() outputs all available tags frontend. If you need all available tags in a loop, the syntax is:

@foreach(var loop in GetLoop("Loop"){

loop.TemplateTags()

}

Regards Thomas

 
Casper Andersen
Reply

Thomas u are a life saver! i was having some other issues, like my search only working in one language because the hidden field id was static and i did not know how to get the dynamic value of it and so on. But the TemplateTags was exactley the last piece of the puzzle that i needed. Thanks a million for all your help!

 
Thomas Schroll
Reply

You're welcome :)

Regards Thomas

 

You must be logged in to post in the forum