Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/doc/src/libraries/core/Language.h @ 7363

Last change on this file since 7363 was 7363, checked in by landauf, 14 years ago

assigned a group to each header file in the core library

  • Property svn:eol-style set to native
File size: 5.8 KB
Line 
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/**
30    @defgroup Language Language
31    @ingroup Core
32*/
33
34/**
35    @file
36    @ingroup Language
37    @brief Definition of the Language and the LanguageEntry class.
38
39    The Language class is used, to get a localisation of a string in the configured language.
40    The string is identified by another string, the label of the entry.
41    If the translation in the configured language isn't available, the default entry, defined in the code, is used.
42
43    Usage:
44     - Set the entry with the default string:
45       Language::getInstance()->addEntry("label of the entry", "the string to translate");
46
47     - Get the localisation of the entry in the configured language:
48       std::cout << Language::getInstance()->getLocalisation("name of the entry") << std::endl;
49*/
50
51#ifndef _Language_H__
52#define _Language_H__
53
54#include "CorePrereqs.h"
55
56#include <map>
57#include <string>
58#include <cassert>
59#include "util/Singleton.h"
60
61namespace orxonox
62{
63    // ###############################
64    // ###      LanguageEntry      ###
65    // ###############################
66    //! The LanguageEntry class stores the default- and the translated string of a given entry in the language file.
67    class _CoreExport LanguageEntry
68    {
69        public:
70            explicit LanguageEntry(const std::string& fallbackEntry);
71            void setLocalisation(const std::string& localisation);
72            void setDefault(const std::string& fallbackEntry);
73
74            /**
75              @brief Returns the localised entry in the configured language.
76              @return The translated entry
77            */
78            inline const std::string& getLocalisation()
79                { return this->localisedEntry_; }
80
81            /**
82              @brief Returns the default entry.
83              @return The default entry
84            */
85            inline const std::string& getDefault()
86                { return this->fallbackEntry_; }
87
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
102        private:
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
107    };
108
109
110    // ###############################
111    // ###         Language        ###
112    // ###############################
113    //! The Language class manges the language files and entries and stores the LanguageEntry objects in a map.
114    class _CoreExport Language : public Singleton<Language>
115    {
116        friend class Singleton<Language>;
117        friend class Core;
118
119        public:
120            Language();
121            ~Language();
122
123            void addEntry(const LanguageEntryLabel& label, const std::string& entry);
124            const std::string& getLocalisation(const LanguageEntryLabel& label, bool bError = true) const;
125
126        private:
127            Language(const Language&);
128
129            void readDefaultLanguageFile();
130            void readTranslatedLanguageFile();
131            void writeDefaultLanguageFile() const;
132            static std::string getFilename(const std::string& language);
133            LanguageEntry* createEntry(const LanguageEntryLabel& label, const std::string& entry);
134
135            std::string defaultLanguage_;                           //!< The default language
136            std::string defaultLocalisation_;                       //!< The returned string, if an entry unavailable entry is requested
137            std::map<std::string, LanguageEntry*> languageEntries_; //!< A map to store all LanguageEntry objects and their labels
138
139            static Language* singletonPtr_s;
140    };
141
142    //! Shortcut function for Language::addEntry
143    inline void AddLanguageEntry(const LanguageEntryLabel& label, const std::string& fallbackString)
144    {
145        Language::getInstance().addEntry(label, fallbackString);
146    }
147
148    //! Shortcut function for Language::getLocalisation
149    inline const std::string& GetLocalisation(const LanguageEntryLabel& label)
150    {
151        return Language::getInstance().getLocalisation(label);
152    }
153
154    //! Shortcut function for Language::getLocalisation without printing an error in case the label doesn't exist
155    inline const std::string& GetLocalisation_noerror(const LanguageEntryLabel& label)
156    {
157        return Language::getInstance().getLocalisation(label, false);
158    }
159}
160
161#endif /* _Language_H__ */
Note: See TracBrowser for help on using the repository browser.