Orchardcore content localization

From CrestApps/CrestApps.AgentSkills · View interactive version

Orchardcore content localization is an AI agent skill from the GitHub repository CrestApps/CrestApps.AgentSkills. Skill for configuring content localization in Orchard Core. Covers LocalizationPart for multi-language content, IContentLocalizationManager, the ContentCulturePicker feature and shape, ContentRequestCultureProvider, culture cookie settings, the ContentLocalization CulturePickerOptions configuration

How to use this skill

Skill file

Orchard Core Content Localization

Guidelines

Localization) to manage localized versions of content items. Configure the

site cultures first (see orchardcore-localization).

item stores its Culture; all translations share a LocalizationSet.

Autoroute) for a front-end culture picker and URL-based culture detection.

hreflang alternates.

culture switchers and lookups.

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:

provider; it sets the culture from the content item matching the current URL.

than one culture is supported. Its model exposes CurrentCulture and

SupportedCultures (CultureInfo). Override ContentCulturePicker.cshtml

in a theme to restyle it.

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

box) and shows each item's culture and translations.

query to emit localized URLs with alternates.

on content types with the part.

Upgrading from 3.x

OrchardCore:ContentLocalization:CulturePickerOptions (the old name still

binds but is deprecated).

LocalizeOwnContent, and ManageContentCulturePicker were removed; use the

same members of OrchardCore.ContentLocalization.ContentLocalizationPermissions.

read .AspNetCore.Culture through document.cookie must read the culture

from the rendered page instead.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.