localization with .net compact framework

北城以北 提交于 2019-12-05 22:42:20

I used the tutorial - guide to do resource localization using Compact Framework: http://www.codeproject.com/Articles/28234/Survival-guide-to-do-resource-localization-using-C

The answer is simple. It should work. But it does not.

There is clearly a bug in Microsoft's tool CABWiz used by Visual Studio to generate CAB files. It has a problem when using files with the same name in different subfolders, like when using localizations.

After hours of trying to fix it, I ended up whith a solution inspired by the CodeProject guide as given by Cornel in the previous answer : You have to "hack" the Visual Studio process of generating CAB, by using resource files with unique name, and then modifying the INF file to specify the original name for deployment on the device.

To automatize a little more, I made a little EXE that is launched as project post-build :

        FileInfo CurrentExeInfo = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);

        // Current Folder + bin\Debug
        DirectoryInfo BinDebug = new DirectoryInfo( Path.Combine( CurrentExeInfo.Directory.FullName,  @"bin\Debug") );

        // Subfolders in \bin\Debug
        Console.WriteLine(BinDebug.FullName);
        string[] Dirs = Directory.GetDirectories(BinDebug.FullName, "*", SearchOption.TopDirectoryOnly);

        // In each localization folder ...
        foreach (string Dir in Dirs)
        {
            DirectoryInfo DirInfo = new DirectoryInfo(Dir);

            // ... Resource files
            string[] RFiles = Directory.GetFiles(Dir, "*.resources.dll");

            foreach (string RFile in RFiles)
            {
                FileInfo RFileInfo = new FileInfo(RFile);
                bool DoCopy = false;

                // No underscore in resource name
                if (!RFileInfo.Name.Contains("_") || RFileInfo.Name.IndexOf("_") == 0)
                {
                    DoCopy = true;
                }
                // underscore in resource name
                // --> Have to check if already a copy 
                else
                { 
                    // prefix removal
                    int PrefixIndex = RFileInfo.Name.IndexOf("_");
                    string TestFilename = RFileInfo.Name.Substring(PrefixIndex + 1);

                    if (!File.Exists(Path.Combine(Dir, TestFilename)))
                    {
                        // File without underscore does not exist, so must copy
                        DoCopy = true;
                    }
                }

                if (DoCopy)
                {
                    // Copy file
                    string NewFileName = Path.Combine(Dir, DirInfo.Name.ToUpper() + "_" + RFileInfo.Name);
                    Console.WriteLine("Copying " + RFile + " -> " + NewFileName);
                    File.Copy(RFile, NewFileName, true);
                }
            }
        }

And then this CAB patcher after normal CAB generation :

    const string cabwizpath = @"C:\Program Files (x86)\Microsoft Visual Studio 9.0\SmartDevices\SDK\SDKTools\cabwiz.exe";

    static void Main(string[] args)
    {
        if (args.Length == 0)
        {
            Console.WriteLine("Aborted: You must enter the inf file information");
            Console.ReadLine();
            return;
        }
        if (!File.Exists(args[0]))
        {
            Console.WriteLine("Aborted: I can not found the INF file!");
            Console.ReadLine();
            return;
        }

        // string to search
        Regex R = new Regex("\"[A-Z]{2,3}_(.+)\\.resources\\.dll\",\"([A-Z]{2,3})_(.+)\\.resources\\.dll\"");

        // File reading
        string inffile = File.ReadAllText(args[0]);

        // Format replace from
        // "FR_ProjectName.resources.dll","FR_ProjectName.resources.dll"
        // To
        // "ProjectName.resources.dll","FR_ProjectName.resources.dll"
        inffile = R.Replace(inffile, "\"$1.resources.dll\",\"$2_$3.resources.dll\"");

        // Rewriting file
        File.WriteAllText(args[0], inffile);
        Console.WriteLine("INF file patched ...");

        // Génération du CAB ...
        Console.WriteLine("Generating correct CAB ... ");
        System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo("\"" + cabwizpath + "\"", "\"" + args[0] + "\"");
        proc.ErrorDialog = true;
        proc.UseShellExecute = false;
        proc.RedirectStandardOutput = true;
        Process CabWiz =  Process.Start(proc);
        Console.WriteLine("\""+cabwizpath + "\" \""+ args[0]+"\"");
        CabWiz.WaitForExit();
        Console.WriteLine("CAB file generated (" + CabWiz.ExitCode + ") !");
    }

I hope it helps.

More links about this :

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!