C++ 是否可以为Qt中的每种语言编译单独的应用程序?
所以我为C++ 是否可以为Qt中的每种语言编译单独的应用程序?,c++,qt,localization,projects-and-solutions,C++,Qt,Localization,Projects And Solutions,所以我为N语言创建了翻译(使用Qt语言学家)。我想将我的应用程序编译成不同前缀的N应用程序,比如\u en\u US或\u fr\u fr嵌入到每个翻译字符串中。此外,我想保留一个版本的应用程序,将自动确定当前的平台语言有所有的翻译版本内。如何更改我的.pro文件以获得这样的结果?我认为嵌入所有翻译并在运行时做出决定要容易得多 要加载哪一个。也许您可以提供一个命令行开关或一个选项来 重写系统区域设置。您甚至不必将它们嵌入到可执行文件中, 您可以在“翻译”目录中发送它们。在运行时获取系统语言环境
N
语言创建了翻译(使用Qt语言学家)。我想将我的应用程序编译成不同前缀的N
应用程序,比如\u en\u US
或\u fr\u fr
嵌入到每个翻译字符串中。此外,我想保留一个版本的应用程序,将自动确定当前的平台语言有所有的翻译版本内。如何更改我的.pro
文件以获得这样的结果?我认为嵌入所有翻译并在运行时做出决定要容易得多
要加载哪一个。也许您可以提供一个命令行开关或一个选项来
重写系统区域设置。您甚至不必将它们嵌入到可执行文件中,
您可以在“翻译”目录中发送它们。在运行时获取系统语言环境
您可以使用类:
无论如何,如果你真的想按自己的方式做,你可以依靠环境变量LANGUAGE\u 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
}
它利用了未记录的数据。请注意,如果
更改环境变量LANGUAGE\u 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]")
Qt的翻译机制的要点是,你不需要严格按照你的要求去做。@Styne,我想OP知道这一点。为什么不试着回答给出的问题呢?@TonyK,因为这种问题高呼“糟糕的设计”。一个明智的解决方案是让用户明白如何更改语言,然后使用Qt的内置机制。这才更有意义。
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)
}
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]")