12 #include <QTextStream>
13 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
14 #include <QRegularExpression>
19 using namespace stefanfrings;
27 if (QDir::isRelativePath(
templatePath) && settings->format()!=QSettings::NativeFormat)
32 QFileInfo configFile(settings->fileName());
36 QString encoding=settings->value(
"encoding").toString();
37 if (encoding.isEmpty())
43 textCodec=QTextCodec::codecForName(encoding.toLocal8Bit());
45 qDebug(
"TemplateLoader: path=%s, codec=%s",qPrintable(
templatePath),qPrintable(encoding));
54 qDebug(
"TemplateCache: trying file %s",qPrintable(fileName));
57 file.open(QIODevice::ReadOnly);
58 QString document=
textCodec->toUnicode(file.readAll());
62 qCritical(
"TemplateLoader: cannot load file %s, %s",qPrintable(fileName),qPrintable(file.errorString()));
77 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
78 QStringList locs=locales.split(
',',Qt::SkipEmptyParts);
80 QStringList locs=locales.split(
',',QString::SkipEmptyParts);
84 foreach (QString loc,locs)
86 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
87 loc.replace(QRegularExpression(
";.*"),
"");
89 loc.replace(QRegExp(
";.*"),
"");
93 QString localizedName=templateName+
"-"+loc.trimmed();
94 if (!tried.contains(localizedName))
96 QString document=
tryFile(localizedName);
97 if (!document.isEmpty()) {
98 return Template(document,localizedName);
100 tried.insert(localizedName);
105 foreach (QString loc,locs)
107 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
108 loc.replace(QRegularExpression(
"[;_-].*"),
"");
110 loc.replace(QRegExp(
"[;_-].*"),
"");
112 QString localizedName=templateName+
"-"+loc.trimmed();
113 if (!tried.contains(localizedName))
115 QString document=
tryFile(localizedName);
116 if (!document.isEmpty())
118 return Template(document,localizedName);
120 tried.insert(localizedName);
125 QString document=
tryFile(templateName);
126 if (!document.isEmpty())
128 return Template(document,templateName);
131 qCritical(
"TemplateCache: cannot find template %s",qPrintable(templateName));
QString fileNameSuffix
Suffix to the filenames.
Template getTemplate(const QString templateName, const QString locales=QString())
Get a template for a given locale.
TemplateLoader(const QSettings *settings, QObject *parent=nullptr)
Constructor.
QTextCodec * textCodec
Codec for decoding the files.
virtual QString tryFile(const QString localizedName)
Try to get a file from cache or filesystem.
virtual ~TemplateLoader()
Destructor.
QString templatePath
Directory where the templates are searched.
Enhanced version of QString for template processing.