Localizing departments

You can localize a department by specifying a culture-specific value for each of the localizable properties of the Department class.

You can localize the following properties of a department:

  • Title
  • Description

When managing localized content, you must set the CurrentUICulture of the current thread to the specified culture. When you are finished managing the content, revert the current thread UI culture to its original value. This is done in the following way:

public static void ManageLocalizedDepartment(string culture)
{
    var cultureInfo = new CultureInfo(culture);
    var currentCulture = Thread.CurrentThread.CurrentUICulture;
    Thread.CurrentThread.CurrentUICulture = cultureInfo;
    
    //Method logic
 
    Thread.CurrentThread.CurrentUICulture = currentCulture;
}

Creating localized department

To create a localized department, you must perform the following:

  1. Set the current UI culture.
    Set the CurrentUICulture property of the current thread to the specified culture.
  2. Create the department.
    Create the department following the steps for creating a monolingual department. When setting the localized properties, use the CultureInfo object as a key to access the value of the LString for the specified language.
  3. Undo the current UI culture.
    Set the current UI culture to its previous value.

Here is a code example:

public static void CreateLocalizedDepartment(string name, string description, string parent, string culture)
{
    var cultureInfo = new CultureInfo(culture);
    var currentCulture = Thread.CurrentThread.CurrentUICulture;
    Thread.CurrentThread.CurrentUICulture = cultureInfo;
 
    TaxonomyManager taxonomyManager = TaxonomyManager.GetManager();
 
    HierarchicalTaxonomy departments = taxonomyManager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name == "Departments").SingleOrDefault();
 
    if (departments != null)
    {
        HierarchicalTaxon department = taxonomyManager.CreateTaxon<HierarchicalTaxon>();
        department.Name = Regex.Replace(name.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
 
        department.Title[cultureInfo] = name;
        department.Description[cultureInfo] = description;
 
        department.UrlName = department.Name;
 
        department.Taxonomy = departments;
 
        if (!String.IsNullOrEmpty(parent))
        {
            HierarchicalTaxon parentDepartment = departments.Taxa.Where(t => t.Title == parent).SingleOrDefault() as HierarchicalTaxon;
 
            if (parentDepartment != null)
            {
                department.Parent = parentDepartment;
                parentDepartment.Subtaxa.Add(department);
            }
        }
 
        taxonomyManager.SaveChanges();
    }
 
    Thread.CurrentThread.CurrentUICulture = currentCulture;
}

Adding translation

To add values for the other languages to the localized properties, you must perform the following:

  1. Set the current UI culture.
    Set the CurrentUICulture property of the current thread to the specified culture.
  2. Modify the department.
    To add the values for the specified language, modify the department and update the LString properties using the specified language as key.
  3. Undo the current UI culture.
    Set the current UI culture to its previous value.

Here is a code example:

public static void TranslateDepartment(Guid departmentId, string title, string description, string culture)
{
    var cultureInfo = new CultureInfo(culture);
    var currentCulture = Thread.CurrentThread.CurrentUICulture;
    Thread.CurrentThread.CurrentUICulture = cultureInfo;
 
    TaxonomyManager taxonomyManager = TaxonomyManager.GetManager();
 
    HierarchicalTaxonomy departments = taxonomyManager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name == "Departments").SingleOrDefault();
 
    if (departments != null)
    {
        HierarchicalTaxon department = departments.Taxa.Where(d => d.Id == departmentId).SingleOrDefault() as HierarchicalTaxon;
 
        if (department != null)
        {
            department.Title[cultureInfo] = title;
            department.Description[cultureInfo] = description;
 
            taxonomyManager.SaveChanges();
        }
    }
 
    Thread.CurrentThread.CurrentUICulture = currentCulture;
}

Querying localized departments

The Querying departments article shows how to query departments by ID, name and title. When working localized departments, the most suitable way is to query them either by the value of their ID property, or by the value of their Name property. Note that both properties are not localized. Don’t query content items by properties of type LString.

Modifying localized departments

If you are not updating any of the LString properties, you can follow the same steps as when modifying a monolingual department. If you want to update the values of the localized properties, follow the steps in the "Translating department" section in this topic.

Deleting localized departments

To delete a localized department you must follow the same steps as when deleting a monolingual department.

To delete a translation of a localized department, you must perform the same as when deleting the whole localized department, but as addition you have to pass the culture of the specified translation to the DeleteItem method.

Here is a code example:

public static void DeleteDepartmentTranslation(Guid departmentId, string language)
{
    TaxonomyManager taxonomyManager = TaxonomyManager.GetManager();
 
    HierarchicalTaxonomy departments = taxonomyManager.GetTaxonomies<HierarchicalTaxonomy>().Where(t => t.Name == "Departments").Single();
 
    if (departments != null)
    {
        HierarchicalTaxon department = departments.Taxa.Where(t => t.Id == departmentId).SingleOrDefault() as HierarchicalTaxon;
 
        if (department != null)
        {
            taxonomyManager.DeleteItem(department, new CultureInfo(language));
        }
    }
 
    taxonomyManager.SaveChanges();
}

Next steps

+1-888-365-2779
sales@sitefinity.com

Related topics:

Feedback

How useful is this article?

Tell us more

Submit
Your message was successfully sent.

We appreciate your feedback.

Your message could not be sent.

OK