From b1260c0b414176dab1c6827341d37e57b1d2ec0b Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Sun, 6 Oct 2019 17:31:27 +0800 Subject: [PATCH] i18n: zh_CN locale support `CONFIG += embed_translations` is used. --- .gitignore | 3 + PineapplePictures.pro | 6 +- appveyor.yml | 2 +- graphicsscene.cpp | 2 +- graphicsview.cpp | 10 ++-- languages/PineapplePictures.ts | 88 ++++++++++++++++++++++++++++ languages/PineapplePictures_zh_CN.ts | 88 ++++++++++++++++++++++++++++ main.cpp | 7 +++ mainwindow.cpp | 15 +++++ 9 files changed, 212 insertions(+), 9 deletions(-) create mode 100644 languages/PineapplePictures.ts create mode 100644 languages/PineapplePictures_zh_CN.ts diff --git a/.gitignore b/.gitignore index fb1a0d8..936149c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ # User files *.user + +# Translation files +*.qm diff --git a/PineapplePictures.pro b/PineapplePictures.pro index 3b4a66a..2cf1dc4 100644 --- a/PineapplePictures.pro +++ b/PineapplePictures.pro @@ -22,7 +22,7 @@ DEFINES += QT_DEPRECATED_WARNINGS # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 -CONFIG += c++11 +CONFIG += c++11 lrelease embed_translations SOURCES += \ main.cpp \ @@ -43,7 +43,9 @@ HEADERS += \ opacityhelper.h \ toolbutton.h -FORMS += +TRANSLATIONS = \ + languages/PineapplePictures.ts \ + languages/PineapplePictures_zh_CN.ts # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin diff --git a/appveyor.yml b/appveyor.yml index 21775e1..d3e7b1c 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -19,7 +19,7 @@ build_script: - mingw32-make - cd release - del /a /f /q "*.o" "*.cpp" "*.h" - - windeployqt --no-quick-import --release .\PineapplePictures.exe + - windeployqt --no-quick-import --no-translations --no-opengl-sw --no-system-d3d-compiler --release .\PineapplePictures.exe artifacts: - path: build\release diff --git a/graphicsscene.cpp b/graphicsscene.cpp index 5d73245..f741d09 100644 --- a/graphicsscene.cpp +++ b/graphicsscene.cpp @@ -12,7 +12,7 @@ GraphicsScene::GraphicsScene(QObject *parent) : QGraphicsScene(parent) { - showText("Drag image here"); + showText(tr("Drag image here")); } GraphicsScene::~GraphicsScene() diff --git a/graphicsview.cpp b/graphicsview.cpp index 05a089b..4339f37 100644 --- a/graphicsview.cpp +++ b/graphicsview.cpp @@ -29,7 +29,7 @@ void GraphicsView::showFromUrlList(const QList &urlList) emit navigatorViewRequired(false, 0); if (urlList.isEmpty()) { // yeah, it's possible. dragging QQ's original sticker will trigger this, for example. - showText("File url list is empty"); + showText(tr("File url list is empty")); return; } QUrl url(urlList.first()); @@ -43,7 +43,7 @@ void GraphicsView::showFromUrlList(const QList &urlList) QImageReader imageReader(filePath); QImage::Format imageFormat = imageReader.imageFormat(); if (imageFormat == QImage::Format_Invalid) { - showText("File is not a valid image"); + showText(tr("File is not a valid image")); } else { showImage(QPixmap::fromImageReader(&imageReader)); } @@ -218,7 +218,7 @@ void GraphicsView::dragEnterEvent(QDragEnterEvent *event) void GraphicsView::dragMoveEvent(QDragMoveEvent *event) { - Q_UNUSED(event); + Q_UNUSED(event) // by default, QGraphicsView/Scene will ignore the action if there are no QGraphicsItem under cursor. // We actually doesn't care and would like to keep the drag event as-is, so just do nothing here. } @@ -235,14 +235,14 @@ void GraphicsView::dropEvent(QDropEvent *event) QImage img = qvariant_cast(mimeData->imageData()); QPixmap pixmap = QPixmap::fromImage(img); if (pixmap.isNull()) { - showText("Image data is invalid"); + showText(tr("Image data is invalid")); } else { showImage(pixmap); } } else if (mimeData->hasText()) { showText(mimeData->text()); } else { - showText("Not supported mimedata: " + mimeData->formats().first()); + showText(tr("Not supported mimedata: %1").arg(mimeData->formats().first())); } } diff --git a/languages/PineapplePictures.ts b/languages/PineapplePictures.ts new file mode 100644 index 0000000..e6ef946 --- /dev/null +++ b/languages/PineapplePictures.ts @@ -0,0 +1,88 @@ + + + + + GraphicsScene + + + Drag image here + + + + + GraphicsView + + + File url list is empty + + + + + File is not a valid image + + + + + Image data is invalid + + + + + Not supported mimedata: %1 + + + + + MainWindow + + + + Stay on top + + + + + + Protected mode + + + + + Help + + + + + Launch application with image file path as argument to load the file. + + + + + Drag and drop image file onto the window is also supported. + + + + + Context menu option explanation: + + + + + Make window stay on top of all other windows. + + + + + Avoid close window accidentally. (eg. by double clicking the window) + + + + + main + + + File list. + + + + diff --git a/languages/PineapplePictures_zh_CN.ts b/languages/PineapplePictures_zh_CN.ts new file mode 100644 index 0000000..e246f70 --- /dev/null +++ b/languages/PineapplePictures_zh_CN.ts @@ -0,0 +1,88 @@ + + + + + GraphicsScene + + + Drag image here + 拖放图片至此 + + + + GraphicsView + + + File url list is empty + 文件 URL 列表为空 + + + + File is not a valid image + 文件不是有效的图片文件 + + + + Image data is invalid + 图片数据无效 + + + + Not supported mimedata: %1 + 不受支持的 MimeData 格式:%1 + + + + MainWindow + + + + Stay on top + 总在最前 + + + + + Protected mode + 保护模式 + + + + Help + 帮助 + + + + Launch application with image file path as argument to load the file. + 以图片文件的路径作为参数运行程序即可直接打开图片文件。 + + + + Drag and drop image file onto the window is also supported. + 也支持拖放图片文件到窗口内来加载图片。 + + + + Context menu option explanation: + 菜单项说明: + + + + Make window stay on top of all other windows. + 使窗口始终至于其它非置顶窗口上方。 + + + + Avoid close window accidentally. (eg. by double clicking the window) + 避免窗口意外关闭。(如:不小心双击了窗口触发了关闭窗口行为) + + + + main + + + File list. + 文件列表。 + + + diff --git a/main.cpp b/main.cpp index ba68d8b..7483ba4 100644 --- a/main.cpp +++ b/main.cpp @@ -1,15 +1,22 @@ #include "mainwindow.h" #include #include +#include #include int main(int argc, char *argv[]) { QApplication a(argc, argv); + // since we did `CONFIG += lrelease embed_translations`... + QTranslator translator; + translator.load(QString("PineapplePictures_%1").arg(QLocale::system().name()), ":/i18n/"); + a.installTranslator(&translator); + // parse commandline arguments QCommandLineParser parser; parser.addPositionalArgument("File list", QCoreApplication::translate("main", "File list.")); + parser.addHelpOption(); parser.process(a); diff --git a/mainwindow.cpp b/mainwindow.cpp index e95cd2d..3d5a821 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -238,8 +238,22 @@ void MainWindow::contextMenuEvent(QContextMenuEvent *event) }); protectedMode->setCheckable(true); protectedMode->setChecked(m_protectedMode); + QAction * helpAction = new QAction(tr("Help")); + connect(helpAction, &QAction::triggered, this, [ = ](){ + QStringList sl { + tr("Launch application with image file path as argument to load the file."), + tr("Drag and drop image file onto the window is also supported."), + "", + tr("Context menu option explanation:"), + (tr("Stay on top") + " : " + tr("Make window stay on top of all other windows.")), + (tr("Protected mode") + " : " + tr("Avoid close window accidentally. (eg. by double clicking the window)")) + }; + m_graphicsView->showText(sl.join('\n')); + }); menu->addAction(stayOnTopMode); menu->addAction(protectedMode); + menu->addSeparator(); + menu->addAction(helpAction); menu->exec(mapToGlobal(event->pos())); menu->deleteLater(); @@ -372,6 +386,7 @@ void MainWindow::toggleProtectedMode() void MainWindow::toggleStayOnTop() { setWindowFlag(Qt::WindowStaysOnTopHint, !stayOnTop()); + show(); } bool MainWindow::stayOnTop()