Language selector

Posted by Community Admin on 03-Aug-2018 12:11

Language selector

All Replies

Posted by Community Admin on 30-Jan-2011 00:00

Hi all,

I'm curious how to start with this:

I want to use the Language Selector widget inside my template. I do not want to use the default layout, but for each language I want to show an image (which is the country flag).

I'm sure this would be possible, but what is the best practice in this?

How can I create a custom template for this, and what should be in it?

Regards,

Daniel

Posted by Community Admin on 31-Jan-2011 00:00

Hi Daniel,

This is possible with a custom control where you can set the flag inside ItemDataBound or ItemCreated events of the repeater. Please check this post.

Regards,
Ivan Dimitrov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 31-Jan-2011 00:00

Hi Ivan,

Should I set this up as a module, like e.g. the NewsRotator or Jobs modules, or as a widget?
Which example can I take as lead?

Regards,

Daniel

Posted by Community Admin on 31-Jan-2011 00:00

Hello Daniel,

It should be a widget.

Kind regards,
Ivan Dimitrov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 31-Jan-2011 00:00

Hi Ivan,

Okay, I have managed to:

- Create a new widget with a repeater that shows the languages
- For each language a flag is shown instead of text

The only thing now is how to concat the right URL's for the links?
If I click on a language, what should happen? Can I get these URL's from somewhere?

Regards,

Daniel

Posted by Community Admin on 03-Feb-2011 00:00

Hi Daniel,

Create an instance of ObjectFactory.Resolve<UrlLocalizationService>();. Then you can use ResolvePageUrl method - Returns the URL for the specified language version of the specified page, using the current url strategy. This method will work for pages in SPLIT mode - it will return correct URL for the desired language no matter which of the different language nodes you specify as an argument.

Best wishes,
Ivan Dimitrov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 03-Feb-2011 00:00

Hi Ivan,

Thanks for the help.
I've set up this code in my ItemDataBound of the Repeater:

/// <summary>
        /// Fires on ItemDataBound
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void languagesRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        
            try
            
                if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
                
                    CultureInfo item = e.Item.DataItem as CultureInfo;
                    Image imgLanguage = e.Item.FindControl("imgLanguage") as Image;
                    HyperLink lnkLanguage = e.Item.FindControl("lnkLanguage") as HyperLink;
 
                    // Set image properties
                    imgLanguage.ImageUrl = "~/Assets/Img/Lang/" + item.TwoLetterISOLanguageName + ".jpg";
                    imgLanguage.ToolTip = item.DisplayName;
 
                    // Set hyperlink properties
                    UrlLocalizationService s = ObjectFactory.Resolve<UrlLocalizationService>();
                    lnkLanguage.NavigateUrl = s.ResolvePageUrl(GetCurrentPage(), item);
                
            
            catch (Exception ex)
            
                throw ex;
            
        

The function 'GetCurrentPage' returns NULL, so there is no current node. How can I achieve this?

/// <summary>
/// Get the current pagenode
/// </summary>
/// <returns></returns>
private PageNode GetCurrentPage()
   try
   
      SiteMapNode currentNode = SiteMapBase.GetCurrentProvider().CurrentNode();
      PageSiteNode node = (PageSiteNode)currentNode;
      return App.WorkWith().Pages().Where(p => p.UrlName == node.UrlName).Get().FirstOrDefault();
   
   catch (Exception ex)
   
      throw ex;
   

Also, if I use:

PageSiteNode p = SiteMapBase.GetActualCurrentNode();

... it returns NULL.

What can this be?

Thanks!
Daniel


Posted by Community Admin on 03-Feb-2011 00:00

Hello Daniel,

Try using this code

var actualSitemapNode = SiteMapBase.GetActualCurrentNode();
 
 PageManager pm = PageManager.GetManager();
 
 var homePageId = Config.Get<PagesConfig>().HomePageId;
 IEnumerable<CultureInfo> availableLanguages = null;
 
 Guid nodeId;
 if (actualSitemapNode != null)
 
     nodeId = actualSitemapNode.Id;
 
 else
 
     nodeId = homePageId;
 
 
 
 this.node = pm.GetPageNode(nodeId);
 
 if (actualSitemapNode != null)
 
     availableLanguages = actualSitemapNode.AvailableLanguages;
 
 else
 
     availableLanguages = this.node.AvailableCultures;
 

The node should exist on the current language.

Kind regards,
Ivan Dimitrov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 03-Feb-2011 00:00

Thanks Ivan, that worked!
Is this all I have to do to select another language? I also have seen some code examples where the CultureInfo was set on the current thread?

Regards,
Daniel

Posted by Community Admin on 04-Feb-2011 00:00

Hello Daniel,

You should set the CurrrentUICulture to get the pages for the proper language.

Greetings,
Ivan Dimitrov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed