My project folder is currently setup like this:

   -root
   ---[source files]
   ---Translations
   ------app_en.ts
   ------app_en.qm
   ------app_fr.ts
   ------app_fr.qm
   ------app_jp.ts
   ------app_jp.qm

My .pro file:

#-------------------------------------------------
#
# Project created by QtCreator 2014-03-18T15:15:40
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = TestApp
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp
TRANSLATIONS=Translations/app_en.ts\
             Translations/app_fr.ts\
             Translations/app_jp.ts
CODECFORSRC   = UTF-8

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

In my main.cpp I try to force the translation to JP:

#include "mainwindow.h"
#include <QApplication>
#include <QTranslator>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTranslator translator;
    translator.load("app_jp");
    a.installTranslator(&translator);
    MainWindow w;
    w.show();

    return a.exec();
}

But the main window still appears in English. Maybe I'm misunderstanding the documentation, but how do we force a certain local for the application? My build output folder has the .qm files in it (I copied them in the root of the build output folder)

有帮助吗?

解决方案

probably you provided translation but it is not marked as finished. Check app_jp.ts (it is a xml) you will have entries with type="unfinished". Clear this flag. Probably you can do this also from Qt Linguist tool (I don't remember where is this option).

Check this tutorial.

Also add some logs (to verify that translator was properly loaded):

bool translatorLoaded = translator.load("app_jp");
if (!translatorLoaded)
    qWarrning("Problem loading translation file");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top