Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/Language.cc @ 1056

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

don't panic, no codechanges!
added a link to www.orxonox.net

File size: 11.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    @file Language.cc
31    @brief Implementation of the Language and the LanguageEntry classes.
32*/
33
34#include <fstream>
35
36#include "Language.h"
37#include "CoreSettings.h"
38
39namespace orxonox
40{
41    // ###############################
42    // ###      LanguageEntry      ###
43    // ###############################
44    /**
45        @brief Constructor: Sets the default entry.
46        @param fallbackEntry The default entry
47    */
48    LanguageEntry::LanguageEntry(const std::string& fallbackEntry)
49    {
50        this->fallbackEntry_ = fallbackEntry;
51        this->localisedEntry_ = fallbackEntry; // Set the localisation to the fallback entry, for the case that no translation gets assigned
52        this->bLocalisationSet_ = false;
53    }
54
55    /**
56        @brief Sets the localisation of the entry.
57        @param localisation The localisation
58    */
59    void LanguageEntry::setLocalisation(const std::string& localisation)
60    {
61        // Check if the translation is more than just an empty string
62        if ((localisation != "") && (localisation.size() > 0))
63        {
64            this->localisedEntry_ = localisation;
65            this->bLocalisationSet_ = true;
66        }
67        else
68            this->localisedEntry_ = this->fallbackEntry_;
69    }
70
71    /**
72        @brief Sets the default entry.
73        @param fallbackEntry The default entry
74    */
75    void LanguageEntry::setDefault(const std::string& fallbackEntry)
76    {
77        // If the default entry changes and the translation wasn't set yet, use the new default entry as translation
78        if (!this->bLocalisationSet_)
79            this->localisedEntry_ = fallbackEntry;
80
81        this->fallbackEntry_ = fallbackEntry;
82    }
83
84    // ###############################
85    // ###        Language         ###
86    // ###############################
87    /**
88        @brief Constructor: Reads the default language file and sets some values.
89    */
90    Language::Language()
91    {
92        this->defaultLanguage_ = "default";
93        this->defaultLocalisation_ = "ERROR: LANGUAGE ENTRY DOESN'T EXIST!";
94
95        // Read the default language file to create all known LanguageEntry objects
96        this->readDefaultLanguageFile();
97    }
98
99    /**
100        @brief Returns a reference to the only existing instance of the Language class and calls the setConfigValues() function.
101        @return The reference to the only existing instance
102    */
103    Language& Language::getLanguage()
104    {
105        static Language instance = Language();
106        return instance;
107    }
108
109    /**
110        @brief Creates a new LanguageEntry with a given label and a given default entry.
111        @param label The label of the entry
112        @param entry The default entry
113        @return The created LanguageEntry object
114    */
115    LanguageEntry* Language::createEntry(const LanguageEntryLabel& label, const std::string& entry)
116    {
117        std::map<std::string, LanguageEntry*>::const_iterator it = this->languageEntries_.find(label);
118
119        // Make sure we don't create a duplicate entry
120        if (it == this->languageEntries_.end())
121        {
122            LanguageEntry* newEntry = new LanguageEntry(entry);
123            newEntry->setLabel(label);
124            this->languageEntries_[label] = newEntry;
125            return newEntry;
126        }
127
128        COUT(2) << "Warning: Language entry " << label << " is duplicate in " << getFileName(this->defaultLanguage_) << "!" << std::endl;
129        return it->second;
130    }
131
132    /**
133        @brief Adds a new LanguageEntry, if it's not already existing.
134        @param label The label of the entry
135        @param entry The default entry
136    */
137    void Language::addEntry(const LanguageEntryLabel& label, const std::string& entry)
138    {
139        COUT(5) << "Language: Called addEntry with\n  label: " << label << "\n  entry: " <<  entry << std::endl;
140        std::map<std::string, LanguageEntry*>::const_iterator it = this->languageEntries_.find(label);
141        if (it == this->languageEntries_.end())
142        {
143            // The entry isn't available yet, meaning it's new, so create it
144            this->createEntry(label, entry);
145        }
146        else if (it->second->getDefault().compare(entry) == 0)
147        {
148            // The entry is available and the default string is the same, so return because everything is fine
149            return;
150        }
151        else
152        {
153            // The defined default entry is not the same as in the default language file - change it to the new entry
154            it->second->setDefault(entry);
155        }
156
157        // Write the default language file because either a new entry was created or an existing entry has changed
158        this->writeDefaultLanguageFile();
159
160    }
161
162    /**
163        @brief Returns the localisation of a given entry.
164        @param label The label of the entry
165        @return The localisation
166    */
167    const std::string& Language::getLocalisation(const LanguageEntryLabel& label) const
168    {
169        std::map<std::string, LanguageEntry*>::const_iterator it = this->languageEntries_.find(label);
170        if (it != this->languageEntries_.end())
171            return it->second->getLocalisation();
172        else
173        {
174            // Uh, oh, an undefined entry was requested: return the default string
175            COUT(2) << "Warning: Language entry \"" << label << "\" not found!" << std::endl;
176            return this->defaultLocalisation_;
177        }
178    }
179
180    /**
181        @brief Creates the name of the language file out of the languages name.
182        @param language The name of the language
183        @return The filename
184    */
185    const std::string Language::getFileName(const std::string& language)
186    {
187        return std::string("translation_" + language + ".lang");
188    }
189
190    /**
191        @brief Reads the default language file and creates a LanguageEntry objects for every entry.
192    */
193    void Language::readDefaultLanguageFile()
194    {
195        COUT(4) << "Read default language file." << std::endl;
196
197        // This creates the file if it's not existing
198        std::ofstream createFile;
199        createFile.open(getFileName(this->defaultLanguage_).c_str(), std::fstream::app);
200        createFile.close();
201
202        // Open the file
203        std::ifstream file;
204        file.open(getFileName(this->defaultLanguage_).c_str(), std::fstream::in);
205
206        if (!file.is_open())
207        {
208            COUT(1) << "An error occurred in Language.cc:" << std::endl;
209            COUT(1) << "Error: Couldn't open file " << getFileName(this->defaultLanguage_) << " to read the default language entries!" << std::endl;
210            return;
211        }
212
213        char line[1024];
214
215        // Iterate through the file and create the LanguageEntries
216        while (file.good() && !file.eof())
217        {
218            file.getline(line, 1024);
219            std::string lineString = std::string(line);
220
221            // Check if the line is empty
222            if ((lineString != "") && (lineString.size() > 0))
223            {
224                unsigned int pos = lineString.find('=');
225
226                // Check if the length is at least 3 and if there's an entry before and behind the =
227                if (pos > 0 && pos < (lineString.size() - 1) && lineString.size() >= 3)
228                    this->createEntry(lineString.substr(0, pos), lineString.substr(pos + 1));
229                else
230                {
231                    COUT(2) << "Warning: Invalid language entry \"" << lineString << "\" in " << getFileName(this->defaultLanguage_) << std::endl;
232                }
233            }
234        }
235
236        file.close();
237    }
238
239    /**
240        @brief Reads the language file of the configured language and assigns the localisation to the corresponding LanguageEntry object.
241    */
242    void Language::readTranslatedLanguageFile()
243    {
244        COUT(4) << "Read translated language file (" << CoreSettings::getLanguage() << ")." << std::endl;
245
246        // Open the file
247        std::ifstream file;
248        file.open(getFileName(CoreSettings::getLanguage()).c_str(), std::fstream::in);
249
250        if (!file.is_open())
251        {
252            COUT(1) << "An error occurred in Language.cc:" << std::endl;
253            COUT(1) << "Error: Couldn't open file " << getFileName(CoreSettings::getLanguage()) << " to read the translated language entries!" << std::endl;
254            CoreSettings::resetLanguage();
255            COUT(3) << "Info: Reset language to " << this->defaultLanguage_ << "." << std::endl;
256            return;
257        }
258
259        char line[1024];
260
261        // Iterate through the file and create the LanguageEntries
262        while (file.good() && !file.eof())
263        {
264            file.getline(line, 1024);
265            std::string lineString = std::string(line);
266
267            // Check if the line is empty
268            if ((lineString != "") && (lineString.size() > 0))
269            {
270                unsigned int pos = lineString.find('=');
271
272                // Check if the length is at least 3 and if there's an entry before and behind the =
273                if (pos > 0 && pos < (lineString.size() - 1) && lineString.size() >= 3)
274                {
275                    std::map<std::string, LanguageEntry*>::const_iterator it = this->languageEntries_.find(lineString.substr(0, pos));
276
277                    // Check if the entry exists
278                    if (it != this->languageEntries_.end())
279                        it->second->setLocalisation(lineString.substr(pos + 1));
280                    else
281                        this->createEntry(lineString.substr(0, pos), this->defaultLocalisation_)->setLocalisation(lineString.substr(pos + 1));
282                }
283                else
284                {
285                    COUT(2) << "Warning: Invalid language entry \"" << lineString << "\" in " << getFileName(CoreSettings::getLanguage()) << std::endl;
286                }
287            }
288        }
289
290        file.close();
291    }
292
293    /**
294        @brief Writes all default entries to the default language file.
295    */
296    void Language::writeDefaultLanguageFile() const
297    {
298        COUT(4) << "Language: Write default language file." << std::endl;
299
300        // Open the file
301        std::ofstream file;
302        file.open(getFileName(this->defaultLanguage_).c_str(), std::fstream::out);
303
304        if (!file.is_open())
305        {
306            COUT(1) << "An error occurred in Language.cc:" << std::endl;
307            COUT(1) << "Error: Couldn't open file " << getFileName(this->defaultLanguage_) << " to write the default language entries!" << std::endl;
308            return;
309        }
310
311        // Iterate through the list an write the lines into the file
312        for (std::map<std::string, LanguageEntry*>::const_iterator it = this->languageEntries_.begin(); it != this->languageEntries_.end(); ++it)
313        {
314            file << (*it).second->getLabel() << "=" << (*it).second->getDefault() << std::endl;
315        }
316
317        file.close();
318    }
319}
Note: See TracBrowser for help on using the repository browser.