Setting the Culture in PublishedRequest.Prepared shouldn't it give me IPublishedContent in that culture? I've set svas the default language but also enabled en-US and published the pages in both languages.
public class PublishedRequestComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.Components().Append<PublishedRequestComponent>();
}
}
To keep it simple I've hard coded the culture here.
public class PublishedRequestComponent : IComponent
{
public void Initialize()
{
PublishedRequest.Prepared += PublishedRequest_Prepared;
}
public void Terminate()
{
}
private void PublishedRequest_Prepared(object sender, System.EventArgs e)
{
var request = sender as PublishedRequest;
request.Culture = new System.Globalization.CultureInfo("en-US");
}
}
Ok, I've investigated it a bit further and it seems to work as expected if I render a page, but I'm trying to do this in an UmbracoApiController. Is it possible to set the "published request culture" there?
The injected Model on pages contains property values based on the current language (based on current domain). So, when you output @Model.Name, you can see that the name is displayed in en-US culture.
But, your code to get the names of the roots children is not sensitive for cultures. You will get all children, even when there is no specific content for specific languages.
The code above works like a charm for getting the name in the specified culture, but it only solves the first part of what I'm trying to do. I also need to access properties defined by me in that culture, is it possible to get a complete Models Builder model in the culture? Or do I need to get them one by one using
.Value("propName", "culture")
(It works but a translated model would be prettier IMO)
var items = Umbraco.ContentAtRoot()
.FirstOrDefault(x => x.IsPublished())
?.Children
.Where(x => x.Cultures.ContainsKey(currentCulture))
.Select(x => new Item
{
Id = x.Id,
Name = x.GetCulture(currentCulture).Name,
Heading = x.Value<string>(nameof(Standard.Heading), currentCulture)
});
Have you ever found a solution for this? I am currently running into the same problem with an api we have. It needs to be multilingual but I don't seem to find a way to do that using the model builder models :(
After digging into the source of Umbraco, I found how they define which language to get the content for. They do this by creating a new VariantionContext.
F.E.
```
public class ContentApiController : UmbracoApiController
{
private readonly IVariationContextAccessor _variationContextAccessor;
public ContentApiController(IVariationContextAccessor variationContextAccessor)
{
_variationContextAccessor = variationContextAccessor;
}
public IHttpActionResult Get(int id, string culture)
{
_variationContextAccessor.VariationContext = new VariationContext(culture);
var cnt = Umbraco.Content(id);
return Ok(cnt.Name);
}
}
This will return the data in the language for which the culture is passed.
But isn't the point of the culture parameter of the .Children and .Descendents extention that you can get all children/descendents in a specific culture when also specificering the type?
_variationContextAccessor.VariationContext = new VariationContext(culture);
I was digging around how to properly set the culture on AJAX requests but gave up the other day.
This seems to do it perfectly. Before, I was passing the culture to each property on partials returned from AJAX requests, but this became tedious and felt wrong when it wasn't necessary for normal requests. Setting the VariationContext in the AJAX controller seems to take care of it.
Set culture in published content request
Setting the Culture in PublishedRequest.Prepared shouldn't it give me IPublishedContent in that culture? I've set
sv
as the default language but also enableden-US
and published the pages in both languages.To keep it simple I've hard coded the culture here.
Get the translated names of the pages...
But all I get is the names in
sv
...Ok, I've investigated it a bit further and it seems to work as expected if I render a page, but I'm trying to do this in an UmbracoApiController. Is it possible to set the "published request culture" there?
Tried to set the thread culture, no difference, the returned variants are
sv
Thats the expected behaviour in Umbraco 8.
The injected Model on pages contains property values based on the current language (based on current domain). So, when you output @Model.Name, you can see that the name is displayed in en-US culture.
But, your code to get the names of the roots children is not sensitive for cultures. You will get all children, even when there is no specific content for specific languages.
The 'Name' property contains always the default language value. Instead you can use the following code:
When you want to list only the children that are available in the current culture, you can use:
The code above works like a charm for getting the name in the specified culture, but it only solves the first part of what I'm trying to do. I also need to access properties defined by me in that culture, is it possible to get a complete Models Builder model in the culture? Or do I need to get them one by one using
(It works but a translated model would be prettier IMO)
Have you ever found a solution for this? I am currently running into the same problem with an api we have. It needs to be multilingual but I don't seem to find a way to do that using the model builder models :(
After digging into the source of Umbraco, I found how they define which language to get the content for. They do this by creating a new VariantionContext.
F.E. ``` public class ContentApiController : UmbracoApiController { private readonly IVariationContextAccessor _variationContextAccessor;
This will return the data in the language for which the culture is passed.
This works really well!
But isn't the point of the culture parameter of the .Children and .Descendents extention that you can get all children/descendents in a specific culture when also specificering the type?
At least this doesn't seem to work when doing async controller requests.
Wow, THIS!
_variationContextAccessor.VariationContext = new VariationContext(culture);
I was digging around how to properly set the culture on AJAX requests but gave up the other day.
This seems to do it perfectly. Before, I was passing the culture to each property on partials returned from AJAX requests, but this became tedious and felt wrong when it wasn't necessary for normal requests. Setting the
VariationContext
in the AJAX controller seems to take care of it.EDIT: Now that I knew what to look for, I also found this on the official documentation: https://our.umbraco.com/documentation/Reference/Language-Variation/
is working on a reply...