Quellcode durchsuchen

Added ctkPluginLocalization class.

Sascha Zelzer vor 14 Jahren
Ursprung
Commit
65c6752f75

+ 1 - 0
Libs/PluginFramework/CMakeLists.txt

@@ -39,6 +39,7 @@ SET(KIT_SRCS
   ctkPluginFrameworkPrivate_p.h
   ctkPluginFrameworkPrivate_p.h
   ctkPluginFrameworkUtil.cpp
   ctkPluginFrameworkUtil.cpp
   ctkPluginFrameworkUtil_p.h
   ctkPluginFrameworkUtil_p.h
+  ctkPluginLocalization.cpp
   ctkPluginManifest.cpp
   ctkPluginManifest.cpp
   ctkPluginManifest_p.h
   ctkPluginManifest_p.h
   ctkPluginPrivate.cpp
   ctkPluginPrivate.cpp

+ 47 - 0
Libs/PluginFramework/ctkPlugin.cpp

@@ -327,6 +327,53 @@ QByteArray ctkPlugin::getResource(const QString& path) const
   return d->archive->getPluginResource(path);
   return d->archive->getPluginResource(path);
 }
 }
 
 
+ctkPluginLocalization ctkPlugin::getPluginLocalization(
+  const QLocale& locale, const QString& base) const
+{
+  Q_D(const ctkPlugin);
+
+  // There are five searching candidates possible:
+  // base +
+  //    "_" + language1 + "_" + country1 + ".qm"
+  // or "_" + language1 + ".qm"
+  // or "_" + language2 + "_" + country2 + ".qm"
+  // or "_" + language2 + ".qm"
+  // or ""  + ".qm"
+  //
+  // Where language1[_country1[_variation1]] is the requested locale,
+  // and language2[_country2[_variation2]] is the default locale.
+
+  QChar localeSep('_');
+  QChar baseSep('_');
+
+  QStringList searchCandidates;
+
+  QStringList localeParts = locale.name().split(localeSep);
+  QStringList defaultParts = QLocale().name().split(localeSep);
+
+  searchCandidates << baseSep + localeParts[0] + localeSep + localeParts[1];
+  searchCandidates << baseSep + localeParts[0];
+  searchCandidates << baseSep + defaultParts[0] + localeSep + defaultParts[1];
+  searchCandidates << baseSep + defaultParts[0];
+  searchCandidates << "";
+
+  QString localizationPath = base.left(base.lastIndexOf('/'));
+  QStringList resourceList = this->getResourceList(localizationPath);
+
+  foreach(QString resource, resourceList)
+  {
+    foreach(QString candidate, searchCandidates)
+    {
+      if (resource.endsWith(candidate + ".qm"))
+      {
+        return ctkPluginLocalization(localizationPath + '/' + resource, locale, d->q_ptr);
+      }
+    }
+  }
+
+  return ctkPluginLocalization();
+}
+
 ctkVersion ctkPlugin::getVersion() const
 ctkVersion ctkPlugin::getVersion() const
 {
 {
   Q_D(const ctkPlugin);
   Q_D(const ctkPlugin);

+ 24 - 0
Libs/PluginFramework/ctkPlugin.h

@@ -27,6 +27,8 @@
 #include <QMetaType>
 #include <QMetaType>
 
 
 #include "ctkVersion.h"
 #include "ctkVersion.h"
+#include "ctkPluginLocalization.h"
+#include "ctkPluginConstants.h"
 #include "service/log/ctkLogStream.h"
 #include "service/log/ctkLogStream.h"
 
 
 class ctkPluginContext;
 class ctkPluginContext;
@@ -636,6 +638,28 @@ public:
   virtual QByteArray getResource(const QString& path) const;
   virtual QByteArray getResource(const QString& path) const;
 
 
   /**
   /**
+   * Returns a <code>ctkPluginLocalization</code> object for the
+   * specified <code>locale</code>. The translations are loaded from a
+   * .qm file starting with <code>base</code>.
+   *
+   * You can use the returned <code>ctkPluginLocalization</code>
+   * object to dynamically translate text without changing the current
+   * locale of the application. This can be used for example to
+   * provide localized messages to multiple users which use the application
+   * (maybe some kind of server) simultaneously but require different
+   * localizations.
+   *
+   * @param locale The locale to be used by the returned
+   *        <code>ctkPluginLocalization</code> object.
+   * @param base The base name of the .qm message file which contains
+   *        translated messages. Defaults to
+   *        <code>ctkPluginConstants::PLUGIN_LOCALIZATION_DEFAULT_BASENAME</code>.
+   * @return A locale specific <code>ctkPluginLocalization</code> instance.
+   */
+  ctkPluginLocalization getPluginLocalization(const QLocale& locale,
+                                              const QString& base = ctkPluginConstants::PLUGIN_LOCALIZATION_DEFAULT_BASENAME) const;
+
+  /**
    * Returns the version of this plugin as specified by its
    * Returns the version of this plugin as specified by its
    * <code>Plugin-Version</code> manifest header. If this plugin does not have a
    * <code>Plugin-Version</code> manifest header. If this plugin does not have a
    * specified version then {@link Version#emptyVersion} is returned.
    * specified version then {@link Version#emptyVersion} is returned.

+ 99 - 0
Libs/PluginFramework/ctkPluginLocalization.cpp

@@ -0,0 +1,99 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=============================================================================*/
+
+
+#include "ctkPluginLocalization.h"
+
+#include "ctkPlugin.h"
+
+#include <QTranslator>
+
+struct ctkPluginLocalizationData : public QSharedData
+{
+  ctkPluginLocalizationData(const QString& fileName, const QLocale& locale,
+                            const QSharedPointer<ctkPlugin>& plugin)
+    : locale(locale), translation(plugin->getResource(fileName))
+  {
+    translator.load(reinterpret_cast<const uchar*>(translation.constData()), translation.size());
+  }
+
+  ctkPluginLocalizationData(const ctkPluginLocalizationData& other)
+    : QSharedData(other),
+      locale(other.locale), translation(other.translation)
+  {
+    translator.load(reinterpret_cast<const uchar*>(translation.constData()), translation.size());
+  }
+
+  ~ctkPluginLocalizationData()
+  {
+
+  }
+
+  QTranslator translator;
+  const QLocale locale;
+  const QByteArray translation;
+};
+
+ctkPluginLocalization::ctkPluginLocalization()
+ : d(0)
+{
+
+}
+
+ctkPluginLocalization::ctkPluginLocalization(const ctkPluginLocalization &pl)
+  : d(pl.d)
+{
+
+}
+
+ctkPluginLocalization::ctkPluginLocalization(const QString& msgFileName,
+                                             const QLocale& locale, const QSharedPointer<ctkPlugin>& plugin)
+  : d(new ctkPluginLocalizationData(msgFileName, locale, plugin))
+{
+
+}
+
+ctkPluginLocalization::~ctkPluginLocalization()
+{
+
+}
+
+ctkPluginLocalization& ctkPluginLocalization::operator=(const ctkPluginLocalization& other)
+{
+  d = other.d;
+  return *this;
+}
+
+QString ctkPluginLocalization::getLocalized(const QString& context, const QString& str) const
+{
+  if (d)
+  {
+    return d->translator.translate(qPrintable(context), qPrintable(str));
+  }
+  return QString();
+}
+
+QLocale ctkPluginLocalization::getLocale() const
+{
+  if (d) return d->locale;
+  return QLocale();
+}
+

+ 92 - 0
Libs/PluginFramework/ctkPluginLocalization.h

@@ -0,0 +1,92 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=============================================================================*/
+
+
+#ifndef CTKPLUGINLOCALIZATION_H
+#define CTKPLUGINLOCALIZATION_H
+
+#include <ctkPluginFrameworkExport.h>
+
+#include <QLocale>
+#include <QSharedDataPointer>
+#include <QSharedPointer>
+
+class ctkPlugin;
+struct ctkPluginLocalizationData;
+
+/**
+ * Translate text into different languages.
+ *
+ * Use this class to dynamically translate human-readable text
+ * in your plugin. You can get an instance of this class
+ * corresponding to a specific locale via the method
+ * <code>ctkPlugin::getPluginLocalization()</code>.
+ *
+ * @see ctkPlugin::getPluginLocalization()
+ */
+class CTK_PLUGINFW_EXPORT ctkPluginLocalization
+{
+public:
+
+  /**
+   * Creates a default ctkPluginLocalization instance, using
+   * the default locale.
+   *
+   * Note that getLocalized() will always return a null QString
+   * for a default constructed ctkPluginLocalization object.
+   * Use <code>ctkPlugin::getPluginLocalization()</code> to create
+   * a valid instance.
+   */
+  ctkPluginLocalization();
+
+  ctkPluginLocalization(const ctkPluginLocalization& pl);
+  ~ctkPluginLocalization();
+
+  ctkPluginLocalization& operator=(const ctkPluginLocalization& other);
+
+  /**
+   * Translate <code>str</code> to a specific locale, using the
+   * specified <code>context</code>.
+   *
+   * @return The translation or a null QString, if no translation
+   *         was found.
+   */
+  QString getLocalized(const QString& context, const QString& str) const;
+
+  /**
+   * Get the locale for which this <code>ctkPluginLocalization</code>
+   * object was constructed.
+   *
+   * @return The locale for this object.
+   */
+  QLocale getLocale() const;
+
+private:
+
+  friend class ctkPlugin;
+
+  ctkPluginLocalization(const QString& msgFileName,
+                        const QLocale& locale, const QSharedPointer<ctkPlugin>& plugin);
+
+  mutable QSharedDataPointer<ctkPluginLocalizationData> d;
+};
+
+#endif // CTKPLUGINLOCALIZATION_H