2

所以我为N语言创建了翻译(使用 Qt linguist)。我想将我的应用程序编译成N与前缀不同_en_US_fr_FR嵌入到每个翻译字符串中的应用程序。此外,我想保留一个版本的应用程序,它将自动确定当前平台语言,其中包含所有翻译版本。我应该如何更改我的.pro文件以达到这样的结果?

4

1 回答 1

1

我认为嵌入所有翻译并在运行时决定加载哪个翻译要容易得多。也许您可以提供一个命令行开关或一个选项来覆盖系统区域设置。您甚至不必将它们嵌入到可执行文件中,您可以将它们发送到“翻译”目录中。要在运行时获取系统语言环境,您可以使用QLocale类:

Application application(argc, argv);

QString locale = QLocale::system().name();
QString translationsDir = ":/translations";

QTranslator appTranslator;
QTranslator qtTranslator;

appTranslator.load(locale, translationsDir);
qtTranslator .load("qt_" + locale,
                    QLibraryInfo::location(QLibraryInfo::TranslationsPath));

application.installTranslator(& appTranslator);
application.installTranslator(& qtTranslator);

无论如何,如果你真的想以你的方式做,你可以依靠环境变量LANGUAGE_ID来检测要在你的构建中嵌入什么语言,然后为每种可用语言重建你的项目。这可能需要很多时间,但也许您只能在最终构建时这样做。

这是一个例子:

#include <iostream>

int main(int argc, char *argv[])
{
#ifdef EMBED_ONLY_ONE_LANGUAGE
    std::cout << "Embedded language is " << LANGUAGE_ID << std::endl;
#elif EMBED_ALL_LANGUAGES
    std::cout << "Embedded all languages" << std::endl;
#else
    std::cout << "What???" << std::endl;
#endif
}

这是 .pro 文件:

TEMPLATE = app
DEPENDPATH += .
INCLUDEPATH += .
TARGET = SomeName

CONFIG -= qt
CONFIG += console

# Input
SOURCES += main.cpp

# It seems that "equals" does not work with environment variables so we
# first read it in a local variable.
LANGUAGE_ID=$$(LANGUAGE_ID)

equals(LANGUAGE_ID,) {
    # No language id specified. Add the build instructions to embed all the
    # translations and to decide at runtime which one to load.
    message(No language id specified)

    # This adds a preprocessor variable so that the program knows that it has
    # every language.
    DEFINES *= EMBED_ALL_LANGUAGES
} else {
    # A language id was specified. Add the build instructions to embed only
    # the relative translation.
    message(Specified language id: $$LANGUAGE_ID)

    # This adds a preprocessor variable LANGUAGE_ID whose value is the language.
    DEFINES *= LANGUAGE_ID=\\\"$$LANGUAGE_ID\\\"

    # This adds a preprocessor variable so that the program knows that it has
    # only one language.
    DEFINES *= EMBED_ONLY_ONE_LANGUAGE

    # This renames the executable.
    TARGET=$${TARGET}_$$(LANGUAGE_ID)
}

它利用了未记录的测试功能equals。请注意,如果您更改环境变量的值,LANGUAGE_ID您必须再次运行 qmake(可能在删除 Makefile 之后)。

一个(也许更好的)替代方法是使用 CMake 并将语言 id 指定为 CMake 的命令行变量:

cmake_minimum_required(VERSION 2.6)
project(SomeName)

set(SOURCES main.cpp)

add_executable(SomeName ${SOURCES})

if(${LANGUAGE_ID} MATCHES "[a-z][a-z]_[A-Z][A-Z]")
    # A language id was specified. Add the build instructions to embed only
    # the relative translation.
    message("Embedding language ${LANGUAGE_ID}")

    # This adds a preprocessor variable LANGUAGE_ID whose value is the language.
    add_definitions("-DLANGUAGE_ID=\"${LANGUAGE_ID}\"")

    # This adds a preprocessor variable so that the program knows that it has
    # only one language.
    add_definitions("-DEMBED_ONLY_ONE_LANGUAGE")

    # This renames the executable.
    set_target_properties(SomeName PROPERTIES OUTPUT_NAME "SomeName_${LANGUAGE_ID}")

else(${LANGUAGE_ID} MATCHES "[a-z][a-z]_[A-Z][A-Z]")
    # No language id specified. Add the build instructions to embed all the
    # translations and to decide at runtime which one to load.
    message("Not embedding any language")

    # This adds a preprocessor variable so that the program knows that it has
    # every language.
    add_definitions("-DEMBED_ALL_LANGUAGES")

endif(${LANGUAGE_ID} MATCHES "[a-z][a-z]_[A-Z][A-Z]")
于 2012-06-11T08:20:27.033 回答