docs/en/multi-lingual-entities.md
//[doc-seo]
{
"Description": "Use ABP multi-lingual objects to select translations by culture, apply fallback rules, and process translations in bulk."
}
The
Volo.Abp.MultiLingualObjectpackage is not published on NuGet. It was removed from the release pipeline in #8271 and re-designing this feature is tracked in #11698. The source code is still maintained in the framework repository, so you can copy the Volo.Abp.MultiLingualObjects project into your solution if you want to use the pattern described below.
The Volo.Abp.MultiLingualObjects project provides a contract and a selection service for objects that store one translation per language. Persistence mapping is application-specific; it does not create a database relationship for the translations.
Copy the Volo.Abp.MultiLingualObjects project into your solution and reference it from the project that defines the consuming module.
Add AbpMultiLingualObjectsModule as a dependency of that module:
using Volo.Abp.Modularity;
using Volo.Abp.MultiLingualObjects;
[DependsOn(typeof(AbpMultiLingualObjectsModule))]
public class MyApplicationModule : AbpModule
{
}
Implement IMultiLingualObject<TTranslation> on the object and IObjectTranslation on its translation type:
using System.Collections.Generic;
using Volo.Abp.MultiLingualObjects;
public class Product : IMultiLingualObject<ProductTranslation>
{
public ICollection<ProductTranslation> Translations { get; set; } =
new List<ProductTranslation>();
}
public class ProductTranslation : IObjectTranslation
{
public string Language { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
}
Language stores a culture name such as en, en-US or tr.
Inject IMultiLingualObjectManager and call GetTranslationAsync:
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.MultiLingualObjects;
public class ProductService
: ITransientDependency
{
private readonly IMultiLingualObjectManager _multiLingualObjectManager;
public ProductService(
IMultiLingualObjectManager multiLingualObjectManager)
{
_multiLingualObjectManager = multiLingualObjectManager;
}
public Task<ProductTranslation?> GetTranslationAsync(Product product)
{
return _multiLingualObjectManager
.GetTranslationAsync<Product, ProductTranslation>(product);
}
}
With the default arguments, the manager uses CultureInfo.CurrentUICulture.Name. It selects translations in this order:
fallbackToParentCultures is true.LocalizationSettingNames.DefaultLanguage.The method returns null when the collection is null or empty. To disable only the parent-culture fallback, set fallbackToParentCultures to false. The default-language and first-available fallbacks still apply.
Use GetBulkTranslationsAsync to select translations for multiple objects with the same culture and fallback settings:
var results = await _multiLingualObjectManager
.GetBulkTranslationsAsync<Product, ProductTranslation>(products);
foreach (var (product, translation) in results)
{
// Use product and its selected translation.
}
Each result keeps the source object together with its selected translation. An object with no translations gets a null translation.