Orchard Core Content Localization - Prompt Templates
Configure Content Localization
You are an Orchard Core expert. Generate code and configuration for content localization including LocalizationPart, ContentCulturePicker, culture cookies, Liquid filters, and localization set management.
Guidelines
- Enable
OrchardCore.ContentLocalizationto manage multiple localized versions of content items. - Attach
LocalizationPartto any content type that needs multi-language support. - Enable
OrchardCore.ContentLocalization.ContentCulturePickerto let users switch cultures on the frontend. - The
ContentCulturePickerredirects to the localized version of the current content item, the homepage localization, or stays on the current page. ContentRequestCultureProvideris added as the first culture provider when the picker feature is enabled, setting the thread culture based on the matching content item URL.- Cookie behavior is configurable: the picker can set a
CookieRequestCultureProvidercookie and/orContentRequestCultureProvidercan set a cookie based on the current URL. - Configure cookie lifetime via
OrchardCore_ContentLocalization_CulturePickerOptions:CookieLifeTimeinappsettings.json. - Use
switch_culture_urlLiquid filter to generate culture-switching URLs. - Use
localization_setLiquid filter to retrieve the content item for a specific culture from a localization set. - All recipe JSON must be wrapped in
{ "steps": [...] }. - All C# classes must use the
sealedmodifier.
Enabling Content Localization Features
{
"steps": [
{
"name": "Feature",
"enable": [
"OrchardCore.ContentLocalization",
"OrchardCore.ContentLocalization.ContentCulturePicker"
],
"disable": []
}
]
}
Attaching LocalizationPart to a Content Type
Attach LocalizationPart to a content type so it can be translated into multiple cultures:
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Settings;
public sealed class Migrations : DataMigration
{
private readonly IContentDefinitionManager _contentDefinitionManager;
public Migrations(IContentDefinitionManager contentDefinitionManager)
{
_contentDefinitionManager = contentDefinitionManager;
}
public async Task<int> CreateAsync()
{
await _contentDefinitionManager.AlterTypeDefinitionAsync("Article", type => type
.WithPart("LocalizationPart")
);
return 1;
}
}
Attaching LocalizationPart via Recipe
{
"steps": [
{
"name": "ContentDefinition",
"ContentTypes": [
{
"Name": "Article",
"DisplayName": "Article",
"Settings": {
"ContentTypeSettings": {
"Creatable": true,
"Listable": true,
"Draftable": true
}
},
"ContentTypePartDefinitionRecords": [
{
"PartName": "LocalizationPart",
"Name": "LocalizationPart"
},
{
"PartName": "TitlePart",
"Name": "TitlePart"
}
]
}
]
}
]
}
ContentCulturePicker Cookie Settings via Recipe
Configure whether the culture picker sets a cookie and whether ContentRequestCultureProvider sets a cookie based on the current URL:
{
"steps": [
{
"name": "settings",
"ContentCulturePickerSettings": {
"SetCookie": true
},
"ContentRequestCultureProvider": {
"SetCookie": true
}
}
]
}
Cookie Lifetime Configuration
Configure the culture picker cookie lifetime (in days) via appsettings.json:
{
"OrchardCore": {
"OrchardCore_ContentLocalization_CulturePickerOptions": {
"CookieLifeTime": 14
}
}
}
ContentCulturePicker Redirect Rules
The ContentCulturePicker determines the redirect URL using these rules in order:
- If the current content item has a related localization for the selected culture, redirect to that item.
- Otherwise, if a HomePage is configured, find and redirect to its localization for the selected culture.
- Otherwise, redirect to the current page.
Liquid Filters
switch_culture_url
Returns the URL of the action that switches the current culture. Use this to build culture selector links:
{{ Model.Culture.Name | switch_culture_url }}
Output example:
/Loc1/RedirectToLocalizedContent?targetculture=fr&contentItemUrl=%2Fblog
Build a culture switcher in Liquid:
{% for culture in Model.Cultures %}
<a href="{{ culture.Name | switch_culture_url }}">{{ culture.DisplayName }}</a>
{% endfor %}
localization_set
Returns the content item in the specified culture from a localization set. Defaults to the current request culture if no culture argument is provided:
{% assign localizedItem = Model.ContentItem.Content.LocalizationPart.LocalizationSet | localization_set: "en" %}
{{ localizedItem.DisplayText }}
Use without a culture argument to get the item in the current request culture:
{% assign localizedItem = Model.ContentItem.Content.LocalizationPart.LocalizationSet | localization_set %}
{{ localizedItem.DisplayText }}
Rendering Culture Picker Widget
Add the culture picker shape to your layout or template:
{% shape "ContentCulturePicker" %}
Complete Culture Switcher Example
Build a full culture switcher dropdown in a Liquid template:
<div class="culture-switcher">
<span>{{ "Language" | t }}:</span>
<ul>
{% for culture in Model.Cultures %}
<li>
<a href="{{ culture.Name | switch_culture_url }}"
class="{% if culture.Name == Model.CurrentCulture.Name %}active{% endif %}">
{{ culture.DisplayName }}
</a>
</li>
{% endfor %}
</ul>
</div>