2012-06-11 4 views
2

그래서 Qt 언어 전문가를 사용하여 N 개의 언어에 대한 번역을 만들었습니다. 내 애플 리케이션 N_en_US 또는 _fr_FR 접두사와 다른 애플 리케이션으로 컴파일하려는 각 번역 된 문자열에 포함. 또한 내부의 모든 번역 버전을 가진 현재 플랫폼 언어를 자동으로 결정하는 앱의 버전을 유지하고 싶습니다. 이러한 결과를 얻기 위해 내 .pro 파일을 어떻게 변경해야합니까?Qt에서 각 언어에 대해 별도의 앱을 컴파일 할 수 있습니까?

+2

하지만 ... :

A는 (아마도 더 나은) 다른 방법은 CMake를 사용하고 으로 CMake의 명령 줄 변수를 언어 ID를 지정하는 것입니다?!? –

+1

Qt의 번역 메커니즘의 요점은 당신이 요구하는 것을 정확하게 할 필요가 없다는 것입니다. –

+2

@Styne, 저는 OP가 이것을 알고 있다고 생각합니다. 왜 주어진 질문에 대답하려고하지 않습니까? – TonyK

답변

1

모든 번역을 임베드하고 런타임에 어떤 것을로드 할 것인가를 결정하는 것이 더 쉬울 것 같습니다. 아마도 명령 줄 스위치 또는 에 대한 옵션을 제공하여 시스템 로캘을 무시할 수 있습니다. 실행 파일에 포함시키지 않아도되므로 "translations"디렉토리에 해당 파일을 포함시킬 수 있습니다. 당신이 QLocale 클래스 사용할 수 있습니다 실행시 시스템 로케일 얻으려면 : 당신이 정말 당신이 빌드에 포함 할 어떤 언어 감지하는 환경 변수 LANGUAGE_ID에 의존 수있는 방법으로 수행하려는 경우

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

어쨌든을, 그리고 사용 가능한 각각의 언어에 대한 프로젝트를 다시 빌드하십시오. 시간이 많이 걸릴 수도 있지만, 최종 빌드 인 에만 해당 작업을 수행 할 수 있습니다. 여기

은 예입니다

#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를 삭제 한 후 가능). * 왜 *

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]") 
관련 문제