stefanfrings::TemplateLoader Class Reference

Loads localized versions of template files. More...

#include <templateloader.h>

Inheritance diagram for stefanfrings::TemplateLoader:
Collaboration diagram for stefanfrings::TemplateLoader:

Public Member Functions

 TemplateLoader (const QSettings *settings, QObject *parent=nullptr)
 Constructor. More...
virtual ~TemplateLoader ()
Template getTemplate (const QString templateName, const QString locales=QString())
 Get a template for a given locale. More...

Protected Member Functions

virtual QString tryFile (const QString localizedName)
 Try to get a file from cache or filesystem. More...

Protected Attributes

QString templatePath
 Directory where the templates are searched.
QString fileNameSuffix
 Suffix to the filenames.
QTextCodec * textCodec
 Codec for decoding the files.

Detailed Description

Loads localized versions of template files.

If the caller requests a file with the name "index" and the suffix is ".tpl" and the requested locale is "de_DE, de, en-US", then files are searched in the following order:

  • index-de_DE.tpl
  • index-de.tpl
  • index-en_US.tpl
  • index-en.tpl
  • index.tpl

The following settings are required:


The path is relative to the directory of the config file. In case of windows, if the settings are in the registry, the path is relative to the current working directory.

See also

Definition at line 40 of file templateloader.h.

Constructor & Destructor Documentation

◆ TemplateLoader()

TemplateLoader::TemplateLoader ( const QSettings *  settings,
QObject *  parent = nullptr 


settingsconfigurations settings
parentparent object

Definition at line 15 of file templateloader.cpp.

Member Function Documentation

◆ getTemplate()

Template TemplateLoader::getTemplate ( const QString  templateName,
const QString  locales = QString() 

Get a template for a given locale.

This method is thread safe.

templateNamebase name of the template file, without suffix and without locale
localesRequested locale(s), e.g. "de_DE, en_EN". Strings in the format of the HTTP header Accept-Locale may be used. Badly formatted parts in the string are silently ignored.
If the template cannot be loaded, an error message is logged and an empty template is returned.

Definition at line 67 of file templateloader.cpp.

◆ tryFile()

QString TemplateLoader::tryFile ( const QString  localizedName)

Try to get a file from cache or filesystem.

localizedNameName of the template with locale to find
The template document, or empty string if not found

Reimplemented in stefanfrings::TemplateCache.

Definition at line 45 of file templateloader.cpp.

The documentation for this class was generated from the following files: