src/hbcore/i18n/hblanguageutil.cpp
changeset 1 f7ac710697a9
child 2 06ff229162e9
equal deleted inserted replaced
0:16d8024aca5e 1:f7ac710697a9
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbCore module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 
       
    26 #include <QFile>
       
    27 #include <QLocale>
       
    28 #include <QTimer>
       
    29 #include <QHash>
       
    30 #include <QHashIterator>
       
    31 #include <QTextStream>
       
    32 #include <QTranslator>
       
    33 #include <QTextCodec>
       
    34 #include <QCoreApplication>
       
    35 
       
    36 #if defined(Q_OS_SYMBIAN)
       
    37 #include <e32lang.h>
       
    38 #include <e32property.h>
       
    39 #include <centralrepository.h> 
       
    40 #include <hal.h>
       
    41 #include <syslangutil.h>
       
    42 #include <CommonEngineDomainCRKeys.h> //Ui language
       
    43 #endif // Q_OS_SYMBIAN
       
    44 
       
    45 #include "hblanguageutil.h"
       
    46 #include "hbfeaturemanager_p.h"
       
    47 
       
    48 #if defined(Q_OS_SYMBIAN)
       
    49 #define LANGUAGE_LIST_FILE "/resource/hbi18n/translations/language_list.txt"
       
    50 #define LANGUAGE_ID_PREFIX "language_"
       
    51 #define TRANSLATOR_PATH "/resource/hbi18n/translations/languages"
       
    52 #endif // Q_OS_SYMBIAN
       
    53 
       
    54 /*!
       
    55     @beta
       
    56     @hbcore
       
    57     \class HbLanguageUtil
       
    58     \brief HbLanguageUtil provides functions for quering supported languages and switching the system language.
       
    59 */
       
    60 
       
    61 #if defined(Q_OS_SYMBIAN)
       
    62 /*!
       
    63     \brief Returns identifiers and localized names of all known languages.
       
    64       
       
    65     \return Localized names and integer identifiers of languages supported in a device  
       
    66 */
       
    67 QHash<int, QString> readLanguageList()
       
    68 {
       
    69     QHash<int, QString> hashLanguages;
       
    70     QString path = "c:";
       
    71     path += QString(LANGUAGE_LIST_FILE);
       
    72     QFile* file = new QFile(path);
       
    73     if (!file->exists() ) {
       
    74         path = "z:";
       
    75         path += QString(LANGUAGE_LIST_FILE);
       
    76         delete file;
       
    77         file = new QFile(path);
       
    78     }
       
    79     if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) {
       
    80         delete file;
       
    81         return hashLanguages;
       
    82     }
       
    83     QTextStream in(file);
       
    84     while (!in.atEnd()) {
       
    85         QString line = in.readLine(256);
       
    86         if (!line.isEmpty()) {
       
    87             int sep = line.indexOf(',');
       
    88             QString strCode = line.left(sep);
       
    89             QString name = line.mid(sep+1);
       
    90 
       
    91             bool ok;
       
    92             int code = strCode.toUInt(&ok);
       
    93             if (!ok) {
       
    94                 continue;
       
    95             }
       
    96             hashLanguages.insert(code, name);
       
    97         }
       
    98     }
       
    99     delete file;
       
   100     return hashLanguages;
       
   101 }
       
   102 #endif // Q_OS_SYMBIAN
       
   103 
       
   104 #if defined(Q_OS_SYMBIAN)
       
   105 
       
   106 /*!
       
   107     \brief Changes the system UI language.
       
   108       
       
   109     \param language identifier of the language  
       
   110 */
       
   111 bool setLocale( int language )
       
   112 {
       
   113     TExtendedLocale dummy;
       
   114     QString no;
       
   115     no = QString( "%1" ).arg( language, 2, 10, QLatin1Char( '0' ) );
       
   116     QString name = QString( "elocl." ).append( no );
       
   117     TPtrC nameptr(name.utf16());
       
   118     
       
   119     TInt err = dummy.LoadLocale( nameptr );
       
   120     if( err != KErrNone )
       
   121         return false;
       
   122     dummy.SaveSystemSettings();
       
   123     // cause localeprivate update on next qlocale object( glp->m_language_id = 0 )
       
   124     QSystemLocale dummy2;
       
   125     return true;
       
   126 }
       
   127 #endif // Q_OS_SYMBIAN
       
   128 
       
   129 
       
   130 /*!
       
   131     \brief Returns names and identifiers of supported languages in a phone.
       
   132  
       
   133     Language names are localized according the language's native presentation.
       
   134     Language ID's returned by this functions may be used as language parameter for changeLanguage(int language) function.
       
   135     Language IDs and names are OS specific and may vary across the platforms and releases.
       
   136      
       
   137     \return Localized names and integer identifiers of languages supported in a device  
       
   138 */
       
   139 QHash<int, QString> HbLanguageUtil::supportedLanguages()
       
   140 {
       
   141 #if defined(Q_OS_SYMBIAN)   
       
   142     QHash<int, QString> languages; 
       
   143     
       
   144     QTranslator translator;
       
   145     if (!translator.load(TRANSLATOR_PATH)) {
       
   146         return languages;
       
   147     } 
       
   148     QCoreApplication::installTranslator(&translator);
       
   149     QHash<int, QString> hashLanguageNames = readLanguageList();
       
   150  
       
   151     CArrayFixFlat<TInt>* systemEpocLanguageCodes = 0;
       
   152     TInt error = SysLangUtil::GetInstalledLanguages( systemEpocLanguageCodes );
       
   153     if ( error != KErrNone ) {
       
   154         delete systemEpocLanguageCodes;
       
   155         return languages;
       
   156     }
       
   157     
       
   158     for (int i = 0; i < systemEpocLanguageCodes->Count(); ++i) {
       
   159         int code = systemEpocLanguageCodes->At(i);
       
   160         QString id = QString(LANGUAGE_ID_PREFIX);
       
   161         id += QString::number(code);
       
   162         QString locName = hbTrId(id.toAscii().constData());
       
   163         if (locName.isEmpty() || locName == id) {
       
   164             locName = hashLanguageNames.value(code);
       
   165         }
       
   166         languages.insert(code, locName);
       
   167     }
       
   168     
       
   169     delete systemEpocLanguageCodes;
       
   170     return languages;
       
   171 #else 
       
   172     QHash<int, QString> dummy;
       
   173     return dummy;
       
   174 #endif
       
   175 }
       
   176 
       
   177 /*!
       
   178     \brief Returns names and identifiers of all known languages.
       
   179  
       
   180     Language names are localized according the language's native presentation.
       
   181     Language ID's returned by this functions may be used as language parameter for changeLanguage(int language) function.
       
   182     Language IDs and names are OS specific and may vary across the platforms and releases.
       
   183      
       
   184     \return Localized names and integer identifiers of known languages 
       
   185 */
       
   186 QHash<int, QString> HbLanguageUtil::allLanguages()
       
   187 {
       
   188 #if defined(Q_OS_SYMBIAN)  
       
   189     QHash<int, QString> langs; 
       
   190     
       
   191     QTranslator translator;
       
   192     if (!translator.load(TRANSLATOR_PATH)) {
       
   193         return langs;
       
   194     } 
       
   195     QCoreApplication::installTranslator(&translator);
       
   196     
       
   197     QHash<int, QString> languageNameList = readLanguageList();
       
   198     QHashIterator<int, QString> i(languageNameList);
       
   199     while (i.hasNext()) {
       
   200         i.next();
       
   201         int code = i.key();
       
   202         QString id = QString(LANGUAGE_ID_PREFIX);
       
   203         id += QString::number(code);
       
   204         QString locName = hbTrId(id.toAscii().constData());
       
   205         if (locName.isEmpty()) {
       
   206             locName = i.value();
       
   207         }
       
   208         langs.insert(code, locName);
       
   209     }
       
   210     return langs;
       
   211 #else 
       
   212     QHash<int, QString> dummy;
       
   213     return dummy;
       
   214 #endif
       
   215 }
       
   216 
       
   217 /*!
       
   218     \brief Changes the device system language.  
       
   219   
       
   220     \param identifier of language to set active
       
   221     \return true if language change was successful
       
   222 */ 
       
   223 bool HbLanguageUtil::changeLanguage( int language )
       
   224 {
       
   225 #if defined(Q_OS_SYMBIAN)
       
   226     if ( !HbFeatureManager::instance()->featureStatus(HbFeatureManager::LanguageSwitch) ) {
       
   227         return false;
       
   228     }
       
   229     
       
   230     CRepository* commonEngineRepository = 0;
       
   231     TRAPD( err1, commonEngineRepository = CRepository::NewL( KCRUidCommonEngineKeys ) );    
       
   232     if ( err1 != KErrNone ) { 
       
   233         return false;
       
   234     }
       
   235     
       
   236     if (!setLocale(language)) {
       
   237     		delete commonEngineRepository;
       
   238         return false;
       
   239     }
       
   240         
       
   241     // Never set Language code 0 to HAL
       
   242     if ( language !=0 ) {
       
   243         if ( HAL::Set( HAL::ELanguageIndex, language ) != KErrNone ) {
       
   244             delete commonEngineRepository;
       
   245             return false;
       
   246         }
       
   247     }
       
   248     if ( commonEngineRepository->Set( KGSDisplayTxtLang, language ) != KErrNone ) {
       
   249         delete commonEngineRepository;
       
   250         return false;
       
   251     }
       
   252     delete commonEngineRepository;
       
   253     return true;
       
   254 
       
   255 #else
       
   256     Q_UNUSED(language);
       
   257     return false;
       
   258 #endif
       
   259 }
       
   260 
       
   261 /*!
       
   262     \brief Returns ID of current language. Localized name 
       
   263   
       
   264     \return identifier of current system language
       
   265 */ 
       
   266 int HbLanguageUtil::currentLanguage()
       
   267 {
       
   268     #if defined(Q_OS_SYMBIAN)
       
   269         TLanguage l = User::Language();
       
   270         return l;
       
   271     #else 
       
   272         return 0;
       
   273     #endif
       
   274 }
       
   275