简体   繁体   English

使用.net紧凑框架进行本地化

[英]localization with .net compact framework

I'm developing an application for mobile devices (Windows CE 5.0, .NET compact framwork 2.0 pre-installed) using .NET compact framework 3.5 and MS Visual Studio 2008. 我正在使用.NET compact framework 3.5和MS Visual Studio 2008开发移动设备应用程序(Windows CE 5.0,.NET compact framwork 2.0预安装)。

I'm using the built-in option for creating localized forms. 我正在使用内置选项来创建本地化表单。 This works perfectly as long as I use the debugging function of Visual Studio with the mobile device connected to my desktop computer. 只要我将Visual Studio的调试功能与连接到台式计算机的移动设备一起使用,这就完美无缺。 In this case, Visual Studio deploys my application along with .NET compact framework 3.5. 在这种情况下,Visual Studio将我的应用程序与.NET compact framework 3.5一起部署。 After disconnecting the mobile device and having installed my application it is still working as expected. 断开移动设备并安装我的应用程序后,它仍然按预期工作。

My problem is: If I install the .NET compact framework using the CAB file provided by Microsoft and then install my application (also by using the CAB file created by Visual Studio) without having used the debugger the application works as well but without localization. 我的问题是:如果我使用Microsoft提供的CAB文件安装.NET compact框架然后安装我的应用程序(也使用Visual Studio创建的CAB文件)而没有使用调试器,那么应​​用程序也能正常运行但没有本地化。 So I think there must be some parts of the .NET framework which are only installed using the deployment function of Visual Studio - and which are making .net recognizing the locale. 所以我认为必须有一些.NET框架的部分只使用Visual Studio的部署功能安装 - 并且正在使.net识别语言环境。 - Does anybody know which parts (libraries...?) are these? - 有谁知道哪些部分(图书馆......?)是这些? Since the application will be provided to users which will not use Visual Studio I've to find a solution for this. 由于应用程序将提供给不使用Visual Studio的用户,我将为此找到解决方案。

我使用教程 - 指南使用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. 微软的工具CABWiz中显然存在一个错误,它由Visual Studio用于生成CAB文件。 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. 经过几个小时的尝试修复后,我最终得到了一个解决方案, 解决方案受到Cornel在上一个答案中给出的CodeProject指南的启发:你必须通过使用具有唯一名称的资源文件来“破解”生成CAB的Visual Studio过程,然后修改INF文件以指定在设备上部署的原始名称。

To automatize a little more, I made a little EXE that is launched as project post-build : 为了自动化更多,我做了一个EXE,它是作为项目后期构建启动的:

        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 : 然后在正常的CAB生成之后这个CAB修补程序:

    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 : 更多关于此的链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM