LanguageWidget.cpp 3.16 KB
Newer Older
1 2 3
/*****************************************************************************
 * LanguageWidget.h: Handle languge settings
 *****************************************************************************
4
 * Copyright (C) 2008-2014 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 26
 *
 * 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 "config.h"
#include "LanguageWidget.h"
#include "SettingsManager.h"
#include "SettingValue.h"
27
#include "VlmcDebug.h"
28 29 30 31 32 33 34 35 36

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

#define TS_PREFIX "vlmc_"

LanguageWidget::LanguageWidget( SettingValue *s, QWidget *parent /*= NULL*/ ) :
37
        ISettingsCategoryWidget( parent, s )
38
{
39
    m_list = new QComboBox( this );
40 41 42 43 44 45 46 47 48 49
    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 )
        {
50
            vlmcWarning() << "Invalid translation file:" << tsFileName;
51 52 53 54 55 56 57 58 59 60
            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 )
        {
61
            vlmcWarning() << "Invalid locale for file:" << tsFileName;
62 63 64 65 66 67 68
            continue;
        }

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

    // Sort the combobox
    m_list->model()->sort( 0 );
74 75 76

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

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

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