Localizing products

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

You can localize the following properties of a product:

  • Title
  • Description
  • UrlName

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 ManageLocalizedProduct(string culture)
{
var cultureInfo = new CultureInfo(culture);
var currentCulture = Thread.CurrentThread.CurrentUICulture;
Thread.CurrentThread.CurrentUICulture = cultureInfo;
 
//Method logic
 
Thread.CurrentThread.CurrentUICulture = currentCulture;
}

Creating localized product

To create a localized product, 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 product.
    Create the product following the steps for creating a monolingual product. When setting the localizable properties, use the CultureInfo object as a key to access the value of the LString properties 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 CreateLocalizedProduct(string productTypeName, string title, string description, string sku,
                                    double? weight, bool isShippable,
                                    decimal price, DateTime saleEndDate,
                                    DateTime saleStartDate, decimal salePrice, bool isActive, string culture)
{
    var cultureInfo = new CultureInfo(culture);
    var currentCulture = Thread.CurrentThread.CurrentUICulture;
    Thread.CurrentThread.CurrentUICulture = cultureInfo;
 
    CatalogManager manager = CatalogManager.GetManager();
    if (manager.GetProducts().Where(x => x.Title == title).SingleOrDefault() != null)
    {
        return;     // Product already exists
    }
 
    productTypeName = productTypeName.ToLower();
 
    ProductType productType = manager.GetProductTypes().Where(x => x.Title.ToLower() == productTypeName).SingleOrDefault();
    if (productType == null)
    {
        return;     // Product Type does not exist
    }
 
    Product product = manager.CreateProduct(productType.ClrType);
    product.ClrType = productType.ClrType;
 
    product.Title[cultureInfo] = title;
    product.Description[cultureInfo] = description;
    product.UrlName[cultureInfo] = Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
 
    product.AssociateBuyerWithRole = Guid.Empty;
    product.DateCreated = DateTime.Now;
    product.IsShippable = isShippable;
    product.Price = price;
    product.IsActive = isActive;
    product.SaleEndDate = saleEndDate;
    product.SaleStartDate = saleStartDate;
    product.SalePrice = salePrice;
    product.Sku = sku;
    product.Visible = true;
    product.Weight = weight;
 
    product.Status = GenericContent.Model.ContentLifecycleStatus.Master;
 
    manager.Provider.RecompileItemUrls(product);
    manager.SaveChanges();
 
    var contextBag = new Dictionary<string, string>();
    contextBag.Add("ContentType", product.GetType().FullName);
 
    string workflowOperation = "Publish";
 
    WorkflowManager.MessageWorkflow(product.Id,
                                    product.GetType(),
                                    "OpenAccessDataProvider",
                                    workflowOperation,
                                    false,
                                    contextBag);
 
    Thread.CurrentThread.CurrentUICulture = currentCulture;
}

Adding translation

To add values for the other languages to the available LString 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 product.
    To add the values for the specified language, modify the product 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 TranslateProduct(Guid productId, string title, string description, string culture)
{
    var cultureInfo = new CultureInfo(culture);
    var currentCulture = Thread.CurrentThread.CurrentUICulture;
    Thread.CurrentThread.CurrentUICulture = cultureInfo;
 
    CatalogManager catalogManager = CatalogManager.GetManager();
    Product product = catalogManager.GetProduct(productId);
 
    if (product != null)
    {
        product.Title[cultureInfo] = title;
        product.Description[cultureInfo] = description;
        product.UrlName[cultureInfo] = Regex.Replace(title.ToLower(), @"[^\w\-\!\$\'\(\)\=\@\d_]+", "-");
 
        catalogManager.Provider.RecompileItemUrls(product);
        catalogManager.SaveChanges();
    }
 
    Thread.CurrentThread.CurrentUICulture = currentCulture;
}

Querying localized products

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

Modifying localized products

If you are not updating any of the LString properties, you can follow the same steps as when modifying a monolingual product. If you want to update the values of the localizable properties, follow the steps in the Adding translation section in this topic.

Deleting localized products

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

To delete a translation of a localized product, you must perform the same as when deleting the whole localized product, but instead the DeleteProduct method you have to call the DeleteItem method and pass the culture of the translation as an argument.

Here is a code example:

public static void DeleteProductTranslation(Guid productId, string culture)
{
    CatalogManager catalogManager = CatalogManager.GetManager();
 
    CultureInfo cultureInfo = new CultureInfo(culture);
 
    Product product = catalogManager.GetProduct(productId);
 
    if (product != null)
    {
        catalogManager.DeleteItem(product, cultureInfo);
 
        catalogManager.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