LanguageWidget.cpp 3.21 KB
Newer Older
1 2 3
/*****************************************************************************
 * LanguageWidget.h: Handle languge settings
 *****************************************************************************
4
 * Copyright (C) 2008-2016 VideoLAN
5
 *
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
 *
 * 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.
 *****************************************************************************/

23 24 25 26
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

27
#include "LanguageWidget.h"
28
#include "Settings/Settings.h"
29 30
#include "Settings/SettingValue.h"
#include "Tools/VlmcDebug.h"
31 32 33 34 35 36 37 38

#include <QApplication>
#include <QComboBox>
#include <QDir>
#include <QTranslator>

#define TS_PREFIX "vlmc_"

Hugo Beauzée-Luyssen's avatar
Hugo Beauzée-Luyssen committed
39
LanguageWidget::LanguageWidget( SettingValue *s, QWidget *parent /*= nullptr*/ ) :
40
        ISettingsCategoryWidget( parent, s )
41
{
42
    m_list = new QComboBox( this );
43 44 45 46 47 48 49 50 51 52
    QDir dir( ":/ts/", "*.qm", QDir::Name | QDir::IgnoreCase, QDir::Files );

    foreach ( const QString& tsFileName, dir.entryList() )
    {
        QString     countryCode;
        int         localePos = tsFileName.lastIndexOf( TS_PREFIX );
        int         extPos = tsFileName.lastIndexOf( ".qm" );

        if ( localePos < 0 || extPos < 0 || localePos > extPos )
        {
53
            vlmcWarning() << "Invalid translation file:" << tsFileName;
54 55 56 57 58 59 60 61 62 63
            continue ;
        }

        localePos += qstrlen( TS_PREFIX );
        countryCode = tsFileName.mid( localePos, extPos - localePos );
        QLocale     locale( countryCode );

        // Check if the country code is valid ISO 639
        if ( locale.language() == QLocale::C )
        {
64
            vlmcWarning() << "Invalid locale for file:" << tsFileName;
65 66 67 68 69 70 71
            continue;
        }

        m_list->addItem( QString( "%1 (%2)" ).arg(
                QLocale::languageToString( locale.language() ),
                QLocale::countryToString( locale.country() ) ), countryCode );
    }
72 73
    // Add the built-in us_US locale.
    m_list->addItem( "English (UnitedStates)", "en_US" );
74 75 76

    // Sort the combobox
    m_list->model()->sort( 0 );
77 78 79

    // Add the system default option (auto-detection of the locale)
    m_list->insertItem( 0, "System Locale (autodetect)", "default" );
80
    layout()->addWidget( m_list );
81
    changed( s->get() );
82 83
}

84
bool
85 86 87 88
LanguageWidget::save()
{
    QString     lang = m_list->itemData( m_list->currentIndex() ).toString();
    m_setting->set( lang );
89
    return true;
90
}
91 92 93 94 95 96 97 98

void
LanguageWidget::changed( const QVariant &val )
{
    int idx = m_list->findData( val.toString() );
    if ( idx != -1 )
        m_list->setCurrentIndex( idx );
}