Created
October 27, 2017 09:40
-
-
Save ps-team/b60dfeee593d102ca08249ece6597055 to your computer and use it in GitHub Desktop.
Localisation - using localisation in Contensis razorviews - In razor we can access the current pages' language folder and then use that to look-up all nodes in the CMS that are in that language. So for example, we can use this to build a custom search results razorview, which is language specific. Or we can build up a more specific search query …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@using System.Collections.ObjectModel | |
@using Contensis.Framework.Web | |
@using Contensis.Framework.Web.Search | |
@{ | |
@* | |
- assuming the site structure has been setup so that all content for each language | |
- is contained in a language folder that sits under the website root, we target the | |
- current pages' language folder and create a dynamic path variable which we tie into | |
- the search api to give us our language 'filter' | |
- | |
- looking at the search content table, it doesn't look like we can find all nodes | |
- by language directly, but this method should be pretty full proof (used on Newport) | |
*@ | |
// identify the language folder and create the language path variable | |
int homeId = CurrentNode.Parent.AncestorAtDepth(1).ID; | |
NodeFactory nf = new NodeFactory(); | |
string languagePath = ((FolderNode)nf.LoadById(homeId)).Path; | |
// build a query using the language path to restrict results to the current language | |
IQuery query = Query.Where("Property_Path").StartsWith(languagePath); | |
// get the nodes using the query | |
ReadOnlyCollection<ContentNode> nodes = new NodeFinder().Find(query); | |
// render the results (if there are any) | |
if(nodes.Count > 0) | |
{ | |
<div> | |
<ul> | |
@{ | |
foreach(ContentNode node in nodes) | |
{ | |
<li> | |
<h2> | |
<a href="@node.Path">@node.Title</a> | |
</h2> | |
<p>@node.Data.Description</p> | |
</li> | |
} | |
} | |
</ul> | |
</div> | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@using System.Collections.ObjectModel | |
@using Contensis.Framework.Web | |
@using Contensis.Framework.Web.Search | |
@{ | |
@* | |
- using the ID of the localised value, we can easily populate hardcoded text elements | |
- on pages/templates for all required languages without clutering the razorview with | |
- string variables set by a bunch of if or switch statements | |
- | |
- useful for things such as the text on a Search button or the Next/Previous text on | |
- pagination links etc etc | |
*@ | |
ILocalisation localisation = CurrentContext.Localisations; | |
// identify current language | |
int language = CurrentNode.Language; | |
// get and render localised value using its ID | |
<p>@localisation.Get(33, language)</p> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment