src/hbcore/i18n/hblanguageutil.cpp
changeset 30 80e4d18b72f5
parent 28 b7da29130b0e
equal deleted inserted replaced
28:b7da29130b0e 30:80e4d18b72f5
     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_r.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_OLD"
       
    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     \deprecated HbLanguageUtil class
       
    61         is deprecated. Please use HbLocaleUtil class instead.
       
    62 */
       
    63 
       
    64 #if defined(Q_OS_SYMBIAN)
       
    65 /*!
       
    66     \brief Returns identifiers and localized names of all known languages.
       
    67       
       
    68     \return localized names and integer identifiers of languages supported in a device  
       
    69 */
       
    70 QHash<int, QString> readLanguageList()
       
    71 {
       
    72     QHash<int, QString> hashLanguages;
       
    73     QString path = "c:";
       
    74     path += QString(LANGUAGE_LIST_FILE);
       
    75     QFile* file = new QFile(path);
       
    76     if (!file->exists() ) {
       
    77         path = "z:";
       
    78         path += QString(LANGUAGE_LIST_FILE);
       
    79         delete file;
       
    80         file = new QFile(path);
       
    81     }
       
    82     if (!file->open(QIODevice::ReadOnly | QIODevice::Text)) {
       
    83         delete file;
       
    84         return hashLanguages;
       
    85     }
       
    86     QTextStream in(file);
       
    87     while (!in.atEnd()) {
       
    88         QString line = in.readLine(256);
       
    89         if (!line.isEmpty()) {
       
    90             int sep = line.indexOf(',');
       
    91             QString strCode = line.left(sep);
       
    92             QString name = line.mid(sep+1);
       
    93 
       
    94             bool ok;
       
    95             int code = strCode.toUInt(&ok);
       
    96             if (!ok) {
       
    97                 continue;
       
    98             }
       
    99             hashLanguages.insert(code, name);
       
   100         }
       
   101     }
       
   102     delete file;
       
   103     return hashLanguages;
       
   104 }
       
   105 #endif // Q_OS_SYMBIAN
       
   106 
       
   107 #if defined(Q_OS_SYMBIAN)
       
   108 
       
   109 /*!
       
   110     \brief Changes the system UI language.
       
   111       
       
   112     \param language identifier of the language  
       
   113     \return true if operation was successful
       
   114 */
       
   115 bool setLocale( int language )
       
   116 {
       
   117     TExtendedLocale dummy;
       
   118     dummy.LoadSystemSettings();
       
   119     QString no;
       
   120     no = QString( "%1" ).arg( language, 2, 10, QLatin1Char( '0' ) );
       
   121     QString name = QString( "elocl." ).append( no );
       
   122     TPtrC nameptr(name.utf16());
       
   123     
       
   124     TInt err = dummy.LoadLocale( nameptr );
       
   125     if( err != KErrNone )
       
   126         return false;
       
   127     dummy.SaveSystemSettings();
       
   128     // cause localeprivate update on next qlocale object( glp->m_language_id = 0 )
       
   129     QSystemLocale dummy2;
       
   130     return true;
       
   131 }
       
   132 #endif // Q_OS_SYMBIAN
       
   133 
       
   134 
       
   135 /*!
       
   136     \brief Returns names and identifiers of supported languages in a phone.
       
   137  
       
   138     Language names are localized according the language's native presentation.
       
   139     Language ID's returned by this functions may be used as language parameter for changeLanguage(int language) function.
       
   140     Language IDs and names are OS specific and may vary across the platforms and releases.
       
   141     
       
   142     \attention Symbian specific API
       
   143     
       
   144     \deprecated HbLanguageUtil::supportedLanguages()
       
   145         is deprecated. Please use HbLocaleUtil::supportedLanguages() instead.
       
   146      
       
   147     \return Symbian - localized names and integer identifiers of languages supported in a device  
       
   148     \return other platforms - empty QHash    
       
   149 */
       
   150 QHash<int, QString> HbLanguageUtil::supportedLanguages()
       
   151 {
       
   152 #if defined(Q_OS_SYMBIAN)   
       
   153     QHash<int, QString> languages; 
       
   154     
       
   155     QTranslator translator;
       
   156     QString path = "c:";
       
   157     path += QString(TRANSLATOR_PATH);
       
   158     if (!translator.load(path)) {
       
   159         path = "z:";
       
   160         path += QString(TRANSLATOR_PATH);
       
   161         if (!translator.load(path)) {
       
   162             return languages;
       
   163         } 
       
   164     } 
       
   165 
       
   166     QCoreApplication::installTranslator(&translator);
       
   167     QHash<int, QString> hashLanguageNames = readLanguageList();
       
   168  
       
   169     CArrayFixFlat<TInt>* systemEpocLanguageCodes = 0;
       
   170     TInt error = SysLangUtil::GetInstalledLanguages( systemEpocLanguageCodes );
       
   171     if ( error != KErrNone ) {
       
   172         delete systemEpocLanguageCodes;
       
   173         return languages;
       
   174     }
       
   175     
       
   176     for (int i = 0; i < systemEpocLanguageCodes->Count(); ++i) {
       
   177         int code = systemEpocLanguageCodes->At(i);
       
   178         QString id = QString(LANGUAGE_ID_PREFIX);
       
   179         id += QString::number(code);
       
   180         QString locName = hbTrId(id.toAscii().constData());
       
   181         if (locName.isEmpty() || locName == id) {
       
   182             locName = hashLanguageNames.value(code);
       
   183         }
       
   184         languages.insert(code, locName);
       
   185     }
       
   186     
       
   187     delete systemEpocLanguageCodes;
       
   188     return languages;
       
   189 #else 
       
   190     QHash<int, QString> dummy;
       
   191     return dummy;
       
   192 #endif
       
   193 }
       
   194 
       
   195 /*!
       
   196     \brief Returns names and identifiers of all known languages.
       
   197  
       
   198     Language names are localized according the language's native presentation.
       
   199     Language ID's returned by this functions may be used as language parameter for changeLanguage(int language) function.
       
   200     Language IDs and names are OS specific and may vary across the platforms and releases.
       
   201      
       
   202     \attention Symbian specific API
       
   203      
       
   204     \deprecated HbLanguageUtil::allLanguages()
       
   205         is deprecated.
       
   206 
       
   207     \return Symbian - localized names and integer identifiers of known languages 
       
   208     \return other platforms - empty QHash    
       
   209 */
       
   210 QHash<int, QString> HbLanguageUtil::allLanguages()
       
   211 {
       
   212 #if defined(Q_OS_SYMBIAN)  
       
   213     QHash<int, QString> langs; 
       
   214     
       
   215     QTranslator translator;
       
   216     QString path = "c:";
       
   217     path += QString(TRANSLATOR_PATH);
       
   218     if (!translator.load(path)) {
       
   219         path = "z:";
       
   220         path += QString(TRANSLATOR_PATH);
       
   221         if (!translator.load(path)) {
       
   222             return langs;
       
   223         } 
       
   224     } 
       
   225 
       
   226     QCoreApplication::installTranslator(&translator);
       
   227     
       
   228     QHash<int, QString> languageNameList = readLanguageList();
       
   229     QHashIterator<int, QString> i(languageNameList);
       
   230     while (i.hasNext()) {
       
   231         i.next();
       
   232         int code = i.key();
       
   233         QString id = QString(LANGUAGE_ID_PREFIX);
       
   234         id += QString::number(code);
       
   235         QString locName = hbTrId(id.toAscii().constData());
       
   236         if (locName.isEmpty()) {
       
   237             locName = i.value();
       
   238         }
       
   239         langs.insert(code, locName);
       
   240     }
       
   241     return langs;
       
   242 #else 
       
   243     QHash<int, QString> dummy;
       
   244     return dummy;
       
   245 #endif
       
   246 }
       
   247 
       
   248 /*!
       
   249     \brief Changes the device system language.  
       
   250      
       
   251     \attention Symbian specific API
       
   252      
       
   253     \deprecated HbLanguageUtil::changeLanguage( int language )
       
   254         is deprecated. Please use HbLocaleUtil::changeLanguage( const QString &language ) instead.
       
   255 
       
   256     \param language identifier of language to set active
       
   257     \return true for Symbian if succesfull and false for other platforms
       
   258 */ 
       
   259 bool HbLanguageUtil::changeLanguage( int language )
       
   260 {
       
   261 #if defined(Q_OS_SYMBIAN)
       
   262     if ( !HbFeatureManager::instance()->featureStatus(HbFeatureManager::LanguageSwitch) ) {
       
   263         return false;
       
   264     }
       
   265     
       
   266     CRepository* commonEngineRepository = 0;
       
   267     TRAPD( err1, commonEngineRepository = CRepository::NewL( KCRUidCommonEngineKeys ) );    
       
   268     if ( err1 != KErrNone ) { 
       
   269         return false;
       
   270     }
       
   271     
       
   272     if (!setLocale(language)) {
       
   273             delete commonEngineRepository;
       
   274         return false;
       
   275     }
       
   276         
       
   277     // Never set Language code 0 to HAL
       
   278     if ( language !=0 ) {
       
   279         if ( HAL::Set( HAL::ELanguageIndex, language ) != KErrNone ) {
       
   280             delete commonEngineRepository;
       
   281             return false;
       
   282         }
       
   283     }
       
   284     if ( commonEngineRepository->Set( KGSDisplayTxtLang, language ) != KErrNone ) {
       
   285         delete commonEngineRepository;
       
   286         return false;
       
   287     }
       
   288     delete commonEngineRepository;
       
   289     return true;
       
   290 
       
   291 #else
       
   292     Q_UNUSED(language);
       
   293     return false;
       
   294 #endif
       
   295 }
       
   296 
       
   297 /*!
       
   298     \brief Returns ID of current language. 
       
   299   
       
   300     \attention Symbian specific API
       
   301      
       
   302     \deprecated HbLanguageUtil::currentLanguage()
       
   303         is deprecated. Please use HbLocaleUtil::currentLanguage() instead.
       
   304 
       
   305     \return identifier of current system language for Symbian and '0' for other platforms
       
   306 */ 
       
   307 int HbLanguageUtil::currentLanguage()
       
   308 {
       
   309     #if defined(Q_OS_SYMBIAN)
       
   310         TLanguage l = User::Language();
       
   311         return l;
       
   312     #else 
       
   313         return 0;
       
   314     #endif
       
   315 }
       
   316