[859] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * |
---|
| 4 | * |
---|
| 5 | * License notice: |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or |
---|
| 8 | * modify it under the terms of the GNU General Public License |
---|
| 9 | * as published by the Free Software Foundation; either version 2 |
---|
| 10 | * of the License, or (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | * |
---|
| 21 | * Author: |
---|
| 22 | * Fabian 'x3n' Landau |
---|
| 23 | * Co-authors: |
---|
| 24 | * ... |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
| 28 | /*! |
---|
| 29 | @file Language.h |
---|
| 30 | @brief Definition of the Language and the LanguageEntry class. |
---|
| 31 | |
---|
| 32 | The Language class is used, to get a translation of a string in the configured language. |
---|
| 33 | The string is identified by another string, the name of the entry. |
---|
| 34 | If the translation in the configured language isn't available, the default entry, defined in the code, is used. |
---|
| 35 | |
---|
| 36 | Usage: |
---|
| 37 | - Set the entry with the default string: |
---|
| 38 | Language::getLanguage()->addEntry("name of the entry", "the string to translate"); |
---|
| 39 | |
---|
| 40 | - Get the translation of the entry in the configured language: |
---|
| 41 | std::cout << Language::getLanguage()->getTranslation("name of the entry") << std::endl; |
---|
| 42 | */ |
---|
| 43 | |
---|
| 44 | #ifndef _Language_H__ |
---|
| 45 | #define _Language_H__ |
---|
| 46 | |
---|
| 47 | #include <map> |
---|
| 48 | #include <string> |
---|
| 49 | |
---|
| 50 | #include "CorePrereqs.h" |
---|
| 51 | #include "OrxonoxClass.h" |
---|
| 52 | |
---|
| 53 | namespace orxonox |
---|
| 54 | { |
---|
| 55 | typedef std::string LanguageEntryName; |
---|
| 56 | |
---|
| 57 | //! The LanguageEntry class stores the default- and the translated string of a given entry in the language file. |
---|
| 58 | class _CoreExport LanguageEntry : public OrxonoxClass |
---|
| 59 | { |
---|
| 60 | public: |
---|
| 61 | explicit LanguageEntry(const std::string& fallbackEntry); |
---|
| 62 | void setTranslation(const std::string& translation); |
---|
| 63 | void setDefault(const std::string& fallbackEntry); |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | @brief Returns the translated entry in the configured language. |
---|
| 67 | @return The translated entry |
---|
| 68 | */ |
---|
| 69 | inline const std::string& getTranslation() |
---|
| 70 | { return this->translatedEntry_; } |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | @brief Returns the default entry. |
---|
| 74 | @return The default entry |
---|
| 75 | */ |
---|
| 76 | inline const std::string& getDefault() |
---|
| 77 | { return this->fallbackEntry_; } |
---|
| 78 | |
---|
| 79 | private: |
---|
| 80 | std::string fallbackEntry_; //!< The default entry: Used, if no translation is available or no language configured |
---|
| 81 | std::string translatedEntry_; //!< The translated entry in the configured language |
---|
| 82 | bool bTranslationSet_; //!< True if the translation was set |
---|
| 83 | }; |
---|
| 84 | template class _CoreExport orxonox::ClassIdentifier<LanguageEntry>; |
---|
| 85 | template class _CoreExport orxonox::ObjectList<LanguageEntry>; |
---|
| 86 | |
---|
| 87 | //! The Language class manges the language files and entries and stores the LanguageEntry objects in a map. |
---|
| 88 | class _CoreExport Language : public OrxonoxClass |
---|
| 89 | { |
---|
| 90 | template <class T> |
---|
| 91 | friend class ClassIdentifier; // forward declaration because of the private destructor |
---|
| 92 | |
---|
| 93 | public: |
---|
| 94 | static Language& getLanguage(); |
---|
| 95 | void setConfigValues(); |
---|
| 96 | void addEntry(const LanguageEntryName& name, const std::string& entry); |
---|
| 97 | const std::string& getTranslation(const LanguageEntryName& name) const; |
---|
| 98 | |
---|
| 99 | private: |
---|
| 100 | Language(); |
---|
| 101 | Language(const Language& language) {} // don't copy |
---|
| 102 | virtual ~Language() {}; // don't delete |
---|
| 103 | |
---|
| 104 | void readDefaultLanguageFile(); |
---|
| 105 | void readTranslatedLanguageFile(); |
---|
| 106 | void writeDefaultLanguageFile() const; |
---|
| 107 | static const std::string getFileName(const std::string& language); |
---|
| 108 | LanguageEntry* createEntry(const LanguageEntryName& name, const std::string& entry); |
---|
| 109 | |
---|
| 110 | std::string language_; //!< The configured language |
---|
| 111 | std::string defaultLanguage_; //!< The default language |
---|
| 112 | std::string defaultTranslation_; //!< The returned string, if an entry unavailable entry is requested |
---|
| 113 | std::map<std::string, LanguageEntry*> languageEntries_; //!< A map to store all LanguageEntry objects and their name |
---|
| 114 | }; |
---|
| 115 | template class _CoreExport orxonox::ClassIdentifier<Language>; |
---|
| 116 | template class _CoreExport orxonox::ObjectList<Language>; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | #endif /* _Language_H__ */ |
---|