LanguageHelper.cpp 3 KB
Newer Older
1 2 3
/*****************************************************************************
 * Languagehelper.cpp: Watch for language changes
 *****************************************************************************
4
 * Copyright (C) 2008-2016 VideoLAN
5
 *
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
6
 * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#include "LanguageHelper.h"

#include <QApplication>
26 27
#include <QLibraryInfo>
#include <QLocale>
28 29 30 31 32
#include <QTranslator>
#include <QVariant>

#define TS_PREFIX "vlmc_"

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
33
LanguageHelper::LanguageHelper() : m_translator( nullptr ), m_qtTranslator( nullptr )
34 35 36 37 38 39 40 41
{
    connect( qApp, SIGNAL( aboutToQuit() ), this, SLOT( deleteLater() ) );
}

LanguageHelper::~LanguageHelper()
{
    if ( m_translator )
        delete m_translator;
42 43
    if ( m_qtTranslator )
        delete m_qtTranslator;
44 45 46 47 48
}

void
LanguageHelper::languageChanged( const QVariant &vLang )
{
49 50 51 52 53 54
    languageChanged( vLang.toString() );
}

void
LanguageHelper::languageChanged( const QString &lang  )
{
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
55
    if ( m_translator != nullptr ||  m_qtTranslator != nullptr )
56
    {
57
        qApp->removeTranslator( m_translator );
58
        qApp->removeTranslator( m_qtTranslator );
59
        delete m_translator;
60
        delete m_qtTranslator;
Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
61 62
        m_translator = nullptr;
        m_qtTranslator = nullptr;
63
    }
64 65

    m_translator = new QTranslator();
66
    m_qtTranslator = new QTranslator();
67 68

    if ( lang.isEmpty() || lang == "default" )
69
    {
70
        m_translator->load( TS_PREFIX + QLocale::system().name(), ":/ts/" );
71
        m_qtTranslator->load( "qt_" + QLocale::system().name(),
72
#if defined( Q_OS_WIN32 ) || defined( Q_OS_MAC )
73 74
                              qApp->applicationDirPath() + "/ts/" );
#else
75
                              QLibraryInfo::location( QLibraryInfo::TranslationsPath ) );
76
#endif
77
    }
78
    else
79
    {
80
        m_translator->load( TS_PREFIX + lang, ":/ts/" );
81
        m_qtTranslator->load( "qt_" + lang,
82
#if defined( Q_OS_WIN32 ) || defined( Q_OS_MAC )
83 84
                              qApp->applicationDirPath() + "/ts/" );
#else
85
                              QLibraryInfo::location( QLibraryInfo::TranslationsPath ) );
86
#endif
87
    }
88

89 90
    qApp->installTranslator( m_translator );   // For translating VLMC UI strings
    qApp->installTranslator( m_qtTranslator ); // For translating Qt's dialog buttons etc.
91
}