Query localized items
You can query items that have a localized field in multiple cultures. You can query all items from a specific culture, for example all items translated in French.
In the following example, you query all dynamic items that have a localized field with a value that applies to a specific culture. To do so:
- Get an instance of the
DynamicModuleManagerclass. - Resolve the name of the dynamic content type using the
TypeResolutionServiceclass. - Get the localized field name for the specified culture using the
LstringPropertyDescriptorclass.
For example, a localized name for a field, calledTitlein French culture isTitle_fr. - Filter the data items by passing the localized field name for the culture and select the
Masterversion using theContentLifecycleStatusclass.
C#
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Descriptors;
using Telerik.Sitefinity.DynamicModules;
using Telerik.Sitefinity.DynamicModules.Model;
using Telerik.Sitefinity.GenericContent.Model;
using Telerik.Sitefinity.Model;
using Telerik.Sitefinity.Utilities.TypeConverters;
namespace Telerik.Sitefinity.Documentation.CodeSnippets.DevGuide.SitefinityEssentials.Multilingual
{
public partial class MultilingualSnippets
{
public static IEnumerable<DynamicContent> LocalizedQuery(string typeName, string fieldName, string targetCulture)
{
DynamicModuleManager manager = DynamicModuleManager.GetManager();
Type dynamicType = TypeResolutionService.ResolveType(typeName);
CultureInfo c = new CultureInfo(targetCulture);
// transform the given culture for persistence as it may be different than the one in the UI
var persistenceCulture = new CultureInfo(AppSettings.CurrentSettings.GetCultureName(new CultureInfo(targetCulture)));
string fieldNameForCulture = LstringPropertyDescriptor.GetFieldNameForCulture(fieldName, persistenceCulture);
var result = manager.GetDataItems(dynamicType)
.Where(x => x.GetValue<string>(fieldNameForCulture) != null)
.Where(x => x.Status == ContentLifecycleStatus.Master)
.ToList();
return result;
}
}
}
As a result, you get a list of all items translated in the specified culture.
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.
Get started with Integration Hub | Sitefinity Cloud
This free lesson teaches administrators, marketers, and other business professionals how to use Sitefinity Integration Hub to create automated workflows between Sitefinity and other business systems.
Web Security for Sitefinity Administrators
This free lesson teaches administrators the basics about protecting your Sitefinity instance and your sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.
Foundations of Sitefinity ASP.NET Core Development
The free on-demand video course teaches developers how to use Sitefinity ASP.NET Core and take advantage of its decoupled architecture and modern development model.