src/hbcore/i18n/hblocaleutil.cpp
changeset 34 ed14f46c0e55
parent 7 923ff622b8b9
equal deleted inserted replaced
31:7516d6d86cf5 34:ed14f46c0e55
    45 
    45 
    46 #include <hbglobal.h>
    46 #include <hbglobal.h>
    47 #include <hblocaleutil.h>
    47 #include <hblocaleutil.h>
    48 
    48 
    49 #if defined(Q_OS_SYMBIAN)
    49 #if defined(Q_OS_SYMBIAN)
    50 #define LANGUAGE_LIST_FILE "/resource/hbi18n/translations/language_list.txt"
    50 #define LANGUAGE_LIST_FILE "/resource/hb/translations/language_list.txt"
    51 #define LANGUAGE_MAPPINGS_FILE "/resource/hbi18n/translations/locale_mappings.txt"
    51 #define LANGUAGE_MAPPINGS_FILE "/resource/hb/translations/locale_mappings.txt"
    52 #define LANGUAGE_ID_PREFIX "language"
    52 #define LANGUAGE_TRANSLATOR_PATH "/resource/hb/translations/languages"
    53 #define LANGUAGE_TRANSLATOR_PATH "/resource/hbi18n/translations/languages"
    53 #define REGION_TRANSLATOR_PATH "/resource/hb/translations/regions"
    54 
    54 #define COLLATION_TRANSLATOR_PATH "/resource/hb/translations/collations"
    55 #define REGION_LIST_FILE "z:/resource/bootdata/regions.txt"
    55 
    56 #define REGION_ID_PREFIX "region"
    56 #define LANGUAGE_ID_PREFIX "txt_language"
    57 #define REGION_TRANSLATOR_PATH "/resource/hbi18n/translations/regions"
    57 #define REGION_ID_PREFIX "txt_region"
    58 #define REGION_DLL_PREFIX "elocl_reg."
    58 #define COLLATION_ID_PREFIX "txt_collation"
    59 
    59 
    60 #define COLLATION_LIST_FILE "z:/resource/bootdata/collations.txt"
       
    61 #define COLLATION_ID_PREFIX "collation"
       
    62 #define COLLATION_TRANSLATOR_PATH "/resource/hbi18n/translations/collations"
       
    63 #define COLLATION_DLL_PREFIX "elocl_col."
       
    64 #define COLLATION_DLL_PREFIX_POSITION 3
    60 #define COLLATION_DLL_PREFIX_POSITION 3
    65 #endif // Q_OS_SYMBIAN
    61 #endif // Q_OS_SYMBIAN
    66 
    62 
    67 /*!
    63 /*!
    68     @beta
    64     @beta
    69     @hbcore
    65     @hbcore
    70     \class HbLocaleUtil
    66     \class HbLocaleUtil
    71     \brief HbLocaleUtil provides functions for quering supported languages, regions and collations and activing them.
    67     \brief The HbLocaleUtil class supports querying the supported languages, regions and collations, and activating them.
    72      
    68      
    73     Language and collation identifiers typically corresponds with two-letter ISO 639 language code, but for certain languages and collations combination of ISO 639 language code and  ISO 3166 country code id used.
    69     With HbLocaleUtil, you can query the supported languages, regions and 
    74     Region identifiers always corresponds with two-letter ISO 3166 country code.
    70     collations from the device, and the ones currently set in the device. You 
    75     
    71     can then use this data to activate the language, region and collation in the 
    76     HbLocaleUtil also provides functions for converting language, region and collation identifiers to their localised names. 
    72     device; either individually, or as a set determined by the specified 
       
    73     language.
       
    74     
       
    75     HbLocaleUtil returns the data from the device as identifiers. The language 
       
    76     and collation identifiers typically correspond to two-letter ISO 639 
       
    77     language codes. However, some languages and collations use a combination of 
       
    78     an ISO 639 language code and an ISO 3166 country code, separated by an 
       
    79     underscore character. Region identifiers always correspond to a two-letter 
       
    80     ISO 3166 country code.
       
    81     
       
    82     HbLocaleUtil also provides functions for converting language, region and 
       
    83     collation identifiers to their localized names. 
       
    84     
       
    85     Example:
       
    86     \snippet{unittest_hblocaleutil/unittest_hblocaleutil.cpp,1}
       
    87     
       
    88     
       
    89     \sa HbStringUtil, HbExtendedLocale
       
    90     
    77 */
    91 */
    78 
    92 
    79 #if defined(Q_OS_SYMBIAN)
    93 #if defined(Q_OS_SYMBIAN)
    80 
    94 
    81 struct HbLocaleMapping
    95 struct HbLocaleMapping
    88     QString regName;
   102     QString regName;
    89     QString collName;
   103     QString collName;
    90 };
   104 };
    91 
   105 
    92 QList<HbLocaleMapping> mappingList;
   106 QList<HbLocaleMapping> mappingList;
    93 QList<int> regions;
       
    94 QStringList availRegions;
   107 QStringList availRegions;
    95 QHash<QString, QString> locRegionNames;
   108 QHash<QString, QString> locRegionNames;
    96 QList<int> collations;
       
    97 QStringList availCollations;
   109 QStringList availCollations;
    98 QHash<QString, QString> locCollationNames;
   110 QHash<QString, QString> locCollationNames;
    99 
   111 
   100 /*!
   112 /*!
   101     \brief Reads langauge, region and collation mappings.
   113     \relates HbLocaleUtil
       
   114     
       
   115     Reads language, region and collation mappings.
   102 */
   116 */
   103 void readMappings()
   117 void readMappings()
   104 {
   118 {
   105     QString path = "c:";
   119     QString path = "c:";
   106     path += QString(LANGUAGE_MAPPINGS_FILE);
   120     path += QString(LANGUAGE_MAPPINGS_FILE);
   151     delete file;
   165     delete file;
   152     return;
   166     return;
   153 }
   167 }
   154 #endif // Q_OS_SYMBIAN
   168 #endif // Q_OS_SYMBIAN
   155 
   169 
   156 #if defined(Q_OS_SYMBIAN)
   170 /*!
   157 
   171 
   158 /*!
   172     Returns the identifier of the current UI language. Typically the language 
   159     \brief Changes the system UI language.
   173     identifier is an ISO 639 language code, but in some cases, it is a 
   160       
   174     combination of an ISO 639 language code and an ISO 3166 country code. For 
   161     \param dllExtension extension of the locale dll
   175     example, for UK English, this returns "en", and for US English, this returns 
   162     \return true if operation was successful
   176     "en_US".
   163 */
   177     
   164 bool setLocale( const QString &dllExtension )
   178     \attention Only fully implemented on the Symbian platform. For other 
   165 {
   179     platforms, always returns an empty QString.
   166     TExtendedLocale dummy;
   180     
   167     dummy.LoadSystemSettings();
   181     \sa supportedLanguages(), localisedLanguageName(), changeLanguage(), languageRegionMappings()
   168     QString name = QString( "elocl_lan." ).append( dllExtension );
   182 
   169     TPtrC nameptr(name.utf16());
       
   170     
       
   171     TInt err = dummy.LoadLocaleAspect( nameptr );
       
   172     if( err != KErrNone )
       
   173         return false;
       
   174     dummy.SaveSystemSettings();
       
   175     // cause localeprivate update on next qlocale object( glp->m_language_id = 0 )
       
   176     QSystemLocale dummy2;
       
   177     return true;
       
   178 }
       
   179 #endif //Q_OS_SYMBIAN
       
   180 
       
   181 /*!
       
   182     \brief Return identifier of the current UI language.
       
   183     
       
   184     \attention Symbian specific API
       
   185 
       
   186     \return Identifier of the language code for Symbian and empty QString for other platforms.
       
   187 */ 
   183 */ 
   188 QString HbLocaleUtil::currentLanguage()
   184 QString HbLocaleUtil::currentLanguage()
   189 {
   185 {
   190 #if defined(Q_OS_SYMBIAN)
   186 #if defined(Q_OS_SYMBIAN)
   191     TLanguage l = User::Language();
   187     TLanguage l = User::Language();
   203 #endif
   199 #endif
   204    return QString();
   200    return QString();
   205 }
   201 }
   206 
   202 
   207 /*!
   203 /*!
   208     \brief Returns identifiers of languages supported in a device. 
   204     
   209     Language identifier may be two-letter ISO 639 language code or combination of ISO 639 language code and ISO 3166 country code 
   205     Returns the identifiers of the languages supported on the device. Typically 
   210     Ex: Great Britain english it returns "en".
   206     the language identifier is an ISO 639 language code, but in some cases, it 
   211     Ex: For U.S. english it returns "en_US"  
   207     is a combination of an ISO 639 language code and an ISO 3166 country code. 
   212     
   208     For example, for UK English, this returns "en", and for US English, this 
   213     \attention Symbian specific API
   209     returns "en_US".
   214     
   210     
   215     \return identifiers of supported languages for Symbian and empty QStringList for other platforms 
   211     \attention Only fully implemented on the Symbian platform. For other 
       
   212     platforms, always returns an empty QStringList.
       
   213     
       
   214     \sa localisedLanguageName(), currentLanguage(), changeLanguage(), changeLocale()
       
   215     
   216 */
   216 */
   217 QStringList HbLocaleUtil::supportedLanguages()
   217 QStringList HbLocaleUtil::supportedLanguages()
   218 {
   218 {
   219 #if defined(Q_OS_SYMBIAN)   
   219 #if defined(Q_OS_SYMBIAN)   
   220     QStringList languages; 
   220     QStringList languages; 
   246     return QStringList();
   246     return QStringList();
   247 #endif
   247 #endif
   248 }
   248 }
   249 
   249 
   250 /*!
   250 /*!
   251     \brief Converts two or five letter language identifier code to localised language name. 
   251     
   252     
   252     Returns the localised language name for the specified language identifier. 
   253     \attention Symbian specific API
   253     If the translation fails, returns an empty QString.
   254     
   254     
   255     \param language identifier 
   255     \param language The two- or five-letter language identifier that corresponds to one of the following:
   256     
   256     - An identifier returned by supportedLanguages() or currentLanguage()
   257     \return Symbian - localised name of the language, an empty String if translation fails
   257     - An HbLanguageRegionMapping::languageId string
   258     \return other platforms - empty QString    
   258     
       
   259     \attention Only fully implemented on the Symbian platform. For other 
       
   260     platforms, always returns an empty QString.
       
   261     
       
   262     \sa changeLanguage(), currentLanguage()
       
   263  
   259 */ 
   264 */ 
   260 QString HbLocaleUtil::localisedLanguageName( const QString &language )
   265 QString HbLocaleUtil::localisedLanguageName( const QString &language )
   261 {
   266 {
   262 #if defined(Q_OS_SYMBIAN)   
   267 #if defined(Q_OS_SYMBIAN)   
   263     QTranslator translator;
   268     QTranslator translator;
   285     return QString();
   290     return QString();
   286 #endif
   291 #endif
   287 }
   292 }
   288 
   293 
   289 /*!
   294 /*!
   290     \brief Changes the system language.  
   295 
   291     The language parameter should correspond with one of the identifiers returned by supportedLanguages(). 
   296     Changes the system language to the one specified in \a language.
   292     
       
   293     \attention Symbian specific API
       
   294   
   297   
   295     \param language identifier of language to set active
   298     \param language The language identifier. This must correspond to an 
   296     
   299     identifier returned by supportedLanguages(), or an 
   297     \return true if language change was successful and false for other platforms
   300     HbLanguageRegionMapping::languageId string.
       
   301     
       
   302     \return \c true if successful, otherwise \c false.
       
   303     
       
   304     \attention Only fully implemented on the Symbian platform. For other 
       
   305     platforms, always returns \c false.
       
   306     
       
   307     \sa localisedLanguageName(), currentLanguage()
       
   308     
   298 */ 
   309 */ 
   299 bool HbLocaleUtil::changeLanguage( const QString &language )
   310 bool HbLocaleUtil::changeLanguage( const QString &language )
   300 {
   311 {
   301 #if defined(Q_OS_SYMBIAN)
   312 #if defined(Q_OS_SYMBIAN)
   302     if(mappingList.isEmpty()) {
   313     if(mappingList.isEmpty()) {
   303         readMappings();
   314         readMappings();
   304     }
   315     }
   305 	
       
   306     int lanCode = -1;
   316     int lanCode = -1;
   307     QString dllExt = "";
       
   308     for (int i = 0;  i < mappingList.count(); ++i) {
   317     for (int i = 0;  i < mappingList.count(); ++i) {
   309         HbLocaleMapping map = mappingList.at(i);
   318         HbLocaleMapping map = mappingList.at(i);
   310         if (map.langName == language) {
   319         if (map.langName == language) {
       
   320             int retVal = -1;
   311             lanCode = map.symLangValue;
   321             lanCode = map.symLangValue;
   312             dllExt = map.languageDllId;
   322             retVal = SysLangUtil::ChangeLanguage(lanCode);
   313             break;
   323             if (!retVal) {
   314         }
   324                 // Never set Language code 0 to HAL
   315     }
   325                 if ( language != 0 ) {
   316     
   326                     HAL::Set( HAL::ELanguageIndex, lanCode );
   317     if (lanCode == -1) {
   327                 }
   318         return false;
   328                 return true;
   319     }
   329             }
   320     
   330             else {
   321     CRepository* commonEngineRepository = 0;
   331                 return false;
   322     TRAPD( err1, commonEngineRepository = CRepository::NewL( KCRUidCommonEngineKeys ) );    
   332             }
   323     if ( err1 != KErrNone ) { 
   333         }
   324         return false;
   334     }        
   325     }
   335     return false;
   326     
       
   327     if (!setLocale(dllExt)) {
       
   328         delete commonEngineRepository;
       
   329         return false;
       
   330     }
       
   331         
       
   332     // Never set Language code 0 to HAL
       
   333     if ( language !=0 ) {
       
   334         if ( HAL::Set( HAL::ELanguageIndex, lanCode ) != KErrNone ) {
       
   335             delete commonEngineRepository;
       
   336             return false;
       
   337         }
       
   338     }
       
   339     if ( commonEngineRepository->Set( KGSDisplayTxtLang, lanCode ) != KErrNone ) {
       
   340         delete commonEngineRepository;
       
   341         return false;
       
   342     }
       
   343     delete commonEngineRepository;
       
   344     return true;
       
   345 
       
   346 #else
   336 #else
   347     Q_UNUSED(language);
   337     Q_UNUSED(language);
   348     return false;
   338     return false;
   349 #endif
   339 #endif
   350 }
   340 }
   351 
   341 
   352 #if defined(Q_OS_SYMBIAN)  
   342 /*!
   353 /*!
   343     
   354     \brief reads the regions.txt file and reads the list of symbian region codes 
   344     Returns the identifiers of regions supported on the device. The region 
   355 */
   345     identifier is a two-letter ISO 3166 code, for example, "GB" for United 
   356 void readRegions()
   346     Kingdom.
   357 {
   347     
   358     QFile* file = new QFile(REGION_LIST_FILE);
   348     \attention Only fully implemented on the Symbian platform. For other 
   359     if (!file->exists() ) 
   349     platforms, always returns an empty QStringList.
   360     {
   350     
   361         delete file;
   351     \sa localisedRegionName(), currentRegion(), changeRegion()
   362         return;
   352 
   363     }
       
   364     if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
       
   365     {
       
   366         delete file;
       
   367         return;
       
   368     } 
       
   369     QTextStream in(file);
       
   370     while (!in.atEnd())
       
   371     {
       
   372         QString line = in.readLine(256);
       
   373         if (!line.isEmpty())
       
   374         {
       
   375             int regCode = line.toUInt();
       
   376             regions.append(regCode);
       
   377         }
       
   378     }
       
   379     return;
       
   380 }
       
   381 #endif
       
   382 
       
   383 /*!
       
   384     \brief Returns names supported regions in a phone. 
       
   385     Region names are identified by 2 letter code(ISO 3166 standard).
       
   386     Ex: For United Kingdom it returns GB 
       
   387     
       
   388     \attention Symbian specific API
       
   389 
       
   390     \return list of supported regions in a device for Symbian and empty QStringList for other platforms 
       
   391 */
   353 */
   392 QStringList HbLocaleUtil::supportedRegions()
   354 QStringList HbLocaleUtil::supportedRegions()
   393 {
   355 {
   394 #if defined(Q_OS_SYMBIAN)
   356 #if defined(Q_OS_SYMBIAN)
   395     if(!availRegions.isEmpty())
   357     if(!availRegions.isEmpty())
   396     {
   358     {
   397         return availRegions;
   359         return availRegions;
   398     }
   360     }
   399 
   361 
   400     if(regions.isEmpty())
   362     QStringList regions; 
   401     {
   363     CArrayFixFlat<TInt>* systemEpocRegionCodes = 0;
   402         readRegions();
   364     TInt error = SysLangUtil::GetInstalledRegions( systemEpocRegionCodes );
   403     }
   365     if ( error != KErrNone ) {
   404     
   366         delete systemEpocRegionCodes;
   405     if(mappingList.isEmpty())
   367         return regions;
   406     {
   368     }
   407         readMappings();
   369     
   408     }
   370     if(mappingList.isEmpty()) {
   409     int regCount = regions.count();
   371         readMappings();
   410     for(int i = 0; i < regCount; i++)
   372     }
   411     {
   373     
   412         int region = regions.at(i);
   374     for (int i = 0; i < systemEpocRegionCodes->Count(); ++i) {
   413         int count = mappingList.count();
   375         int code = systemEpocRegionCodes->At(i);
   414         for (int j = 0; j < count; j++)
   376         for (int j = 0; j < mappingList.count(); ++j) {
   415         {
   377             HbLocaleMapping map = mappingList.at(j);
   416             HbLocaleMapping mapping = mappingList.at(j);
   378             QString dllid = map.regionDllId;
   417             QString regCode = mapping.regionDllId;
   379             bool ok;
   418             if(region == regCode.toUInt())
   380             int symRegionValue = dllid.toInt(&ok,10);
   419             {
   381             if (symRegionValue == code) {
   420                 availRegions.append(mapping.regName);
   382                 availRegions.append(map.regName);
   421                 break;
   383                 break;
   422             }
   384             }
   423         }
   385         }
   424     }
   386     }
       
   387     
       
   388     delete systemEpocRegionCodes;
   425     return availRegions;
   389     return availRegions;
   426 #else
   390 #else 
   427     return QStringList();
   391     return QStringList();
   428 #endif
   392 #endif
   429 }
   393 }
   430 
   394 
   431 /*!
   395 /*!
   432     \brief Converts two letter region identifier to localised region name. 
   396 
   433     
   397     Returns the localised region name for the given two-letter region 
   434     \attention Symbian specific API
   398     identifier. If the translation fails, returns an empty QString.
   435     
   399     
   436     \param region region identifier 
   400     \param region The region identifier that corresponds to one of the following:
   437     
   401     - An identifier returned by supportedRegions() or currentRegion()
   438     \return Symbian - localised name of the region, an empty String if translation fails
   402     - An HbLanguageRegionMapping::regionId string
   439     \return other platforms - empty QString    
   403     
       
   404     \attention Only fully implemented on the Symbian platform. For other 
       
   405     platforms, always returns an empty QString.
       
   406     
       
   407     \sa currentRegion(), changeRegion()
       
   408   
   440 */ 
   409 */ 
   441 QString HbLocaleUtil::localisedRegionName( const QString &region ) 
   410 QString HbLocaleUtil::localisedRegionName( const QString &region ) 
   442 {
   411 {
   443 #if defined(Q_OS_SYMBIAN)       
   412 #if defined(Q_OS_SYMBIAN)       
   444     if(locRegionNames.isEmpty())
   413     if(locRegionNames.isEmpty())
   479     return QString();
   448     return QString();
   480 #endif    
   449 #endif    
   481 }
   450 }
   482 
   451 
   483 /*!
   452 /*!
   484     \brief Changes the system region.  
   453     Changes the system region to the one specified in \a region.
   485     The region parameter should correspond with one of the identifiers returned by supportedRegions(). 
   454 
   486   
   455     \param region The region identifier. This must correspond to an identifier 
   487     \attention Symbian specific API
   456     returned by supportedRegions(), or an HbLanguageRegionMapping::regionId 
   488 
   457     string.
   489     \param region identifier of region to set active
   458     
   490     
   459     \return \c true if successful, otherwise \c false.
   491     \return true if region change was successful for Symbian and false for other platforms
   460     
       
   461     \attention Only fully implemented on the Symbian platform. For other 
       
   462     platforms, always returns \c false.
       
   463     
       
   464     \sa localisedRegionName(), currentRegion()
       
   465     
   492 */ 
   466 */ 
   493 bool HbLocaleUtil::changeRegion( const QString &region )
   467 bool HbLocaleUtil::changeRegion( const QString &region )
   494 {
   468 {
   495 #if defined(Q_OS_SYMBIAN)   
   469 #if defined(Q_OS_SYMBIAN)   
   496     TExtendedLocale dummy;
   470    
   497     QString regDllName = QString( "elocl_reg." );
       
   498     
       
   499     if(mappingList.isEmpty())
   471     if(mappingList.isEmpty())
   500     {
   472     {
   501         readMappings();
   473         readMappings();
   502     }
   474     }
   503     int count = mappingList.count();
   475     int count = mappingList.count();
   505     {
   477     {
   506         HbLocaleMapping mapping = mappingList.at(j);
   478         HbLocaleMapping mapping = mappingList.at(j);
   507         QString name = mapping.regName;
   479         QString name = mapping.regName;
   508         if(name == region)
   480         if(name == region)
   509         {
   481         {
   510             dummy.LoadSystemSettings();
   482             int retVal = -1;
   511             regDllName += mapping.regionDllId;
   483             QString dllid = mapping.regionDllId;
   512             TPtrC nameptr(regDllName.utf16());
   484             bool ok;
   513             TInt err = dummy.LoadLocaleAspect( nameptr );
   485             int regioncode = dllid.toInt(&ok,10);
   514             if( err != KErrNone )
   486             if(!regioncode)
   515                 return false;
   487                 return false;
   516             dummy.SaveSystemSettings();
   488             retVal = SysLangUtil::ChangeRegion(regioncode);
   517             // cause localeprivate update on next qlocale object
   489             if(retVal != 0)
   518             QSystemLocale dummy2;
   490                 return false;
   519             return true; 
   491             else
       
   492                 return true;
   520         }
   493         }
   521     }
   494     }
   522     return false;
   495     return false;
   523 #else
   496 #else
   524     Q_UNUSED(region);
   497     Q_UNUSED(region);
   525     return false;
   498     return false;
   526 #endif    
   499 #endif    
   527 }
   500 }
   528 
   501 
   529 /*!
   502 /*!
   530     \brief Return identifier of the current region.
   503     
   531 
   504     Returns the identifier of the current region. The region identifier is a 
   532     \attention Symbian specific API
   505     two-letter ISO 3166 code, for example, "GB" for United Kingdom.
   533 
   506 
   534     \return identifier of the region for Symbian and empty QString for other platforms
   507     \attention Only fully implemented on the Symbian platform. For other 
       
   508     platforms, always returns an empty QString.
       
   509     
       
   510     \sa supportedRegions(), localisedRegionName(), changeRegion(), languageRegionMappings()
       
   511 
   535 */ 
   512 */ 
   536 QString HbLocaleUtil::currentRegion()
   513 QString HbLocaleUtil::currentRegion()
   537 {
   514 {
   538 #if defined(Q_OS_SYMBIAN)      
   515 #if defined(Q_OS_SYMBIAN)      
   539     if(mappingList.isEmpty())
   516     if(mappingList.isEmpty())
   553     }
   530     }
   554 #endif    
   531 #endif    
   555     return QString();
   532     return QString();
   556 }
   533 }
   557 
   534 
   558 #if defined(Q_OS_SYMBIAN)      
   535 /*!
   559 /*!
   536     
   560     \brief reads the collations.txt file and reads the list of symbian collation codes 
   537     Returns the identifiers of the collations supported on a device. Typically 
   561 */
   538     the collation identifier is an ISO 639 language code, but in some cases, it 
   562 void readCollations()
   539     is a combination of an ISO 639 language code and an ISO 3166 country code. 
   563 {
   540     For example, for UK English, this returns "en", and for US English, this 
   564     QFile* file = new QFile(COLLATION_LIST_FILE);
   541     returns "en_US".
   565     if (!file->exists() ) 
   542     
   566     {
   543     \attention Only fully implemented on the Symbian platform. For other 
   567         delete file;
   544     platforms, always returns an empty QStringList.
   568         return ;
   545     
   569     }
   546     \sa localisedCollationName(), currentCollation(), changeCollation()
   570     if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
   547  
   571     {
       
   572         delete file;
       
   573         return ;
       
   574     } 
       
   575     QTextStream in(file);
       
   576     while (!in.atEnd())
       
   577     {
       
   578         QString line = in.readLine(256);
       
   579         if (!line.isEmpty())
       
   580         {
       
   581             int colCode = line.toUInt();
       
   582             collations.append(colCode);
       
   583         }
       
   584     }
       
   585     return ;
       
   586 }
       
   587 #endif
       
   588 
       
   589 /*!
       
   590     \brief Returns identifiers of collations supported in a device. 
       
   591     Collation identifier may be two-letter ISO 639 language code or combination of ISO 639 language code and ISO 3166 country code 
       
   592     Ex: Great Britain english it returns "en".
       
   593     Ex: For U.S. english it returns "en_US"  
       
   594     
       
   595     \attention Symbian specific API
       
   596 
       
   597     \return identifiers of supported collations for Symbian and empty QStringList for other platforms 
       
   598 */
   548 */
   599 QStringList HbLocaleUtil::supportedCollations()
   549 QStringList HbLocaleUtil::supportedCollations()
   600 {
   550 {
   601 #if defined(Q_OS_SYMBIAN)
   551 #if defined(Q_OS_SYMBIAN)
   602     if(!availCollations.isEmpty())
   552     if(!availCollations.isEmpty())
   603     {
   553     {
   604         return availCollations;
   554         return availCollations;
   605     }
   555     }
   606 
   556 
   607     if(collations.isEmpty())
   557     QStringList collations; 
   608     {
   558     CArrayFixFlat<TInt>* systemEpocCollationCodes = 0;
   609         readCollations();
   559     TInt error = SysLangUtil::GetInstalledCollations( systemEpocCollationCodes );
   610     }
   560     if ( error != KErrNone ) {
   611     
   561         delete systemEpocCollationCodes;
   612     if(mappingList.isEmpty())
   562         return collations;
   613     {
   563     }
   614         readMappings();
   564     
   615     }
   565     if(mappingList.isEmpty()) {
   616     int colCount = collations.count();
   566         readMappings();
   617     for(int i = 0; i < colCount; i++)
   567     }
   618     {
   568     
   619         int collation = collations.at(i);
   569     for (int i = 0; i < systemEpocCollationCodes->Count(); ++i) {
   620         int count = mappingList.count();
   570         int code = systemEpocCollationCodes->At(i);
   621         for (int j = 0; j < count; j++)
   571         for (int j = 0; j < mappingList.count(); ++j) {
   622         {
   572             HbLocaleMapping map = mappingList.at(j);
   623             HbLocaleMapping mapping = mappingList.at(j);
   573             bool ok;
   624             QString colCode = mapping.collationDllId;
   574             int symCollatValue = map.collationDllId.toInt(&ok,10);
   625             if(collation == colCode.toUInt())
   575             if (symCollatValue == code) {
   626             {
   576                 availCollations.append(map.collName);
   627                 availCollations.append(mapping.collName);
       
   628                 break;
   577                 break;
   629             }
   578             }
   630         }
   579         }
   631     }
   580     }
       
   581     
       
   582     delete systemEpocCollationCodes;
   632     return availCollations;
   583     return availCollations;
   633 #else
   584 #else 
   634     return QStringList();
   585     return QStringList();
   635 #endif
   586 #endif
   636 }
   587 }
   637 
   588 
   638 /*!
   589 /*!
   639     \brief Converts collation identifier to localised collation name. 
   590     
   640     
   591     Returns the localised collation name for the given collation identifier. If 
   641     \attention Symbian specific API
   592     the translation fails, returns an empty QString.
   642     
   593     
   643     \param collation region collation identifier 
   594     \param collation The region collation identifier that corresponds to one of the following:
   644     
   595     - An identifier returned by supportedCollations() or currentCollation()
   645     \return Symbian - localised name of the collation, an empty String if translation fails
   596     - An HbLanguageRegionMapping::collationId string
   646     \return other platforms - empty QString    
   597     
       
   598     \attention Only fully implemented on the Symbian platform. For other 
       
   599     platforms, always returns an empty QString.
       
   600     
       
   601     \sa currentCollation(), changeCollation()
       
   602   
   647 */ 
   603 */ 
   648 QString HbLocaleUtil::localisedCollationName( const QString &collation ) 
   604 QString HbLocaleUtil::localisedCollationName( const QString &collation ) 
   649 {
   605 {
   650 #if defined(Q_OS_SYMBIAN)       
   606 #if defined(Q_OS_SYMBIAN)       
   651     if(locCollationNames.isEmpty())
   607     if(locCollationNames.isEmpty())
   686     return QString();
   642     return QString();
   687 #endif    
   643 #endif    
   688 }
   644 }
   689 
   645 
   690 /*!
   646 /*!
   691     \brief Changes the system collation.  
   647     
   692     The collation parameter should correspond with one of the identifiers returned by supportedCollations(). 
   648     Changes the system collation to the one specified in \a collation.
   693   
   649     
   694     \attention Symbian specific API
   650     \param collation The collation identifier. This must correspond to an 
   695     
   651     identifier returned by supportedCollations(), or an 
   696     \param collation identifier of collation to set active
   652     HbLanguageRegionMapping::collationId string.
   697     \return true if collation change was successful for Symbian and false for other platforms
   653     
       
   654     \return \c true if successful, otherwise \c false.
       
   655     
       
   656     \attention Only fully implemented on the Symbian platform. For other 
       
   657     platforms, always returns \c false.
       
   658     
       
   659     \sa localisedCollationName(), currentCollation()
   698 */ 
   660 */ 
   699 bool HbLocaleUtil::changeCollation( const QString &collation )
   661 bool HbLocaleUtil::changeCollation( const QString &collation )
   700 {
   662 {
   701 #if defined(Q_OS_SYMBIAN) 
   663 #if defined(Q_OS_SYMBIAN) 
   702     TExtendedLocale dummy;
       
   703     QString colDllName = QString( "elocl_col." );
       
   704     
   664     
   705     if(mappingList.isEmpty())
   665     if(mappingList.isEmpty())
   706     {
   666     {
   707         readMappings();
   667         readMappings();
   708     }
   668     }
   711     {
   671     {
   712         HbLocaleMapping mapping = mappingList.at(j);
   672         HbLocaleMapping mapping = mappingList.at(j);
   713         QString name = mapping.collName;
   673         QString name = mapping.collName;
   714         if(name == collation)
   674         if(name == collation)
   715         {
   675         {
   716             dummy.LoadSystemSettings();
   676             int retVal = -1;
   717             colDllName += mapping.collationDllId;
   677             QString dllid = mapping.collationDllId;
   718             TPtrC nameptr(colDllName.utf16());
   678             bool ok;
   719             TInt err = dummy.LoadLocaleAspect( nameptr );
   679             int collCode = dllid.toInt(&ok,10);
   720             if( err != KErrNone )
   680             if(!collCode)
   721                 return false;
   681                 return false;
   722             dummy.SaveSystemSettings();
   682             retVal = SysLangUtil::ChangeCollation(collCode);
   723             // cause localeprivate update on next qlocale object
   683             if(retVal != 0)
   724             QSystemLocale dummy2;
   684                 return false;
   725             return true; 
   685             else
       
   686                 return true;
   726         }
   687         }
   727     }
   688     }
   728     return false;
   689     return false;
   729 #else
   690 #else
   730     Q_UNUSED(collation);
   691     Q_UNUSED(collation);
   731     return false;
   692     return false;
   732 #endif    
   693 #endif    
   733 }
   694 }
   734 
   695 
   735 /*!
   696 /*!
   736     \brief Return identifier of the current collation.
   697     
   737 
   698     Returns the identifier of the current collation. Typically the collation 
   738     \attention Symbian specific API
   699     identifier is an ISO 639 language code, but in some cases, it is a 
   739     
   700     combination of an ISO 639 language code and an ISO 3166 country code. For 
   740     \return identifier of the collation for Symbian and empty QString for other platforms
   701     example, for UK English, this returns "en", and for US English, this returns 
       
   702     "en_US".
       
   703 
       
   704     \attention Only fully implemented on the Symbian platform. For other 
       
   705     platforms, always returns an empty QString.
       
   706     
       
   707     \sa supportedCollations(), localisedCollationName(), changeCollation(), languageRegionMappings()
   741 */ 
   708 */ 
   742 QString HbLocaleUtil::currentCollation()
   709 QString HbLocaleUtil::currentCollation()
   743 {
   710 {
   744 #if defined(Q_OS_SYMBIAN)      
   711 #if defined(Q_OS_SYMBIAN)      
   745     if(mappingList.isEmpty())
   712     if(mappingList.isEmpty())
   772 #endif    
   739 #endif    
   773     return QString();    
   740     return QString();    
   774 }
   741 }
   775 
   742 
   776 /*!
   743 /*!
   777     \brief Changes the system language, region and collation.  
   744 
   778     The language parameter should correspond with one of the identifiers returned by supportedLanguages(). 
   745     Changes the system language, region and collation as specified by \a 
   779     Proper region and collation is selected automatically according the language.  
   746     language. The region and collation are selected automatically based on the 
       
   747     specified language.
       
   748 
       
   749     \param language The language identifier. This must correspond to an 
       
   750     identifier returned by supportedLanguages(), or an 
       
   751     HbLanguageRegionMapping::languageId string.
   780   
   752   
   781     \attention Symbian specific API
   753     \return \c true if successful, otherwise \c false.
   782     
   754     
   783     \param language identifier of language which language, region and collation settings should set active  
   755     \attention Only fully implemented on the Symbian platform. For other 
   784     
   756     platforms, always returns \c false.
   785     \return Symbian - true if language, region and collation change was successful
   757     
   786     \return other platforms - false    
   758     \sa supportedRegions(), supportedCollations(), languageRegionMappings()
       
   759  
   787 */ 
   760 */ 
   788 bool HbLocaleUtil::changeLocale( const QString &language )
   761 bool HbLocaleUtil::changeLocale( const QString &language )
   789 {
   762 {
   790 #if defined(Q_OS_SYMBIAN) 
   763 #if defined(Q_OS_SYMBIAN) 
   791     if(mappingList.isEmpty()) {
   764     if(mappingList.isEmpty()) {
   811     Q_UNUSED(language);
   784     Q_UNUSED(language);
   812 #endif // Q_OS_SYMBIAN
   785 #endif // Q_OS_SYMBIAN
   813     return false;   
   786     return false;   
   814 }
   787 }
   815 
   788 
       
   789 /*!
       
   790     
       
   791     \struct HbLanguageRegionMapping hblocaleutil.h
       
   792     
       
   793     Defines the data structure for language, region and collation identifier 
       
   794     mappings.
       
   795     
       
   796     \sa HbLocaleUtil::languageRegionMappings()
       
   797   
       
   798 */
       
   799 
       
   800 /*!
       
   801     
       
   802     \var HbLanguageRegionMapping::languageId
       
   803     
       
   804     A language identifier. Typically this is an ISO 639 language code, but in 
       
   805     some cases, it is a combination of an ISO 639 language code and an ISO 3166 
       
   806     country code. For example, for UK English, this returns "en", and for US 
       
   807     English, this returns "en_US".
       
   808   
       
   809 */ 
       
   810 
       
   811 /*!
       
   812     
       
   813     \var HbLanguageRegionMapping::regionId
       
   814     
       
   815     A region identifier. This is a two-letter ISO 3166 code, for example, "GB" 
       
   816     for United Kingdom.
       
   817   
       
   818 */ 
       
   819 
       
   820 /*!
       
   821     
       
   822     \var HbLanguageRegionMapping::collationId
       
   823     
       
   824     A collation identifier. Typically this is an ISO 639 language code, but in 
       
   825     some cases, it is a combination of an ISO 639 language code and an ISO 3166 
       
   826     country code. For example, for UK English, this returns "en", and for US 
       
   827     English, this returns "en_US".
       
   828   
       
   829 */ 
       
   830 
       
   831 /*!
       
   832     
       
   833     Returns a list of language, region and collation identifier mappings. For 
       
   834     example, for UK English, the mapping has "en" for language, "GB" for region 
       
   835     and "en" for collation. For US English, the corresponding values are 
       
   836     "en_US", "US" and "en_US". You can use the optional \a onlySupported 
       
   837     parameter to limit the search only to the languages, regions and collations 
       
   838     supported on the device.
       
   839         
       
   840     If there is a mapping with a language that does not have a corresponding 
       
   841     region, the region is returned as an empty QString. Similarly, if there is a 
       
   842     mapping with a region that does not have a corresponding language and 
       
   843     collation, the language and collation are returned as empty QStrings.
       
   844     
       
   845     \param onlySupported Specify \c true to only return the mappings for 
       
   846     languages, regions and collations that are supported on the device. To 
       
   847     return mappings for all known languages, regions and collations, specify \c 
       
   848     false.
       
   849     
       
   850     For example:
       
   851     \snippet{unittest_hblocaleutil/unittest_hblocaleutil.cpp,2}
       
   852 
       
   853     \attention Only fully implemented on the Symbian platform. For other 
       
   854     platforms, always returns an empty list.
       
   855     
       
   856     \sa currentLanguage(), currentRegion(), currentCollation()
       
   857   
       
   858 */ 
       
   859 QList<HbLanguageRegionMapping> HbLocaleUtil::languageRegionMappings( bool onlySupported )
       
   860 {
       
   861     QList<HbLanguageRegionMapping> mps;
       
   862 #if defined(Q_OS_SYMBIAN) 
       
   863     if (mappingList.isEmpty()) {
       
   864         readMappings();
       
   865     }
       
   866    
       
   867     QStringList supportedLanguages;
       
   868     QStringList supportedRegions;
       
   869     if (onlySupported) {
       
   870         supportedLanguages = HbLocaleUtil::supportedLanguages();
       
   871         supportedRegions = HbLocaleUtil::supportedRegions();
       
   872     }
       
   873     
       
   874     for (int i = 0;  i < mappingList.count(); ++i) {
       
   875 
       
   876         HbLocaleMapping map = mappingList.at(i);
       
   877 
       
   878         HbLanguageRegionMapping mapping;
       
   879         mapping.languageId = map.langName;
       
   880         mapping.regionId = map.regName;
       
   881         mapping.collationId = map.collName;
       
   882 
       
   883         if (onlySupported) {
       
   884             bool langSupported = supportedLanguages.contains(map.langName);
       
   885             bool regSupported = supportedRegions.contains(map.regName);
       
   886             if (!langSupported && !regSupported) {
       
   887                 continue;
       
   888             }
       
   889             if (!langSupported) {
       
   890                 mapping.languageId = "";
       
   891             }
       
   892             if (!regSupported) {
       
   893                 mapping.regionId = "";
       
   894             }
       
   895         }
       
   896 
       
   897         mps.append(mapping);
       
   898 
       
   899     }
       
   900 #else
       
   901     Q_UNUSED(onlySupported);
       
   902 #endif // Q_OS_SYMBIAN
       
   903     return mps;
       
   904 }
       
   905