Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/core/Language.h @ 871

Last change on this file since 871 was 871, checked in by landauf, 16 years ago

merged core branch to trunk

File size: 5.5 KB
RevLine 
[704]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
[871]28/**
[720]29    @file Language.h
30    @brief Definition of the Language and the LanguageEntry class.
31
[871]32    The Language class is used, to get a localisation of a string in the configured language.
33    The string is identified by another string, the label of the entry.
[720]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:
[871]38       Language::getLanguage()->addEntry("label of the entry", "the string to translate");
[720]39
[871]40     - Get the localisation of the entry in the configured language:
41       std::cout << Language::getLanguage()->getLocalisation("name of the entry") << std::endl;
[720]42*/
43
[704]44#ifndef _Language_H__
45#define _Language_H__
46
47#include <map>
[715]48#include <string>
[704]49
50#include "CorePrereqs.h"
51#include "OrxonoxClass.h"
52
[871]53
54#define AddLanguageEntry(label, fallbackstring) \
55    orxonox::Language::getLanguage().addEntry(label, fallbackstring)
56
57#define GetLocalisation(label) \
58    orxonox::Language::getLanguage().getLocalisation(label)
59
60
[704]61namespace orxonox
62{
[871]63    // ###############################
64    // ###      LanguageEntry      ###
65    // ###############################
[720]66    //! The LanguageEntry class stores the default- and the translated string of a given entry in the language file.
[704]67    class _CoreExport LanguageEntry : public OrxonoxClass
68    {
69        public:
[715]70            explicit LanguageEntry(const std::string& fallbackEntry);
[871]71            void setLocalisation(const std::string& localisation);
[715]72            void setDefault(const std::string& fallbackEntry);
[704]73
[728]74            /**
[871]75              @brief Returns the localised entry in the configured language.
[728]76              @return The translated entry
77            */
[871]78            inline const std::string& getLocalisation()
79                { return this->localisedEntry_; }
[704]80
[728]81            /**
82              @brief Returns the default entry.
83              @return The default entry
84            */
[715]85            inline const std::string& getDefault()
[704]86                { return this->fallbackEntry_; }
87
[871]88            /**
89                @brief Sets the label of this entry.
90                @param label The label
91            */
92            inline void setLabel(const LanguageEntryLabel& label)
93                { this->label_ = label; }
94
95            /**
96                @brief Returns the label of this entry.
97                @return The label
98            */
99            inline const LanguageEntryLabel& getLabel() const
100                { return this->label_; }
101
[704]102        private:
[871]103            LanguageEntryLabel label_;              //!< The label of the entry
104            std::string fallbackEntry_;             //!< The default entry: Used, if no translation is available or no language configured
105            std::string localisedEntry_;            //!< The localised entry in the configured language
106            bool bLocalisationSet_;                 //!< True if the translation was set
[704]107    };
108
[871]109
110    // ###############################
111    // ###         Language        ###
112    // ###############################
[720]113    //! The Language class manges the language files and entries and stores the LanguageEntry objects in a map.
[704]114    class _CoreExport Language : public OrxonoxClass
115    {
[737]116        template <class T>
117        friend class ClassIdentifier; // forward declaration because of the private destructor
118
[704]119        public:
120            static Language& getLanguage();
121            void setConfigValues();
[871]122            void addEntry(const LanguageEntryLabel& label, const std::string& entry);
123            const std::string& getLocalisation(const LanguageEntryLabel& label) const;
[704]124
125        private:
126            Language();
127            Language(const Language& language) {}   // don't copy
128            virtual ~Language() {};                 // don't delete
129
130            void readDefaultLanguageFile();
131            void readTranslatedLanguageFile();
132            void writeDefaultLanguageFile() const;
[715]133            static const std::string getFileName(const std::string& language);
[871]134            LanguageEntry* createEntry(const LanguageEntryLabel& label, const std::string& entry);
[704]135
[720]136            std::string language_;                                  //!< The configured language
137            std::string defaultLanguage_;                           //!< The default language
[871]138            std::string defaultLocalisation_;                       //!< The returned string, if an entry unavailable entry is requested
139            std::map<std::string, LanguageEntry*> languageEntries_; //!< A map to store all LanguageEntry objects and their labels
[704]140    };
141}
142
143#endif /* _Language_H__ */
Note: See TracBrowser for help on using the repository browser.