Windows Universal/Store App中的类库本地化 [英] Class Library Localization in Windows Universal / Store App

查看:48
本文介绍了Windows Universal/Store App中的类库本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows Store应用程序项目和一个类库项目的解决方案,我想添加本地化支持.
如何将所有资源文件添加到我的类库中,并在我的应用程序和类库中使用它们?

I have a solution with a Windows Store app project and a Class Library project and I want to add Localization support.
How can I add the all the Resource files to my Class Library and use them in both my App and Class Library?

推荐答案

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

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;

这篇关于Windows Universal/Store App中的类库本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆