Qt 에서 멀티언어 설정을 하고 있습니다.
안녕하세요.. ^^"
Qt 멀티언어 설정 중 도대체가 안되는 부문이 있어서 문의 드립니다.
코드는 간단한 예제프로그램을 참조하여 만들었습니다.
Title메뉴(Language)가 있고 Language를 누르면 1 German, 2 English, 3 Korean 의 Action메뉴가 나타납니다.
그 중에 언어를 선택하면 Title 부분의 Language 글자를 각각의 언어에 맞게 변경하려고 하는데요.
선택 시 setTitle(tr("&Language"));를 해도 변경이 되질 않습니다.
.qm 코드는 각 언어에 맞게 3개를 만들었습니다.
혹시 적극적으로 도와주실분이 계실지 몰라서 전체코드 첨부합니다..^^;;
코드는 아래와 같습니다..
#include
#include
#include
#include
#include "mainwindow.h"
MainWindow::MainWindow()
{
createMenus();
retranslateUi();
}
void MainWindow::createMenus()
{
languageMenu = new QMenu(this);
languageActionGroup = new QActionGroup(this);
connect(languageActionGroup, SIGNAL(triggered(QAction *)), this, SLOT(switchLanguage(QAction *)));
QDir qmDir = directoryOf(".");
QStringList fileNames = qmDir.entryList(QStringList("multilang_*.qm"));
for(int i=0;i
{
QString locale = fileNames[i];
locale.remove(0, locale.indexOf('_') + 1);
locale.chop(3);
QTranslator translator;
translator.load(fileNames[i], qmDir.absolutePath());
QString language = translator.translate("MainWindow", "English");
QAction *action = new QAction(tr("&%1 %2")
.arg(i+1).arg(language), this);
action->setCheckable(true);
action->setData(locale);
languageMenu->addAction(action);
languageActionGroup->addAction(action);
if(language == "English")
action->setCheckable(true);
}
menuBar()->addMenu(languageMenu);
}
void MainWindow::retranslateUi()
{
languageMenu->setTitle(tr("&Language"));
}
void MainWindow::switchLanguage(QAction *action)
{
QString locale = action->data().toString();
QString qmPath = directoryOf(".").absolutePath();
appTranslator->load("multilang_"+locale, qmPath);
qtTranslator->load("qt_"+locale, qmPath);
}
void MainWindow::changeEvent(QEvent *event)
{
if(event->type() == QEvent::LocaleChange)
{
QString qmPath = directoryOf(".").absolutePath();
appTranslator->load("multilang_"+QLocale::system().name(), qmPath);
qtTranslator->load("qt_"+QLocale::system().name(), qmPath);
retranslateUi();
}
QMainWindow::changeEvent(event);
}
QDir MainWindow::directoryOf(const QString &subdir)
{
QDir dir(QApplication::applicationDirPath());
dir.cd(subdir);
return dir;
}
첨부 | 파일 크기 |
---|---|
multilang.tar.gz | 26.11 KB |
Qt 사이트에서 발췌했습니다. 혹시 참고가
Qt 사이트에서 발췌했습니다. 혹시 참고가 될지??
Dynamic Translation
Some applications, such as Qt Linguist, must be able to support changes to the user's language settings while they are still running. To make widgets aware of changes to the installed QTranslators, reimplement the widget's changeEvent() function to check whether the event is a LanguageChange event, and update the text displayed by widgets using the tr() function in the usual way. For example:
void QWidget::changeEvent(QEvent *event)
{
if (e->type() == QEvent::LanguageChange) {
titleLabel->setText(tr("Document Title"));
...
okPushButton->setText(tr("&OK"));
} else
QWidget::changeEvent(event);
}
All other change events should be passed on by calling the default implementation of the function.
The list of installed translators might change in reaction to a LocaleChange event, or the application might provide a user interface that allows the user to change the current application language.
The default event handler for QWidget subclasses responds to the QEvent::LanguageChange event, and will call this function when necessary; other application components can also force widgets to update themselves by posting the LanguageChange event to them.
원문은 아래 링크에서 확인하실 수 있습니다.
http://doc.qt.nokia.com/4.5/i18n.html#dynamic-translation
댓글 달기