이 페이지에서

실행 시 언어 전환 기능이 있는 지역화된 시계

이 예제는 CMake에서 Qt의 번역 및 현지화 기능을 사용하는 모범 사례를 보여주며, Qt Quick, 특히 런타임 중에 애플리케이션의 언어를 변경하는 방법을 보여줍니다. 이 예제는 더 간단한 ‘현지화된 시계 예제’를 확장한 것입니다.

Qt 애플리케이션 번역에 대한 자세한 내용은 《 Qt Linguist 》 매뉴얼을 참조하십시오.

사용자 인터페이스

이 예제는 시스템의 로케일 및 언어에 따라 현재 시간과 날짜를 표시합니다. UI 텍스트는 영어, 독일어, 프랑스어, 스페인어, 이탈리아어, 일본어, 한국어, 포르투갈어, 아랍어, 중국어로 현지화되어 있습니다. 사용자는 메뉴 버튼을 사용하여 다른 언어와 로케일을 선택할 수도 있습니다.

애플리케이션의 영어 버전:

영어로 표시된 디지털 시계의 스크린샷

실행 중에 언어를 변경한 후의 독일어 버전:

독일어로 표시된 디지털 시계의 스크린샷

구현

이 애플리케이션은 네 가지 부분으로 구성되어 있습니다:

CMakeLists.txt

CMake 통합 방식은 ‘Localized Clock’ 예제의 CMake 통합 방식과 동일합니다. 자세한 내용은 해당 페이지를 참조하십시오.

main.cpp

이 애플리케이션의 시작점이며, QML 모듈을 불러오는 역할을 합니다.

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    QObject::connect(
            &engine, &QQmlApplicationEngine::objectCreationFailed, &app,
            [](const QUrl &) { QCoreApplication::exit(-1); }, Qt::QueuedConnection);
    engine.loadFromModule("qtexamples.localizedclockswitchlocale", "Main");
    return app.exec();
}
번역기 관리자 (translator.h, translator.cpp)

TranslatorManager 는 런타임 시 언어 및 로케일 전환을 관리하는 QML 싱글톤 클래스입니다. 이 클래스의 핵심 메서드는 언어 코드를 나타내는 문자열을 매개변수로 받는 ` TranslatorManager::switchLanguage()`입니다.

이 메서드는 ` Q_INVOKABLE`로 표시되어 있지만, `TranslatorManager` 클래스는 ` QML_ELEMENT ` 및 `QML_SINGLETON`으로 선언되어 있습니다. 이를 통해 QML에서 이 메서드에 직접 접근할 수 있습니다. 또한 Main.qml을 참조하십시오.

void TranslatorManager::switchLanguage(const QString&lang)
{
    QLocale locale(lang);
    QLocale::setDefault(locale);
    qApp->removeTranslator(&m_translator); // not necessary from Qt 6.10
   if (m_translator.load(locale, "clock"_L1, "_"_L1, ":/i18n/"_L1)
       && qApp->installTranslator(&m_translator)) {
        m_engine.retranslate();
    } else {
        qWarning("Could not load translation to %s!", qPrintable(locale.name()));
    }
}
실행 시 번역 업데이트

언어가 변경되면 이전 번역이 제거되고(QCoreApplication::removeTranslator()), 선택된 언어에 대한 새로운 번역이 로드(QTranslator::load()) 및 설치(QCoreApplication::installTranslator())됩니다. 그런 다음 QQmlEngine::retranslate()가 호출되어 QML 문서 내의 문자열에 대한 재번역이 트리거됩니다.

날짜 및 시간 형식 업데이트

언어를 변경하면 날짜와 시간의 표시 방식에도 영향을 미칠 수 있습니다. 예를 들어, 시스템의 QLocale 이 "en"에 대해서는 "en_US" 로케일을, "de"에 대해서는 "de_DE" 로케일을 선호하는 경우:

  • 애플리케이션은 처음에 국가 코드로 “US”가 설정된 상태로 로드됩니다.
  • 미국 로케일 규칙에 따라 날짜는 MM/DD/YYYY 형식으로 표시되며, 시간은 12시간제를 사용합니다.
  • 언어를 독일어로 변경하면, "de_DE"가 독일어의 첫 번째 우선 로케일이 되므로 국가가 독일로 변경됩니다.
  • 독일어 관례에 따라 날짜는 DD.MM.YYYY 형식으로 표시되며 24시간 제가 사용됩니다.

언어를 전환하면, 애플리케이션은 QLocale::setDefault()를 사용하여 선택한 언어에 따라 QLocale 를 업데이트합니다. 시간을 표시할 때, Date.toLocaleTimeString() 함수는 애플리케이션의 로케일에 따라 시간과 날짜를 현지화합니다( Main.qml 참조).

Main.qml

메인 QML 파일은 애플리케이션의 사용자 인터페이스를 정의합니다. 이 UI는 시간, 날짜 및 초 카운터를 표시합니다. 기본 번역(창 제목) 및 복수형 처리(초 수)에 대해서는 'Localized Clock' 예제를 참조하십시오.

Menu 에서 항목을 선택하면 해당 언어 코드와 함께 TranslatorManager.switchLanguage() 가 호출됩니다. 이 호출은 번역 가능한 텍스트를 다시 번역하는 작업도 처리합니다( Translator Manager 참조). 그러나 언어가 변경되었을 때 새로운 현지화 형식으로 시간 표기를 업데이트하려면 updateClock() 를 호출해야 합니다.

        ListModel {
            id: languageModel
            ListElement { name: "English"; code: "en" }
            ListElement { name: "Deutsch"; code: "de" }
            ListElement { name: "العربية"; code: "ar" }
            ListElement { name: "한국어"; code: "ko" }
            ListElement { name: "中文"; code: "zh" }
            ListElement { name: "日本語"; code: "ja" }
            ListElement { name: "Français"; code: "fr" }
            ListElement { name: "Italiano"; code: "it" }
            ListElement { name: "Español"; code: "es" }
            ListElement { name: "Português"; code: "pt" }
        }
        Menu {
            id: languageMenu
            width: languageButton.width
            Repeater {
                model: languageModel
                delegate: MenuItem {
                    required property string name
                    required property string code
                    text: name
                    onTriggered: {
                        TranslatorManager.switchLanguage(code)
                        root.selectedLanguage = name
                        root.updateClock()
                    }
                }
            }
        }

코드 예제 @ code.qt.io

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.