Multilingual Support
The AI Context module supports content translation for ai_context_item
entities. When AI consumers select context items, they receive content in
the current content language when a translation exists.
How it works
Translation detection
Translation support is primarily handled in two services:
- AiContextSelector -- detects the current content language and selects translated entities in a single pass before filtering/scoring
- AiContextRenderer -- renders the already-translated entities
AiContextSelector uses AiContextLanguageDetectorTrait to detect the
current content language. Detection order is:
- Request path -- checks for a language prefix (for example
/fr/) - Referer header -- checks the HTTP Referer for a language prefix, which covers AJAX calls from language-prefixed pages
- Drupal language negotiation -- falls back to
getCurrentLanguage(TYPE_CONTENT)
Why not just getCurrentLanguage()?
Drupal's language negotiation relies on the request URL. AJAX-based tools
can POST to an unprefixed path even when the page the user is viewing is
language-prefixed. In that case, getCurrentLanguage() can return the
default language unless the Referer is also checked.
Fallback behavior
When a translation is not available for the current language, the system falls back to the default language version of the content. This ensures that context remains available even when translations are incomplete.
Compatibility
The behavior works in all of these cases:
- content translation is enabled and translations exist
- content translation is enabled but translations do not exist
- content translation is not enabled
- the language module is not enabled
Setting up translations
Enabling translation
- Enable the Language and Content Translation modules
- Add additional languages at
/admin/config/regional/language - Enable translation for AI Context Items at
/admin/config/regional/content-language - Check "AI Context Item" and select which fields should be translatable
- Save the configuration
Adding translations
- Go to
/admin/ai/context/items - Click Edit on a context item
- Click the Translate tab
- Click Add for the language you want to translate to
- Enter the translated content
- Save the translation
Usage example
$selector = \Drupal::service('ai_context.selector');
$request_factory = \Drupal::service('ai_context.request_factory');
$request = $request_factory->fromAgent('my_agent', 'database operations');
$result = $selector->select($request);
$context_text = $result->getRenderedText();
The selector returns translated content when the current request or Referer is language-prefixed. If a translation is missing, the default-language entity is used instead.
Technical details
Safe translation checks
The implementation uses safe translation checks that work even when translation is not enabled:
if ($item->hasTranslation($currentLanguage)) {
$item = $item->getTranslation($currentLanguage);
}
Performance
- translation loading happens in the same entity-loading pass
- no extra translation work happens in the renderer
- language detection happens once per request
Migration notes
No migration is required. Translation support is automatically available once you:
- Update the module code
- Clear caches (
drush cr) - Enable content translation for
ai_context_itementities (optional) - Add translations to your context items (optional)
For troubleshooting, see Multilingual troubleshooting.