Orchard Core Content Localization
Guidelines
- Enable
OrchardCore.ContentLocalization(depends on Content Types and
Localization) to manage localized versions of content items. Configure the
site cultures first (see orchardcore-localization).
- Attach
LocalizationPartto every content type that needs translations. Each
item stores its Culture; all translations share a LocalizationSet.
- Enable
OrchardCore.ContentLocalization.ContentCulturePicker(requires
Autoroute) for a front-end culture picker and URL-based culture detection.
- Enable
OrchardCore.ContentLocalization.Sitemapsfor localized sitemaps with
hreflang alternates.
- Use
switch_culture_urlandlocalization_setLiquid filters for custom
culture switchers and lookups.
- All recipe JSON must be wrapped in
{ "steps": [...] }. - All C# classes must use the
sealedmodifier, except View Models.
Enabling Content Localization Features
{
"steps": [
{
"name": "Feature",
"enable": [
"OrchardCore.ContentLocalization",
"OrchardCore.ContentLocalization.ContentCulturePicker"
]
}
]
}
Attaching LocalizationPart to a Content Type
using OrchardCore.ContentManagement.Metadata;
using OrchardCore.ContentManagement.Metadata.Settings;
using OrchardCore.Data.Migration;
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"
}
]
}
]
}
]
}
Localized content items in a content step share the same LocalizationSet:
{
"steps": [
{
"name": "content",
"data": [
{
"ContentItemId": "[js:uuid()]",
"ContentType": "Article",
"DisplayText": "About",
"Published": true,
"TitlePart": { "Title": "About" },
"LocalizationPart": { "LocalizationSet": "about-set", "Culture": "en-US" }
},
{
"ContentItemId": "[js:uuid()]",
"ContentType": "Article",
"DisplayText": "À propos",
"Published": true,
"TitlePart": { "Title": "À propos" },
"LocalizationPart": { "LocalizationSet": "about-set", "Culture": "fr-FR" }
}
]
}
]
}
Localizing Content in Code
IContentLocalizationManager clones an item into another culture and reads
localization sets:
using OrchardCore.ContentLocalization;
using OrchardCore.ContentManagement;
public sealed class TranslationService
{
private readonly IContentLocalizationManager _contentLocalizationManager;
public TranslationService(IContentLocalizationManager contentLocalizationManager)
{
_contentLocalizationManager = contentLocalizationManager;
}
public Task<ContentItem> CreateFrenchVersionAsync(ContentItem contentItem)
=> _contentLocalizationManager.LocalizeAsync(contentItem, "fr-FR");
public Task<ContentItem> FindAsync(string localizationSet, string culture)
=> _contentLocalizationManager.GetContentItemAsync(localizationSet, culture);
}
Other members: GetItemsForSetAsync(), GetItemsForSetsAsync(sets, culture),
DeduplicateContentItemsAsync(), and GetFirstItemIdForSetsAsync() (current
culture, then default culture, then the first item of each set). In Razor,
await Orchard.GetContentCultureAsync(contentItem) returns an item's culture.
Hook into localization with IContentLocalizationHandler or
ContentLocalizationPartHandlerBase<TPart>.
Permissions
OrchardCore.ContentLocalization.ContentLocalizationPermissions declares
LocalizeContent (localize content for others), LocalizeOwnContent, and
ManageContentCulturePicker (the settings pages). Administrators and Editors
get all three; Authors and Contributors get LocalizeOwnContent.
ContentCulturePicker
With the picker feature enabled:
ContentRequestCultureProvideris added as the first request culture
provider; it sets the culture from the content item matching the current URL.
- The
ContentCulturePickershape is added to the front-endNavbarwhen more
than one culture is supported. Its model exposes CurrentCulture and
SupportedCultures (CultureInfo). Override ContentCulturePicker.cshtml
in a theme to restyle it.
- The
RedirectToLocalizedContentroute redirects to the item's localization
for the selected culture, otherwise (when RedirectToHomepage is on) the
home page localization, otherwise back to the current page.
Settings live under Settings → Localization → Content Culture Picker
(ContentCulturePickerSettings) and Content Culture
(ContentRequestCultureProviderSettings):
{
"steps": [
{
"name": "settings",
"ContentCulturePickerSettings": {
"SetCookie": true,
"RedirectToHomepage": false
},
"ContentRequestCultureProviderSettings": {
"SetCookie": true
}
}
]
}
ContentCulturePickerSettings.SetCookie (default true) writes the
CookieRequestCultureProvider cookie when a culture is picked;
ContentRequestCultureProviderSettings.SetCookie writes it from the culture
of the content item matching the URL. The cookie (.AspNetCore.Culture) is
HttpOnly, and Secure when the request is served over HTTPS; client-side
scripts cannot read it, so read the current culture from the page (for example
the lang attribute of <html>).
Cookie Lifetime Configuration
{
"OrchardCore": {
"ContentLocalization": {
"CulturePickerOptions": {
"CookieLifeTime": 14
}
}
}
}
CookieLifeTime is in days (default 14) and can also be set per tenant under
OrchardCore:{TenantName}:ContentLocalization:CulturePickerOptions.
Liquid Filters
`switch_culture_url`
Returns the RedirectToLocalizedContent URL for a target culture and the
current path and query string:
{{ "fr" | switch_culture_url }}
/RedirectToLocalizedContent?targetCulture=fr&contentItemUrl=%2Fblog
`localization_set`
Returns the content item of a localization set in the given culture (the
request culture when omitted). An array of sets returns one item per set.
{% assign localizedItem = Model.ContentItem.Content.LocalizationPart.LocalizationSet | localization_set: "en" %}
{{ localizedItem.DisplayText }}
{% assign current = Model.ContentItem.Content.LocalizationPart.LocalizationSet | localization_set %}
Culture Switcher in Liquid
The Liquid Culture object lists the site cultures:
<ul class="culture-switcher">
{% for culture in Culture.SupportedCultures %}
<li class="{% if culture.Name == Culture.Name %}active{% endif %}">
<a href="{{ culture.Name | switch_culture_url }}">{{ culture.NativeName }}</a>
</li>
{% endfor %}
</ul>
Admin, Sitemaps, and GraphQL
- The contents admin list adds a culture filter (
culture:fr-FRin the search
box) and shows each item's culture and translations.
OrchardCore.ContentLocalization.Sitemapsreplaces the sitemap content items
query to emit localized URLs with alternates.
- GraphQL exposes
localizationPart { culture localizationSet localizations { contentItemId } }
on content types with the part.
Upgrading from 3.x
OrchardCore_ContentLocalization_CulturePickerOptions→
OrchardCore:ContentLocalization:CulturePickerOptions (the old name still
binds but is deprecated).
OrchardCore.ContentLocalization.Permissions.LocalizeContent,
LocalizeOwnContent, and ManageContentCulturePicker were removed; use the
same members of OrchardCore.ContentLocalization.ContentLocalizationPermissions.
- The culture cookie is now
HttpOnly(andSecureover HTTPS). Scripts that
read .AspNetCore.Culture through document.cookie must read the culture
from the rendered page instead.