Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/Language.h @ 7351

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

merged consolecommands3 branch back to trunk.

note: the console command interface has changed completely, but the documentation is not yet up to date. just copy an existing command and change it to your needs, it's pretty self-explanatory. also the include files related to console commands are now located in core/command/. in the game it should work exactly like before, except for some changes in the auto-completion.

  • Property svn:eol-style set to native
File size: 5.7 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    @file
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:
39       Language::getInstance()->addEntry("label of the entry", "the string to translate");
40
41     - Get the localisation of the entry in the configured language:
42       std::cout << Language::getInstance()->getLocalisation("name of the entry") << std::endl;
43*/
44
45#ifndef _Language_H__
46#define _Language_H__
47
48#include "CorePrereqs.h"
49
50#include <map>
51#include <string>
52#include <cassert>
53#include "util/Singleton.h"
54
55namespace orxonox
56{
57    // ###############################
58    // ###      LanguageEntry      ###
59    // ###############################
60    //! The LanguageEntry class stores the default- and the translated string of a given entry in the language file.
61    class _CoreExport LanguageEntry
62    {
63        public:
64            explicit LanguageEntry(const std::string& fallbackEntry);
65            void setLocalisation(const std::string& localisation);
66            void setDefault(const std::string& fallbackEntry);
67
68            /**
69              @brief Returns the localised entry in the configured language.
70              @return The translated entry
71            */
72            inline const std::string& getLocalisation()
73                { return this->localisedEntry_; }
74
75            /**
76              @brief Returns the default entry.
77              @return The default entry
78            */
79            inline const std::string& getDefault()
80                { return this->fallbackEntry_; }
81
82            /**
83                @brief Sets the label of this entry.
84                @param label The label
85            */
86            inline void setLabel(const LanguageEntryLabel& label)
87                { this->label_ = label; }
88
89            /**
90                @brief Returns the label of this entry.
91                @return The label
92            */
93            inline const LanguageEntryLabel& getLabel() const
94                { return this->label_; }
95
96        private:
97            LanguageEntryLabel label_;              //!< The label of the entry
98            std::string fallbackEntry_;             //!< The default entry: Used, if no translation is available or no language configured
99            std::string localisedEntry_;            //!< The localised entry in the configured language
100            bool bLocalisationSet_;                 //!< True if the translation was set
101    };
102
103
104    // ###############################
105    // ###         Language        ###
106    // ###############################
107    //! The Language class manges the language files and entries and stores the LanguageEntry objects in a map.
108    class _CoreExport Language : public Singleton<Language>
109    {
110        friend class Singleton<Language>;
111        friend class Core;
112
113        public:
114            Language();
115            ~Language();
116
117            void addEntry(const LanguageEntryLabel& label, const std::string& entry);
118            const std::string& getLocalisation(const LanguageEntryLabel& label, bool bError = true) const;
119
120        private:
121            Language(const Language&);
122
123            void readDefaultLanguageFile();
124            void readTranslatedLanguageFile();
125            void writeDefaultLanguageFile() const;
126            static std::string getFilename(const std::string& language);
127            LanguageEntry* createEntry(const LanguageEntryLabel& label, const std::string& entry);
128
129            std::string defaultLanguage_;                           //!< The default language
130            std::string defaultLocalisation_;                       //!< The returned string, if an entry unavailable entry is requested
131            std::map<std::string, LanguageEntry*> languageEntries_; //!< A map to store all LanguageEntry objects and their labels
132
133            static Language* singletonPtr_s;
134    };
135
136    //! Shortcut function for Language::addEntry
137    inline void AddLanguageEntry(const LanguageEntryLabel& label, const std::string& fallbackString)
138    {
139        Language::getInstance().addEntry(label, fallbackString);
140    }
141
142    //! Shortcut function for Language::getLocalisation
143    inline const std::string& GetLocalisation(const LanguageEntryLabel& label)
144    {
145        return Language::getInstance().getLocalisation(label);
146    }
147
148    //! Shortcut function for Language::getLocalisation without printing an error in case the label doesn't exist
149    inline const std::string& GetLocalisation_noerror(const LanguageEntryLabel& label)
150    {
151        return Language::getInstance().getLocalisation(label, false);
152    }
153}
154
155#endif /* _Language_H__ */
Note: See TracBrowser for help on using the repository browser.