步骤 3 - 加载语言包

在此步骤中,您使用 Kanzi Engine API 在 Kanzi 应用程序中加载您在本教程上一步中创建的语言环境包。

使用 Kanzi Engine API 加载语言包。

在本节中,您使用 Kanzi Engine API 加载在上一节中创建的语言环境包。应用程序在启动时初始化语言环境包,并触发从存储在 Application/bin/Locale_packs 目录中的所有语言环境包中加载资源。

要加载地区包:

  1. 在 <KanziWorkspace>/Tutorials/Localization/Start/Application/bin/Locale_packs 目录的文件资源管理器中,创建一个 binaries.cfg 文件。在文件中添加您要加载的包含语言包的每个 kzb 文件的位置和名称。每行只添加一个 kzb 文件的名称。

    例如,如果您有一个适用于 Japanese、Korean 和 Chinese 语言环境的语言环境包,并且您将 ja-JP 语言环境名称用于 Japanese 语言环境,将 ko-KR 用于 Korean 语言环境,将 zh-ZH 用于 Chinese 语言环境,请创建 binaries.cfg 文件,在文本编辑器中打开它,将这些语言环境包文件的名称添加到 binaries.cfg 文件中,并保存该文件。

    ja-JP
    ko-KR
    zh-ZH
    
  2. 在 Kanzi Studio 中,选择 File > Open Kanzi Command Prompt。

    Kanzi Command Prompt 打开 Windows 命令提示符,并为您打开 Kanzi Command Prompt 的 Kanzi 版本设置 Kanzi 环境变量。

    使用 Kanzi Command Prompt 访问 Kanzi 实用程序和构建工具,例如 Gradle、SCons 和 CMake,而无需使用绝对路径或设置环境变量。

    ../../_images/open-kanzi-command-prompt.png

    小技巧

    您可以在 Windows 开始菜单的 Rightware 目录中找到 Kanzi Command Prompt。

    当您安装了多个版本的 Kanzi 时,确保将您要在该命令提示符下使用的 Kanzi 版本的 Kanzi Command Prompt 启动。

  3. 在 <KanziWorkspace>/Tutorials/Localization/Start/Application 目录中的 Kanzi Command Prompt 中,运行生成教程应用程序的 Visual Studio 解决方案的脚本。

    例如,如果您使用 Visual Studio 2019,运行

    generate_cmake_vs2019_solution.bat
    

    此脚本在目录 <KanziWorkspace>/Tutorials/Localization/Start/Application/build_vs2019 中为应用程序生成一个 Visual Studio 解决方案。

  4. 在 Visual Studio 中,打开 <KanziWorkspace>/Tutorials/Localization/Start/Application/build_vs<Version>/Localization_start.sln Visual Studio 解决方案。

    ../../_images/localization_start-project-contents.png
  5. 在 Visual Studio 的 Solution Explorer 中,右键点击 Localization_start 项目并选择 Set as StartUp Project。

    ../../_images/visual-studio-set-as-startup-project6.png
  6. 在本教程中,您使用在 Kanzi Studio 项目中定义的自定义属性类型。要访问这些自定义属性类型,请创建一个 DynamicPropertyType,并使用与 Kanzi Studio 项目中相同的名称。

    在 LocalizationApplication 类中的 localization.cpp 文件中,添加:

    class LocalizationApplication : public ExampleApplication
    {
    
  7. 在 LocalizationApplication 类中的 Application::onProjectLoaded 函数中,实现事件处理程序,该程序加载存储在 Locale_packs 目录中的语言环境包 kzb 文件的资源,并将语言环境添加到项目中。

    // Set the name of the directory where you stored the locale packs.
    static const string localizationFolderName = "Locale_packs";
    
    // Get the Screen node in the kzb file.
    // You use the Screen node to access all the other nodes and resources in the kzb file.
    ScreenSharedPtr screen = getScreen();
    // Domain is a collection of all subsystems and contains the Kanzi resource manager.
    // You need to get the resource manager to access the resources in the kzb file.
    ResourceManager* resourceManager = getDomain()->getResourceManager();
    
    // Get the LocaleSelector node which contains the locale selection buttons.
    // Access alias target nodes using the # sign followed by the name of the alias.
    Node2DSharedPtr localeSelector = screen->lookupNode<Node2D>("#LocaleSelector");
    
    // Get the LocaleButton prefabricated template used for the buttons that set the locale.
    // Get the reference to the template using the kzb URL.
    
    // When you use the full path of a resource, start the path with kzb://
    // followed by the project name and the location of the resource.
    PrefabTemplateSharedPtr localeButtonPrefab = resourceManager->acquireResource<PrefabTemplate>("kzb://localization/Prefabs/LocaleButton");
    
    // Get the custom property type for setting the name of the locale in the
    // LocaleButton node. You created this custom property type in the Kanzi Studio project.
    DynamicPropertyType<string> localeNameProperty = DynamicPropertyType<string>("Localization.LocaleName");
    
    // Get the custom property type for setting the locale in the application.
    // You created this custom property type in the Kanzi Studio project.
    DynamicPropertyType<string> localeIdProperty = DynamicPropertyType<string>("Localization.LocaleID");
    
    // Read the binaries.cfg file that contains the list of the kzb files
    // that contain the locale packs.
    string configFileName = localizationFolderName + "/binaries.cfg";
    ReadOnlyDiskFile binariesConfigFile(configFileName);
    string binariesConfigContents = readAsText(binariesConfigFile);
    stringstream binariesConfigFileStream(binariesConfigContents);
    
    string localeId;
    
    // Load the resources stored in the locale pack kzb files.
    while (getline(binariesConfigFileStream, localeId))
    {
        trim(localeId);
    
        // Create the path for the kzb file.
        string localizationKzbFilePath = localizationFolderName + "/" + localeId + ".kzb";
    
        // Add the kzb file to the Kanzi resource manager.
        getResourceManager()->addKzbFile(localizationKzbFilePath);
    
        string dictionaryUrl = "kzb://localization/Locales/" + localeId;
        ResourceDictionarySharedPtr localeDictionary = getResourceManager()->acquireResource<ResourceDictionary>(dictionaryUrl);
    
        // Instantiate the LocaleButton prefab.
        Node2DSharedPtr localeButton = localeButtonPrefab->instantiate<Node2D>("LocaleButton_" + localeId);
    
        // Get the name of the locale that is shown in the application from the
        // resource ID LocaleDisplayName stored in the localization table in the locale pack.
        TextResourceSharedPtr localeDisplayNameResource = localeDictionary->acquire<TextResource>(ResourceID("LocaleDisplayName"));
    
        // Set the LocaleName property so that the Text Block 2D node in the button shows the name of the locale.
        localeButton->setProperty(localeNameProperty, localeDisplayNameResource->getText());
        // Set the LocaleID property so that when you click the button the application changes to that locale.
        localeButton->setProperty(localeIdProperty, localeId);
    
        // Set the style of the Text Block 2D node in the button so that it sets
        // the correct font for the LocaleDisplayName of the locale.
        // Use this approach only to apply a style from the locale pack without changing the locale
        // in the application.
        optional<string> localeStyle = localeDictionary->find(ResourceID("LocaleStyle"));
        StyleSharedPtr style = getResourceManager()->acquireResource<Style>(*localeStyle);
        localeButton->setStyle(style);
    
        // Add the LocaleButton node to the LocaleSelector node.
        localeSelector->addChild(localeButton);
    }
    
  8. 选择您要使用的解决方案配置并运行您的应用程序。

    在开发期间,选择 Debug 配置。当您准备好创建生产版本时,选择 Release 配置。

    ../../_images/debug-configuration.png

在应用程序中,Kanzi 从 binaries.cfg 文件中列出的 kzb 文件加载语言环境包,并在 LocaleSelector 节点中为每个语言环境实例化一个 LocaleButton 预设。

../../_images/localization-completed.gif

接下来该做什么?

在本教程中,您学习了如何本地化 Kanzi 应用程序,并在您的应用程序中使用仅包含本地化资源的附加 kzb 文件。要进一步学习本教程,您可以使用本地化表 POT 模板文件将此 Kanzi 应用程序的内容翻译为其他语言。此外,您可以创建其他资源类型,并为不同的语言环境创建不同的资源。例如:创建多个动画,并为每个不同的语言环境使用一个动画:

另请参阅

要了解有关如何本地化您的 Kanzi 应用程序的更多信息,请参阅 本地化 (Localization)。

要了解有关 kzb 文件的更多信息,请参阅 kzb 文件。

要了解 Kanzi 如何处理资源,请参阅 资源管理。

要详细了解有关如何使用资源字典的信息,请参阅 资源 (Resources)。

要详细了解有关如何使用应用程序中字体的信息,请参阅 导入字体。

要详细了解有关如何使用应用程序中状态机的信息,请参阅 使用状态机。

要了解有关管理您的 Kanzi Studio 项目的更多信息,请参阅 工程 (Projects)。

要详细了解有关创建 Kanzi 应用程序的信息,请参阅 教程。

要了解有关 Kanzi Studio 功能的更多信息,请参阅 使用 …。