How can I use the current language as a variable in my masterpage?
-
10-12-2019 - |
Question
I'm a web designer working with SharePoint 2013. I created a custom template for a company and now I want to go further with variations capabilities. The website should be available in German and in English.
On the footer of the master page, I placed some hard-coded links like this:
<a href="/Authenticate.aspx">Login</a>
<a href="/imprint">Imprint</a>
<a href="/disclaimer">Disclaimer</a>
And I want them to rebuild dynamically to my variations site like this for the German language:
<a href="/de/...">Login</a>
<a href="/de/...">Imprint</a>
<a href="/de/...">Disclaimer</a>
or this for English:
<a href="/en/...">Login</a>
<a href="/en/...">Imprint</a>
<a href="/en/...">Disclaimer</a>
Is there a variable that I could place in front of the links in my master page? Or a method to get the current language?
I was thinking of something like this:
<a href="{Locale}/login">...</a>
<a href="$language/imprint...">...</a>
<a href="getLanguage()/disclaimer">...</a>
I'm not an ASP.NET coder, so I don't really know how find out the language. I would appreciate if someone could help me.
Solution 2
I solved the problem with another method: I used SharePoints own Javascript variable - _spPageContextInfo - see: _spPageContextInfo is your new best friend !
With "currentCultureName" I could use the language name as a variable to compare to the target language and to expand my PageLayout with custom links, compliant to my variations settings.
The Code could look like this:
$(document).ready(function() {
$language = _spPageContextInfo.currentCultureName;
// If it's german, I'll get german links
if ($language == "de-DE") {
$('.footer').html('<a href="/de/kontakt/">Kontakt</a>');
} else {
// And if not, it will target to the english page
$('.footer').html('<a href="/en/contact">Contact</a>');
}
});
OTHER TIPS
You could use the SPWeb.Locale property of the current context (as in SPContext.Current.Web.Locale
) to glean this information.
If you look at the Remarks section of the CultureInfo Class description on MSDN it will explain this.
A simple example would be:
<a href="/<%= SPContext.Current.Web.Locale.TwoLetterISOLanguageName %>/Login.aspx">...</a>