且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Windows Universal/Store App中的类库本地化

更新时间:2023-01-01 21:03:50

好吧,我找到了解决方法,并找到了一个示例项目

Ok, I found how to do this and with a sample project found here Basically the implementation is the following:

  • 在ClassLibrary中创建一个名为字符串"的文件夹
  • 在字符串"文件夹中,为每种语言(例如,en,fr,pt等)创建一个
  • 并使用键/值在每个文件夹中添加一个Resources.resw

现在在您的ClassLibrary中添加一个具有以下代码(适用于您的项目)的新类:

Now add a new Class in your ClassLibrary that has the following code(adapted to your project):

using System;
using Windows.ApplicationModel.Resources;

namespace MyClassLibraryName.Tools {
    public static class LocalizationTool {
        static ResourceLoader resourceLoader = null;

        public static string MyStringOne {
            get {
                String name;
                GetLibraryName("MyStringOne", out name);
                return name;
            }
        }

        private static void GetLibraryName(string resourceName, out string resourceValue) {
            if(resourceLoader == null) {
                resourceLoader = ResourceLoader.GetForCurrentView("MyClassLibraryName/Resources");
            }
            resourceValue = resourceLoader.GetString(resourceName);
        }
    }
}

在您的ClassLibrary或MainApp中,只需调用以下内容:

And in your ClassLibrary or MainApp just call the following:

string text = LocalizationTool.MyStringOne;