How do you filter content in Umbraco based on language-specific property values?
For example, let's say the default language is en-US and we have a property called "slug". We have a US version of an article with the slug of "color" and we have localised this article for en-AU where the slug property is overridden to the Australian spelling of "colour".
However, this only seems to be searching property values in the primary language, so doesn't find the article. I only want the search to look at localised en-AU values.
Additionally, how do you only retrieve articles where they have been localised for Australia?
Firstly using XPath for querying Umbraco is sadly not the most efficient way anymore :-( - with the move to V8, the published cache is no longer stored as an XML file and is instead in a BTree in-memory solution that everyone calls nuCache.
The usual XPath queries have been remade, on top of nuCache, to help people migrate from V7, but they are not as performant as working directly with nuCache, and haven't been expanded to work with the new vary by culture language options of doc types.
There is a new Razor Cheatsheet that can be a useful gateway to some of the methods we can now use:
So if you the user were on the US version of the page, and you wanted to check if there was an Aussie version of the content you could use
@Model.HasCulture("en-AU")
Or if you had a reference to your Articles 'Section' page you could use
articleSectionPage.Descendents("en-AU")
To bring back all the content in the section only available in the Australian culture.
Finally, Umbraco, ships with Examine which is a wrapper around Lucene Search, so you can use this to query your site for keywords like 'colour' ... if you have not used Examine before then there is quite a bit to it:
But essentially for every property and every language variation the value is stored as a separate fields in the Lucene Index for the page, so you can create custom search queries, and target multiple or single language variation fields - so that your search term doesn't match by accident in a different language...
(Not sure if all this answers your question though!)
Thanks so much for the detailed response. That is all really helpful.
I am using a react front end so won't be using any of the razor functionality. So, I just have an API controller that queries Umbraco and returns JSON to the react component.
I have arrived at using UmbracoHelper.ContentAtRoot() and chaining the Linq queries to get the data that I need where I can specify the culture as you mentioned.
This seems to work quite well - do you think it is the right approach to use the UmbracoHelper like that?
The Examine search functionality looks interesting and powerful. There is quite a bit to it as you say.
It's probably fine! - But it's all about the context! - So if your website has thousands of pages, and millions of requests a day and your API Controller output isn't cached, you might find that Examine would be a better approach, particularly if you are searching for keywords in rich text properties.
The Umbraco Cache is pretty fast though these days, plenty of sites are built with this approach! (I try to keep things as simple as possible, can always refactor if there is an issue)
Retrieve Content For Specific Language
How do you filter content in Umbraco based on language-specific property values?
For example, let's say the default language is en-US and we have a property called "slug". We have a US version of an article with the slug of "color" and we have localised this article for en-AU where the slug property is overridden to the Australian spelling of "colour".
I have tried using the following:
UmbracoHelper.ContentSingleAtXPath("//article[slug='colour']")
However, this only seems to be searching property values in the primary language, so doesn't find the article. I only want the search to look at localised en-AU values.
Additionally, how do you only retrieve articles where they have been localised for Australia?
Hi Jason
Firstly using XPath for querying Umbraco is sadly not the most efficient way anymore :-( - with the move to V8, the published cache is no longer stored as an XML file and is instead in a BTree in-memory solution that everyone calls nuCache.
The usual XPath queries have been remade, on top of nuCache, to help people migrate from V7, but they are not as performant as working directly with nuCache, and haven't been expanded to work with the new vary by culture language options of doc types.
There is a new Razor Cheatsheet that can be a useful gateway to some of the methods we can now use:
https://docs.umbraco.com/umbraco-cms/fundamentals/design/templates/razor-cheatsheet
So if you the user were on the US version of the page, and you wanted to check if there was an Aussie version of the content you could use
@Model.HasCulture("en-AU")
Or if you had a reference to your Articles 'Section' page you could use
articleSectionPage.Descendents("en-AU")
To bring back all the content in the section only available in the Australian culture.
Finally, Umbraco, ships with Examine which is a wrapper around Lucene Search, so you can use this to query your site for keywords like 'colour' ... if you have not used Examine before then there is quite a bit to it:
https://docs.umbraco.com/umbraco-cms/reference/searching/examine/
But essentially for every property and every language variation the value is stored as a separate fields in the Lucene Index for the page, so you can create custom search queries, and target multiple or single language variation fields - so that your search term doesn't match by accident in a different language...
(Not sure if all this answers your question though!)
regards
Marc
Hi Marc,
Thanks so much for the detailed response. That is all really helpful.
I am using a react front end so won't be using any of the razor functionality. So, I just have an API controller that queries Umbraco and returns JSON to the react component.
I have arrived at using UmbracoHelper.ContentAtRoot() and chaining the Linq queries to get the data that I need where I can specify the culture as you mentioned.
This seems to work quite well - do you think it is the right approach to use the UmbracoHelper like that?
The Examine search functionality looks interesting and powerful. There is quite a bit to it as you say.
Thanks,
Jason
Hi Jason
It's probably fine! - But it's all about the context! - So if your website has thousands of pages, and millions of requests a day and your API Controller output isn't cached, you might find that Examine would be a better approach, particularly if you are searching for keywords in rich text properties.
The Umbraco Cache is pretty fast though these days, plenty of sites are built with this approach! (I try to keep things as simple as possible, can always refactor if there is an issue)
regards
Marc
is working on a reply...