Cutelyst  2.13.0
langselect.h
1 /*
2  * Copyright (C) 2018 Matthias Fehring <kontakt@buschmann23.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef C_UTILS_LANGSELECT_H
20 #define C_UTILS_LANGSELECT_H
21 
22 #include <Cutelyst/cutelyst_global.h>
23 #include <Cutelyst/plugin.h>
24 #include <QVector>
25 #include <QLocale>
26 
27 namespace Cutelyst {
28 
47 class Context;
48 class LangSelectPrivate;
49 
312 class CUTELYST_PLUGIN_UTILS_LANGSELECT_EXPORT LangSelect : public Plugin
313 {
314  Q_OBJECT
315  Q_DECLARE_PRIVATE(LangSelect)
316 public:
320  enum Source : quint8 {
321  URLQuery = 0,
322  Session = 1,
323  Cookie = 2,
324  SubDomain = 3,
325  Domain = 4,
326  AcceptHeader = 254,
327  Fallback = 255
328  };
329  Q_ENUM(Source)
330 
331 
341  LangSelect(Application *parent, Source source);
342 
349  LangSelect(Application *parent);
350 
354  virtual ~LangSelect() override;
355 
363  void setSupportedLocales(const QVector<QLocale> &locales);
364 
371  void setSupportedLocales(const QStringList &locales);
372 
377  void addSupportedLocale(const QLocale &locale);
378 
384  void addSupportedLocale(const QString &locale);
385 
408  void setLocalesFromDir(const QString &path, const QString &name, const QString &prefix = QStringLiteral("."), const QString &suffix = QStringLiteral(".qm"));
409 
431  void setLocalesFromDirs(const QString &path, const QString &name);
432 
437  QVector<QLocale> supportedLocales() const;
438 
442  void setQueryKey(const QString &key);
443 
447  void setSessionKey(const QString &key);
448 
452  void setCookieName(const QString &name);
453 
459  void setSubDomainMap(const QMap<QString,QLocale> &map);
460 
466  void setDomainMap(const QMap<QString,QLocale> &map);
467 
471  void setFallbackLocale(const QLocale &fallback);
472 
478  void setDetectFromHeader(bool enabled);
479 
483  void setLanguageCodeStashKey(const QString &key = QStringLiteral("c_langselect_lang"));
484 
489  void setLanguageDirStashKey(const QString &key = QStringLiteral("c_langselect_dir"));
490 
495  static QVector<QLocale> getSupportedLocales();
496 
514  static bool fromUrlQuery(Context *c, const QString &key = QString());
515 
531  static bool fromSession(Context *c, const QString &key = QString());
532 
548  static bool fromCookie(Context *c, const QString &name = QString());
549 
563  static bool fromSubDomain(Context *c, const QMap<QString, QLocale> &subDomainMap = QMap<QString, QLocale>());
564 
578  static bool fromDomain(Context *c, const QMap<QString, QLocale> &domainMap = QMap<QString, QLocale>());
579 
594  static bool fromPath(Context *c, const QString &locale);
595 
596 protected:
603  virtual bool setup(Application *app) override;
604 
605 private:
606  LangSelectPrivate *const d_ptr;
607 };
608 
609 }
610 
611 #endif // C_UTILS_LANGSELECT_H
Cutelyst::Plugin
Definition: plugin.h:30
Cutelyst::Session
Definition: session.h:105
Cutelyst::Application
The Cutelyst Application.
Definition: application.h:55
Cutelyst::Context
The Cutelyst Context.
Definition: context.h:50
Cutelyst::LangSelect::Source
Source
Definition: langselect.h:320
Cutelyst
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:7
Cutelyst::LangSelect
Language selection plugin.
Definition: langselect.h:312