2012-06-11 54 views
2

所以我創建了翻譯爲N語言(使用Qt語言學家)。我想編譯我的應用程序到N應用程序,該應用程序的前綴不同於_en_US_fr_FR,這些應用程序嵌入到每個翻譯的字符串中。此外,我想保留一個版本的應用程序,它將自動確定當前的平臺語言,其中包含所有翻譯版本。我應該如何更改我的.pro文件以實現這樣的結果?是否可以爲Qt中的每種語言編譯單獨的應用程序?

+2

但... * *爲什麼?!? –

+1

Qt的翻譯機制的重點在於你不需要精確地做你正在問的東西。 –

+2

@Styne,我認爲OP知道這一點。爲什麼不嘗試回答這個問題? – TonyK

回答

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) 
} 

它使用無證test function equals的。請注意,如果您 更改環境變量LANGUAGE_ID的值,則必須再次運行qmake (可能在刪除Makefiles之後)。

A(也許更好)替代方案是使用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]") 
相關問題