A Simple C# Utility to Upgrade Language Packs

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
11 years ago
Hello,

I hope this is useful for those who use nopCommerce language packs. It all started when I couldn't find the Arabic language pack for nopCommerce 2.80, and the latest I could find was for nopCommerce 1.90. Although this sounds very outdated, but I thought that it was better than nothing. At least I have something to start with.

Anyway, I faced a situation where I wanted a language pack XML file that has ALL the resource keys of v2.80, but wanted the subset of those keys (which were already there since v1.9) in Arabic. And since necessity is the mother of invention, I developed this simple utility that generates this desired file. Basically, the utility takes two inputs:

1. The path to the outdated non-English language pack XML file.

2. The path of an up-to-date vanilla English language pack XML file. You can simply get this one by logging into the Admin panel of nopCommerce 2.80 --> Go to Configuration --> Languages --> Edit the English language --> Click on the Export Resources button --> Save the generated XML file somewhere on your hard disk.

The utility will load all the resource key names from file #2, look if the same key exist in file #1, modify the value of resource key in file #2 with the translated value from file #1, loop through all the resource keys, and finally save a copy of file #2 under the name "file#1_upgraded_yymmdd-HHMM.xml".

Here is the code.. It is a C# console application.


using System;
using System.Xml;
using System.IO;

namespace langpackupg
{
    class Program
    {
        static void Main(string[] args)
        {
            string outdated_langpack_file_path, vanilla_en_langpack_file_path;

            Console.WriteLine("Enter the path to your old language pack file that you want to upgrade");
            Console.Write("> ");
            outdated_langpack_file_path = Console.ReadLine();

            Console.WriteLine("\nEnter the path to the vanilla English language pack file of the latest nopCommerce version");
            Console.Write("> ");
            vanilla_en_langpack_file_path = Console.ReadLine();

            Console.Write("\nLoading the two files in XML DOMs...");

            DateTime timestamp = DateTime.Now;
            string upgraded_langpack_file_path = outdated_langpack_file_path.Insert(
                outdated_langpack_file_path.LastIndexOf('.'),
                string.Format("_upgraded_{0:yyMMdd-HHmm}", timestamp));
            File.Copy(vanilla_en_langpack_file_path, upgraded_langpack_file_path, true);

            XmlDocument outdated_langpack_xml = new XmlDocument();
            outdated_langpack_xml.Load(outdated_langpack_file_path);

            XmlDocument upgraded_langpack_xml = new XmlDocument();
            upgraded_langpack_xml.Load(upgraded_langpack_file_path);

            Console.WriteLine("Done.\n\nComparing...");

            XmlNodeList upgradable_list = upgraded_langpack_xml.SelectNodes("/Language/LocaleResource");

            int successCount = 0;
            for (int i = 0; i < upgradable_list.Count; i++)
      {
                try
                {
                    Console.Write("\r{0}%   ", (int)(100.0 * i / upgradable_list.Count));
                    XmlNode node = outdated_langpack_xml.SelectSingleNode(string.Format("/Language/LocaleResource[@Name='{0}']", upgradable_list[i].Attributes["Name"].Value));
                    if (node != null)
                    {
                        // Update the Value of this node
                        upgradable_list[i].FirstChild.InnerXml = node.FirstChild.InnerXml;
                        successCount++;
                    }
                }
                catch (Exception)
                {
                    continue;
                }
      }
            upgraded_langpack_xml.SelectSingleNode("/Language").Attributes["Name"].Value = outdated_langpack_xml.SelectSingleNode("/Language").Attributes["Name"].Value;
            upgraded_langpack_xml.Save(upgraded_langpack_file_path);
            Console.WriteLine("\r100%   \nSuccess! {0} out of {1} were migrated.", successCount, upgradable_list.Count);
        }
    }
}
11 years ago
Ok folks... I just figured out that there already exists a tool that does the same thing, and probably with a nice and colorful UI :)

https://www.nopcommerce.com/p/590/nopcommerce-language-pack-upgrader-ver-140.aspx

Didn't notice it before now.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.