• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDECore

kcatalog.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002    Copyright (c) 2001 Hans Petter Bieker <bieker@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "kcatalog_p.h"
00021 #include "kstandarddirs.h"
00022 
00023 #include <config.h>
00024 
00025 #include <QtCore/QFile>
00026 
00027 #include <kdebug.h>
00028 
00029 #include <stdlib.h>
00030 #include <locale.h>
00031 #include "gettext.h"
00032 
00033 // not defined on win32 :(
00034 #ifdef _WIN32
00035 # ifndef LC_MESSAGES
00036 #  define LC_MESSAGES 42
00037 # endif
00038 #endif
00039 
00040 class KCatalogPrivate
00041 {
00042 public:
00043   QByteArray language;
00044   QByteArray name;
00045   QByteArray localeDir;
00046 
00047   QByteArray systemLanguage;
00048 
00049   static int localeSet;
00050   static QByteArray currentLanguage;
00051 
00052   void setupGettextEnv ();
00053   void resetSystemLanguage ();
00054 };
00055 
00056 QDebug operator<<(QDebug debug, const KCatalog &c)
00057 {
00058   return debug << c.d->language << " " << c.d->name << " " << c.d->localeDir;
00059 }
00060 
00061 int KCatalogPrivate::localeSet = 0;
00062 QByteArray KCatalogPrivate::currentLanguage;
00063 
00064 KCatalog::KCatalog(const QString & name, const QString & language )
00065   : d( new KCatalogPrivate )
00066 {
00067   // Set locales only once.
00068   if (! KCatalogPrivate::localeSet) {
00069     setlocale(LC_ALL, "");
00070     KCatalogPrivate::localeSet = 1;
00071   }
00072 
00073   // Find locale directory for this catalog.
00074   QString localeDir = catalogLocaleDir( name, language );
00075 
00076   d->language = QFile::encodeName( language );
00077   d->name = QFile::encodeName( name );
00078   d->localeDir = QFile::encodeName( localeDir );
00079 
00080   // Always get translations in UTF-8, regardless of user's environment.
00081   bind_textdomain_codeset( d->name, "UTF-8" );
00082 
00083   // Invalidate current language, to trigger binding at next translate call.
00084   KCatalogPrivate::currentLanguage.clear();
00085 }
00086 
00087 KCatalog::KCatalog(const KCatalog & rhs)
00088   : d( new KCatalogPrivate )
00089 {
00090   *this = rhs;
00091 }
00092 
00093 KCatalog & KCatalog::operator=(const KCatalog & rhs)
00094 {
00095   *d = *rhs.d;
00096 
00097   // Update Gettext environment.
00098   // (Sometimes Gettext picks up wrong locale directory if bindings are not
00099   // updated here. No idea why that happens.)
00100   d->setupGettextEnv();
00101   d->resetSystemLanguage();
00102 
00103   return *this;
00104 }
00105 
00106 KCatalog::~KCatalog()
00107 {
00108   delete d;
00109 }
00110 
00111 QString KCatalog::catalogLocaleDir( const QString &name,
00112                                     const QString &language )
00113 {
00114   QString relpath =  QString::fromLatin1( "%1/LC_MESSAGES/%2.mo" )
00115                     .arg( language ).arg( name );
00116   return KGlobal::dirs()->findResourceDir( "locale", relpath );
00117 }
00118 
00119 QString KCatalog::name() const
00120 {
00121   return d->name;
00122 }
00123 
00124 QString KCatalog::language() const
00125 {
00126   return d->language;
00127 }
00128 
00129 QString KCatalog::localeDir() const
00130 {
00131   return d->localeDir;
00132 }
00133 
00134 void KCatalogPrivate::setupGettextEnv ()
00135 {
00136   // Point Gettext to current language, recording system value for recovery.
00137   systemLanguage = qgetenv("LANGUAGE");
00138   if (systemLanguage != language) {
00139     qputenv("LANGUAGE", language);
00140   }
00141 
00142   // Rebind text domain if language actually changed from the last time,
00143   // as locale directories may differ for different languages of same catalog.
00144   if (language != currentLanguage) {
00145 
00146     currentLanguage = language;
00147 
00148     bindtextdomain(name, localeDir);
00149 
00150     // // Magic to make sure Gettext doesn't use stale cached translation
00151     // // from previous language.
00152     // extern int _nl_msg_cat_cntr;
00153     // ++_nl_msg_cat_cntr;
00154     //
00155     // Note: Not needed, caching of translations is not an issue because
00156     // language is switched only if translation is not found.
00157   }
00158 }
00159 
00160 void KCatalogPrivate::resetSystemLanguage ()
00161 {
00162   if (language != systemLanguage) {
00163     qputenv("LANGUAGE", systemLanguage);
00164   }
00165 }
00166 
00167 QString KCatalog::translate(const char * msgid) const
00168 {
00169   d->setupGettextEnv();
00170   const char *msgstr = dgettext(d->name, msgid);
00171   d->resetSystemLanguage();
00172   return QString::fromUtf8(msgstr);
00173 }
00174 
00175 QString KCatalog::translate(const char * msgctxt, const char * msgid) const
00176 {
00177   d->setupGettextEnv();
00178   const char *msgstr = dpgettext_expr(d->name, msgctxt, msgid);
00179   d->resetSystemLanguage();
00180   return QString::fromUtf8(msgstr);
00181 }
00182 
00183 QString KCatalog::translate(const char * msgid, const char * msgid_plural,
00184                             unsigned long n) const
00185 {
00186   d->setupGettextEnv();
00187   const char *msgstr = dngettext(d->name, msgid, msgid_plural, n);
00188   d->resetSystemLanguage();
00189   return QString::fromUtf8(msgstr);
00190 }
00191 
00192 QString KCatalog::translate(const char * msgctxt, const char * msgid,
00193                             const char * msgid_plural, unsigned long n) const
00194 {
00195   d->setupGettextEnv();
00196   const char *msgstr = dnpgettext_expr(d->name, msgctxt, msgid, msgid_plural, n);
00197   d->resetSystemLanguage();
00198   return QString::fromUtf8(msgstr);
00199 }
00200 
00201 QString KCatalog::translateStrict(const char * msgid) const
00202 {
00203   d->setupGettextEnv();
00204   const char *msgstr = dgettext(d->name, msgid);
00205   d->resetSystemLanguage();
00206   return msgstr != msgid ? QString::fromUtf8(msgstr) : QString();
00207 }
00208 
00209 QString KCatalog::translateStrict(const char * msgctxt, const char * msgid) const
00210 {
00211   d->setupGettextEnv();
00212   const char *msgstr = dpgettext_expr(d->name, msgctxt, msgid);
00213   d->resetSystemLanguage();
00214   return msgstr != msgid ? QString::fromUtf8(msgstr) : QString();
00215 }
00216 
00217 QString KCatalog::translateStrict(const char * msgid, const char * msgid_plural,
00218                                   unsigned long n) const
00219 {
00220   d->setupGettextEnv();
00221   const char *msgstr = dngettext(d->name, msgid, msgid_plural, n);
00222   d->resetSystemLanguage();
00223   return msgstr != msgid && msgstr != msgid_plural ? QString::fromUtf8(msgstr) : QString();
00224 }
00225 
00226 QString KCatalog::translateStrict(const char * msgctxt, const char * msgid,
00227                                   const char * msgid_plural, unsigned long n) const
00228 {
00229   d->setupGettextEnv();
00230   const char *msgstr = dnpgettext_expr(d->name, msgctxt, msgid, msgid_plural, n);
00231   d->resetSystemLanguage();
00232   return msgstr != msgid && msgstr != msgid_plural ? QString::fromUtf8(msgstr) : QString();
00233 }
00234 

KDECore

Skip menu "KDECore"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal