Language selector
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,
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
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,
Hello Daniel,
It should be a widget.
Kind regards,
Ivan Dimitrov
the Telerik team
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,
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
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;
/// <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;
PageSiteNode p = SiteMapBase.GetActualCurrentNode();
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;
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
Hello Daniel,
You should set the CurrrentUICulture to get the pages for the proper language.
Greetings,
Ivan Dimitrov
the Telerik team