[1505] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | /** |
---|
[2171] | 30 | @file |
---|
[1505] | 31 | @brief Definition of the Language and the LanguageEntry class. |
---|
| 32 | |
---|
| 33 | The Language class is used, to get a localisation of a string in the configured language. |
---|
| 34 | The string is identified by another string, the label of the entry. |
---|
| 35 | If the translation in the configured language isn't available, the default entry, defined in the code, is used. |
---|
| 36 | |
---|
| 37 | Usage: |
---|
| 38 | - Set the entry with the default string: |
---|
[3370] | 39 | Language::getInstance()->addEntry("label of the entry", "the string to translate"); |
---|
[1505] | 40 | |
---|
| 41 | - Get the localisation of the entry in the configured language: |
---|
[3370] | 42 | std::cout << Language::getInstance()->getLocalisation("name of the entry") << std::endl; |
---|
[1505] | 43 | */ |
---|
| 44 | |
---|
| 45 | #ifndef _Language_H__ |
---|
| 46 | #define _Language_H__ |
---|
| 47 | |
---|
| 48 | #include "CorePrereqs.h" |
---|
| 49 | |
---|
| 50 | #include <map> |
---|
| 51 | #include <string> |
---|
[2662] | 52 | #include <cassert> |
---|
[3370] | 53 | #include "util/Singleton.h" |
---|
[1505] | 54 | |
---|
| 55 | #define AddLanguageEntry(label, fallbackstring) \ |
---|
[3370] | 56 | orxonox::Language::getInstance().addEntry(label, fallbackstring) |
---|
[1505] | 57 | |
---|
| 58 | #define GetLocalisation(label) \ |
---|
[3370] | 59 | orxonox::Language::getInstance().getLocalisation(label) |
---|
[1505] | 60 | |
---|
| 61 | |
---|
| 62 | namespace orxonox |
---|
| 63 | { |
---|
| 64 | // ############################### |
---|
| 65 | // ### LanguageEntry ### |
---|
| 66 | // ############################### |
---|
| 67 | //! The LanguageEntry class stores the default- and the translated string of a given entry in the language file. |
---|
| 68 | class _CoreExport LanguageEntry |
---|
| 69 | { |
---|
| 70 | public: |
---|
| 71 | explicit LanguageEntry(const std::string& fallbackEntry); |
---|
| 72 | void setLocalisation(const std::string& localisation); |
---|
| 73 | void setDefault(const std::string& fallbackEntry); |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | @brief Returns the localised entry in the configured language. |
---|
| 77 | @return The translated entry |
---|
| 78 | */ |
---|
| 79 | inline const std::string& getLocalisation() |
---|
| 80 | { return this->localisedEntry_; } |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | @brief Returns the default entry. |
---|
| 84 | @return The default entry |
---|
| 85 | */ |
---|
| 86 | inline const std::string& getDefault() |
---|
| 87 | { return this->fallbackEntry_; } |
---|
| 88 | |
---|
| 89 | /** |
---|
| 90 | @brief Sets the label of this entry. |
---|
| 91 | @param label The label |
---|
| 92 | */ |
---|
| 93 | inline void setLabel(const LanguageEntryLabel& label) |
---|
| 94 | { this->label_ = label; } |
---|
| 95 | |
---|
| 96 | /** |
---|
| 97 | @brief Returns the label of this entry. |
---|
| 98 | @return The label |
---|
| 99 | */ |
---|
| 100 | inline const LanguageEntryLabel& getLabel() const |
---|
| 101 | { return this->label_; } |
---|
| 102 | |
---|
| 103 | private: |
---|
| 104 | LanguageEntryLabel label_; //!< The label of the entry |
---|
| 105 | std::string fallbackEntry_; //!< The default entry: Used, if no translation is available or no language configured |
---|
| 106 | std::string localisedEntry_; //!< The localised entry in the configured language |
---|
| 107 | bool bLocalisationSet_; //!< True if the translation was set |
---|
| 108 | }; |
---|
| 109 | |
---|
| 110 | |
---|
| 111 | // ############################### |
---|
| 112 | // ### Language ### |
---|
| 113 | // ############################### |
---|
| 114 | //! The Language class manges the language files and entries and stores the LanguageEntry objects in a map. |
---|
[3370] | 115 | class _CoreExport Language : public Singleton<Language> |
---|
[1505] | 116 | { |
---|
[3370] | 117 | friend class Singleton<Language>; |
---|
[3280] | 118 | friend class CoreConfiguration; |
---|
[1505] | 119 | |
---|
| 120 | public: |
---|
[2662] | 121 | Language(); |
---|
| 122 | ~Language(); |
---|
| 123 | |
---|
[1505] | 124 | void addEntry(const LanguageEntryLabel& label, const std::string& entry); |
---|
| 125 | const std::string& getLocalisation(const LanguageEntryLabel& label) const; |
---|
| 126 | |
---|
| 127 | private: |
---|
[2662] | 128 | Language(const Language&); |
---|
[1505] | 129 | |
---|
| 130 | void readDefaultLanguageFile(); |
---|
| 131 | void readTranslatedLanguageFile(); |
---|
| 132 | void writeDefaultLanguageFile() const; |
---|
[3280] | 133 | static std::string getFilename(const std::string& language); |
---|
[1505] | 134 | LanguageEntry* createEntry(const LanguageEntryLabel& label, const std::string& entry); |
---|
| 135 | |
---|
| 136 | std::string defaultLanguage_; //!< The default language |
---|
| 137 | std::string defaultLocalisation_; //!< The returned string, if an entry unavailable entry is requested |
---|
| 138 | std::map<std::string, LanguageEntry*> languageEntries_; //!< A map to store all LanguageEntry objects and their labels |
---|
[2662] | 139 | |
---|
[3370] | 140 | static Language* singletonPtr_s; |
---|
[1505] | 141 | }; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | #endif /* _Language_H__ */ |
---|