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的具体步骤
- 创建翻译文件
首先创建翻译文件 xxxx.tet
language: French
countries: fr be mc ch lu
"hello" = "bonjour"
"goodbye" = "au revoir"
- .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()
{
}