cutelyst 4.0.0
A C++ Web Framework built on top of Qt, using the simple approach of Catalyst (Perl) framework.
langselect.h
1/*
2 * SPDX-FileCopyrightText: (C) 2018-2022 Matthias Fehring <mf@huessenbergnetz.de>
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#ifndef C_UTILS_LANGSELECT_H
7#define C_UTILS_LANGSELECT_H
8
9#include <Cutelyst/cutelyst_global.h>
10#include <Cutelyst/plugin.h>
11
12#include <QLocale>
13#include <QVector>
14
15namespace Cutelyst {
16
37class Context;
38class LangSelectPrivate;
39
369class CUTELYST_PLUGIN_UTILS_LANGSELECT_EXPORT LangSelect
370 : public Plugin // clazy:exclude=ctor-missing-parent-argument
371{
372 Q_OBJECT
373 Q_DECLARE_PRIVATE(LangSelect) // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
374 Q_DISABLE_COPY(LangSelect)
375public:
379 enum Source : quint8 {
380 URLQuery = 0,
382 Session = 1,
384 Cookie =
385 2,
386 SubDomain = 3,
388 Domain =
389 4,
390 AcceptHeader =
391 254,
392 Fallback = 255
393 };
394 Q_ENUM(Source)
395
396
406 LangSelect(Application *parent, Source source);
407
414 LangSelect(Application *parent);
415
419 ~LangSelect() override;
420
428 void setSupportedLocales(const QVector<QLocale> &locales);
429
436 void setSupportedLocales(const QStringList &locales);
437
442 void addSupportedLocale(const QLocale &locale);
443
449 void addSupportedLocale(const QString &locale);
450
477 void setLocalesFromDir(const QString &path,
478 const QString &name,
479 const QString &prefix = QStringLiteral("."),
480 const QString &suffix = QStringLiteral(".qm"));
481
506 void setLocalesFromDirs(const QString &path, const QString &name);
507
513 [[nodiscard]] QVector<QLocale> supportedLocales() const;
514
518 void setQueryKey(const QString &key);
519
523 void setSessionKey(const QString &key);
524
528 void setCookieName(const QByteArray &name);
529
535 void setSubDomainMap(const QMap<QString, QLocale> &map);
536
542 void setDomainMap(const QMap<QString, QLocale> &map);
543
547 void setFallbackLocale(const QLocale &fallback);
548
554 void setDetectFromHeader(bool enabled);
555
559 void setLanguageCodeStashKey(const QString &key = QStringLiteral("c_langselect_lang"));
560
565 void setLanguageDirStashKey(const QString &key = QStringLiteral("c_langselect_dir"));
566
571 static QVector<QLocale> getSupportedLocales();
572
593 static bool fromUrlQuery(Context *c, const QString &key = QString());
594
612 static bool fromSession(Context *c, const QString &key = QString());
613
631 static bool fromCookie(Context *c, const QByteArray &name = {});
632
649 static bool
650 fromSubDomain(Context *c,
651 const QMap<QString, QLocale> &subDomainMap = QMap<QString, QLocale>());
652
668 static bool fromDomain(Context *c,
669 const QMap<QString, QLocale> &domainMap = QMap<QString, QLocale>());
670
686 static bool fromPath(Context *c, const QString &locale);
687
688protected:
695 bool setup(Application *app) override;
696
697private:
698 const std::unique_ptr<LangSelectPrivate> d_ptr;
699};
700
701} // namespace Cutelyst
702
703#endif // C_UTILS_LANGSELECT_H
The Cutelyst Application.
Definition: application.h:43
The Cutelyst Context.
Definition: context.h:38
Language selection plugin.
Definition: langselect.h:371
~LangSelect() override
The Cutelyst namespace holds all public Cutelyst API.
Definition: Mainpage.dox:8