src/hbcore/i18n/hbtranslator.cpp
changeset 0 16d8024aca5e
child 1 f7ac710697a9
equal deleted inserted replaced
-1:000000000000 0:16d8024aca5e
       
     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 <QApplication>
       
    27 #include <QFileInfo>
       
    28 #include <QLocale>
       
    29 #include <QTranslator> 
       
    30 #include <QHash>
       
    31 
       
    32 #include <hbfindfile.h>
       
    33 #include <hbtranslator.h>
       
    34 #include <hbtranslator_p.h>
       
    35 
       
    36 
       
    37 const QString defaultpath = "/resource/qt/translations/";
       
    38 
       
    39 /*!
       
    40     @beta
       
    41     @hbcore
       
    42     \class HbTranslator
       
    43     \brief HbTranslator installs QTranslator(s) automatically needed in localisation
       
    44     and loads translation files into QTranslator.
       
    45 */
       
    46 
       
    47 /*!
       
    48     Default case: searches translation file from default location with default name, which is <executablename>.qm
       
    49 */
       
    50 HbTranslator::HbTranslator(): d(new HbTranslatorPrivate())
       
    51 {
       
    52     QFileInfo info(qApp->applicationFilePath());
       
    53     QString defaultname = info.baseName();  // defaultname = <executablename>
       
    54     d->installTranslator(defaultpath, defaultname);
       
    55 }
       
    56 
       
    57 /*!
       
    58     Searches translation file \a file from default location.
       
    59 */
       
    60 
       
    61 HbTranslator::HbTranslator(const QString &file): d(new HbTranslatorPrivate())
       
    62 {
       
    63     d->installTranslator(defaultpath, file);
       
    64 }
       
    65 
       
    66 /*!
       
    67     Searches translation file \a file from path \a path.
       
    68     \code
       
    69     HbTranslator trans("/resource/qt/custom/", "customfile");
       
    70     \endcode
       
    71 */
       
    72 HbTranslator::HbTranslator(const QString &path, const QString &file): d(new HbTranslatorPrivate())
       
    73 {
       
    74     d->installTranslator(path, file);
       
    75 }
       
    76 
       
    77 HbTranslator::~HbTranslator()
       
    78 {
       
    79     delete d;
       
    80 }
       
    81 
       
    82 /*!
       
    83     Loads common.ts translations from default location.
       
    84 */
       
    85 void HbTranslator::loadCommon()
       
    86 {
       
    87     QString lang = QLocale::system().name();
       
    88     QString commonts = QString("Z:") + defaultpath + QString("common_") + lang;
       
    89     d->common.load(commonts);
       
    90     qApp->installTranslator(&d->common);
       
    91 }
       
    92 
       
    93 // internal function for common operations of HbTranslator
       
    94 void HbTranslatorPrivate::installTranslator(const QString &path, const QString &name)
       
    95 {
       
    96     QString filepath = qApp->applicationFilePath();
       
    97     QChar drive = filepath.at(0);
       
    98     QString lang = QLocale::system().name();
       
    99     QString lang2 = lang;
       
   100     languageDowngrade(lang);
       
   101     QString tsfile = path + name + QString("_") + lang + QString(".qm");
       
   102     if (!HbFindFile::hbFindFile(tsfile, drive)) {
       
   103     	tsfile = path + name + QString("_") + lang2 + QString(".qm");
       
   104         HbFindFile::hbFindFile(tsfile);
       
   105     }
       
   106     translator.load(tsfile);
       
   107     qApp->installTranslator(&translator);
       
   108 }
       
   109 
       
   110 // internal function for solving conflict between QLocale::system().name() and actual ts file naming convention.
       
   111 bool HbTranslatorPrivate::languageDowngrade(QString &lang)
       
   112 {
       
   113     static QHash<QString, QString> languages;
       
   114     languages["en_GB"] = "en";
       
   115     languages["fr_FR"] = "fr";
       
   116     languages["de_DE"] = "de";
       
   117     languages["es_ES"] = "es";
       
   118     languages["it_IT"] = "it";
       
   119     languages["sv_SE"] = "sv";
       
   120     languages["da_DK"] = "da";
       
   121     languages["no_NO"] = "no";
       
   122     languages["fi_FI"] = "fi";
       
   123     languages["en_US"] = "en_US";
       
   124     languages["pt_PT"] = "pt";
       
   125     languages["tr_TR"] = "tr";
       
   126     languages["is_IS"] = "is";
       
   127     languages["ru_RU"] = "ru";
       
   128     languages["hu_HU"] = "hu";
       
   129     languages["nl_NL"] = "nl";
       
   130     languages["cs_CZ"] = "cs";
       
   131     languages["sk_SK"] = "sk";
       
   132     languages["pl_PL"] = "pl";
       
   133     languages["sl_SI"] = "sl";
       
   134     languages["zh_TW"] = "zh_TW";
       
   135     languages["zh_HK"] = "zh_HK";
       
   136     languages["zh_CN"] = "zh_CN";
       
   137     languages["ja_JP"] = "ja";
       
   138     languages["th_TH"] = "th";
       
   139     languages["ar_AE"] = "ar";
       
   140     languages["tl_PH"] = "tl";
       
   141     languages["bg_BG"] = "bg";
       
   142     languages["ca_ES"] = "ca";
       
   143     languages["hr_HR"] = "hr";
       
   144     languages["et_EE"] = "et";
       
   145     languages["fa_IR"] = "fa";
       
   146     languages["fr_CA"] = "fr_CA";
       
   147     languages["el_GR"] = "el";
       
   148     languages["he_IL"] = "he";
       
   149     languages["hi_IN"] = "hi";
       
   150     languages["id_ID"] = "id";
       
   151     languages["ko_KR"] = "ko";
       
   152     languages["lv_LV"] = "lv";
       
   153     languages["lt_LT"] = "lt";
       
   154     languages["ms_MY"] = "ms";
       
   155     languages["pt_BR"] = "pt_BR";
       
   156     languages["ro_RO"] = "ro";
       
   157     languages["sr_YU"] = "sr";
       
   158     languages["es_MX"] = "es_MX"; //!!
       
   159     languages["uk_UA"] = "uk";
       
   160     languages["ur_PK"] = "ur";
       
   161     languages["vi_VN"] = "vi";
       
   162     languages["eu_ES"] = "eu";
       
   163     languages["gl_ES"] = "gl";
       
   164     
       
   165     if (languages.contains(lang)) {
       
   166     	lang  = languages.value(lang);
       
   167     	return true;
       
   168     }
       
   169     return false;
       
   170 }