• Skip to content
  • Skip to link menu
KDE 4.1 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 static const QByteArray GLUE = GETTEXT_CONTEXT_GLUE;
00041 
00042 class KCatalogPrivate
00043 {
00044 public:
00045   QByteArray language;
00046   QByteArray name;
00047   QByteArray localeDir;
00048 
00049   static int localeSet;
00050   static QByteArray currentLanguage;
00051 
00052   void changeBindings () const;
00053 };
00054 
00055 QDebug operator<<(QDebug debug, const KCatalog &c)
00056 {
00057   return debug << c.d->language << " " << c.d->name << " " << c.d->localeDir;
00058 }
00059 
00060 int KCatalogPrivate::localeSet = 0;
00061 QByteArray KCatalogPrivate::currentLanguage;
00062 
00063 KCatalog::KCatalog(const QString & name, const QString & language )
00064   : d( new KCatalogPrivate )
00065 {
00066   // Set locales only once.
00067   if (! KCatalogPrivate::localeSet) {
00068     setlocale(LC_ALL, "");
00069     KCatalogPrivate::localeSet = 1;
00070   }
00071 
00072   // Find locale directory for this catalog.
00073   QString localeDir = catalogLocaleDir( name, language );
00074 
00075   d->language = QFile::encodeName( language );
00076   d->name = QFile::encodeName( name );
00077   d->localeDir = QFile::encodeName( localeDir );
00078 
00079   // Always get translations in UTF-8, regardless of user's environment.
00080   bind_textdomain_codeset( d->name, "UTF-8" );
00081 
00082   // Invalidate current language, to trigger binding at next translate call.
00083   KCatalogPrivate::currentLanguage.clear();
00084 }
00085 
00086 KCatalog::KCatalog(const KCatalog & rhs)
00087   : d( new KCatalogPrivate )
00088 {
00089   *this = rhs;
00090 }
00091 
00092 KCatalog & KCatalog::operator=(const KCatalog & rhs)
00093 {
00094   *d = *rhs.d;
00095 
00096   // Update bindings.
00097   // (Sometimes Gettext picks up wrong locale directory if bindings are not
00098   // updated here. No idea why that happens.)
00099   d->changeBindings();
00100 
00101   return *this;
00102 }
00103 
00104 KCatalog::~KCatalog()
00105 {
00106   delete d;
00107 }
00108 
00109 QString KCatalog::catalogLocaleDir( const QString &name,
00110                                     const QString &language )
00111 {
00112   QString relpath =  QString::fromLatin1( "%1/LC_MESSAGES/%2.mo" )
00113                     .arg( language ).arg( name );
00114   return KGlobal::dirs()->findResourceDir( "locale", relpath );
00115 }
00116 
00117 QString KCatalog::name() const
00118 {
00119   return d->name;
00120 }
00121 
00122 QString KCatalog::language() const
00123 {
00124   return d->language;
00125 }
00126 
00127 QString KCatalog::localeDir() const
00128 {
00129   return d->localeDir;
00130 }
00131 
00132 void KCatalogPrivate::changeBindings () const
00133 {
00134   if (language != currentLanguage) {
00135 
00136     currentLanguage = language;
00137 
00138     // Point Gettext to new language.
00139     setenv("LANGUAGE", language, 1);
00140 
00141     // Locale directories may differ for different languages of same catalog.
00142     bindtextdomain(name, localeDir);
00143 
00144     // // Magic to make sure Gettext doesn't use stale cached translation
00145     // // from previous language.
00146     // extern int _nl_msg_cat_cntr;
00147     // ++_nl_msg_cat_cntr;
00148     //
00149     // Note: Not needed, caching of translations is not an issue because
00150     // language is switched only if translation is not found.
00151   }
00152 }
00153 
00154 QString KCatalog::translate(const char * msgid) const
00155 {
00156   d->changeBindings();
00157   return QString::fromUtf8(dgettext(d->name, msgid));
00158 }
00159 
00160 QString KCatalog::translate(const char * msgctxt, const char * msgid) const
00161 {
00162   d->changeBindings();
00163   return QString::fromUtf8(dpgettext_expr(d->name, msgctxt, msgid));
00164 }
00165 
00166 QString KCatalog::translate(const char * msgid, const char * msgid_plural,
00167                             unsigned long n) const
00168 {
00169   d->changeBindings();
00170   return QString::fromUtf8(dngettext(d->name, msgid, msgid_plural, n));
00171 }
00172 
00173 QString KCatalog::translate(const char * msgctxt, const char * msgid,
00174                             const char * msgid_plural, unsigned long n) const
00175 {
00176   d->changeBindings();
00177   return QString::fromUtf8(dnpgettext_expr(d->name, msgctxt, msgid, msgid_plural, n));
00178 }
00179 
00180 QString KCatalog::translateStrict(const char * msgid) const
00181 {
00182   d->changeBindings();
00183   const char *msgstr = dgettext(d->name, msgid);
00184   return msgstr != msgid ? QString::fromUtf8(msgstr) : QString();
00185 }
00186 
00187 QString KCatalog::translateStrict(const char * msgctxt, const char * msgid) const
00188 {
00189   d->changeBindings();
00190   const char *msgstr = dpgettext_expr(d->name, msgctxt, msgid);
00191   return msgstr != msgid ? QString::fromUtf8(msgstr) : QString();
00192 }
00193 
00194 QString KCatalog::translateStrict(const char * msgid, const char * msgid_plural,
00195                                   unsigned long n) const
00196 {
00197   d->changeBindings();
00198   const char *msgstr = dngettext(d->name, msgid, msgid_plural, n);
00199   return msgstr != msgid && msgstr != msgid_plural ? QString::fromUtf8(msgstr) : QString();
00200 }
00201 
00202 QString KCatalog::translateStrict(const char * msgctxt, const char * msgid,
00203                                   const char * msgid_plural, unsigned long n) const
00204 {
00205   d->changeBindings();
00206   const char *msgstr = dnpgettext_expr(d->name, msgctxt, msgid, msgid_plural, n);
00207   return msgstr != msgid && msgstr != msgid_plural ? QString::fromUtf8(msgstr) : QString();
00208 }
00209 

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
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
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