【juce::translation】juce实现翻译文件生成器,多国语言显示

translation File Builder(翻译文件生成器)

1.1构造函数

 LocalisedStrings::LocalisedStrings(const String & fileContents, bool ignoreCaseOfKeys)

                从翻译文件的文本创建一组翻译。当您创建其中一个映射时,您可以调用         setCurrentMappings() 以使其成为系统正在使用的映射集。

 LocalisedStrings::LocalisedStrings(const File & fileToLoad, bool ignoreCaseOfKeys)

                从翻译文件的文本创建一组翻译。当您创建其中一个映射时,您可以调用         setCurrentMappings() 以使其成为系统正在使用的映射集。

LocalisedStrings::LocalisedStrings(const File & fileToLoad, bool  ignoreCaseOfKeys)

    LocalisedStrings::LocalisedStrings (const LocalisedStrings & )

1.2设置翻译映像

static void LocalisedStrings::setCurrentMappings(LocalisedStrings * newTranslations)

选择系统要使用的当前映射集。您传入的对象将在不再需要时自动删除,因此请不要保留指向它的指针。您也可以传入 nullptr 来删除当前映射。

static String LocalisedStrings::translateWithCurrentMappings(const String & text)

尝试使用当前选定的映射集翻译字符串。如果未设置映射,或者映射不包含字符串的翻译,则将仅返回原始字符串。                

  static String LocalisedStrings::translateWithCurrentMappings(const char* text)

尝试使用当前选定的映射集翻译字符串。如果未设置映射,或者映射不包含字符串的翻译,则将仅返回原始字符串。

1.3翻译

String LocalisedStrings::translate(const String & text) const

尝试查找字符串并返回其本地化版本。如果在列表中找不到该字符串,则将返回原始字符串

String LocalisedStrings::translate(const String & text, const String & resultIfNotFound) const

尝试查找字符串并返回其本地化版本。如果在列表中找不到该字符串,则将返回 resultIfNotFound 字符串。

 String LocalisedStrings::getLanguageName() const

返回翻译文件中指定的语言的名称。 这是在文件中使用以“language:”开头的行指定的,

 const StringArray& LocalisedStrings::getCountryCodes() const

返回翻译文件中列出的合适国家/地区代码的列表。

 const StringPairArray& LocalisedStrings::getMappings() const

提供对实际映射列表的访问

 void LocalisedStrings::addStrings(const LocalisedStrings&)

将另一组翻译添加并合并到此集中。请注意,新 LocalisedStrings 对象的语言名称和国家 / 地区代码必须与此对象的语言名称和国家 / 地区代码匹配 - 如果它们不匹配,将引发断言。任何现有值的映射都将被新值覆盖。

 void LocalisedStrings::setFallback(LocalisedStrings * fallbackStrings)

为此对象提供一组字符串,如果找不到字符串,则用作回退。传入的对象将由此对象拥有,并在不再需要时由此对象删除。可以是 nullptr 来清除现有的回退对象。

1.4 juce 中实现translation的具体步骤

  1. 创建翻译文件

首先创建翻译文件 xxxx.tet

language: French

countries: fr be mc ch lu

"hello" = "bonjour"

"goodbye" = "au revoir"

  1. .h文件
#pragma once

#include <JuceHeader.h>


using namespace juce;

class TranslationStu  : public juce::Component

{

public:

    TranslationStu();

    ~TranslationStu() override;

    void paint (juce::Graphics&) override;

    void resized() override;

private:

    LocalisedStrings* w_translation; //实例化翻译对象

    JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (TranslationStu)

};

3、.cpp文件

#include <JuceHeader.h>

#include "TranslationStu.h"

TranslationStu::TranslationStu()

{

//设置全局字体,防止中文乱码

    juce::LookAndFeel::getDefaultLookAndFeel().setDefaultSansSerifTypefaceName(L"宋体");

String str = L"";

    juce::File tt("D:\\translation_Demo.txt");

    str = tt.loadFileAsString();


    w_translation = new LocalisedStrings(str, true);//构造函数


    LocalisedStrings::setCurrentMappings(w_translation);//设置翻译映像

}

TranslationStu::~TranslationStu()

{

}

void TranslationStu::paint (juce::Graphics& g)

{

    g.fillAll (getLookAndFeel().findColour (juce::ResizableWindow::backgroundColourId));   // clear the background

    g.setColour (juce::Colours::grey);

    g.drawRect (getLocalBounds(), 1);   // draw an outline around the component



    g.setColour (juce::Colours::white);

    //Nirmala UI Semilight 显示印度语和英语的字体

    g.setFont(juce::Font(L"Nirmala UI Semilight", 16.0f, 0));

    String str1 = translate(L"你好");//翻译

    g.drawText(str1, getLocalBounds(), juce::Justification::centred, true);

}

void TranslationStu::resized()

{

}

  • 19
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值