File indexing completed on 2024-11-24 03:56:29

0001 /*
0002  * SPDX-FileCopyrightText: 2019-2023 Mattia Basaglia <dev@dragon.best>
0003  *
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include <QString>
0010 #include <QMap>
0011 
0012 class QTranslator;
0013 
0014 namespace app {
0015 
0016 class TranslationService
0017 {
0018 public:
0019     static TranslationService& instance()
0020     {
0021         static TranslationService instance;
0022         return instance;
0023     }
0024 
0025     void change_lang_code ( QString code );
0026 
0027     /**
0028      * \brief Initialize the resource system
0029      */
0030     void initialize(QString default_lang_code="en");
0031 
0032     /**
0033      *  \brief Determine human readable language name from ISO 639-1 code
0034      *
0035      *  Depending on Qt version the returned string is either in English or
0036      *  in the language itself.
0037      *
0038      *  If lang_code is not rectognised, a null string is returned
0039     */
0040     QString language_name ( QString lang_code );
0041 
0042 
0043     /**
0044      *  \brief Register a translation
0045      *
0046      *  \param name Human-readable language name
0047      *  \param code ISO 639-1 language code
0048      *  \param file Path to the translation file, if empty no file gets loaded
0049     */
0050     void register_translation ( QString name, QString code, QString file );
0051 
0052 
0053     QString current_language_name();
0054     QString current_language_code();
0055     QTranslator* translator();
0056 
0057     /**
0058      *  \brief Map of language names to codes
0059      */
0060     const QMap<QString, QString>& available_languages();
0061 
0062 private:
0063     TranslationService() = default;
0064     TranslationService(const TranslationService&) = delete;
0065     ~TranslationService() = default;
0066 
0067 
0068     QMap<QString, QString> lang_names; ///< map lang_name -> lang_code
0069     QMap<QString, QTranslator*> translators; ///< map lang_code -> translator
0070     QString current_language;
0071 
0072 
0073 };
0074 
0075 } // namespace app