Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 3614 was 3370, checked in by rgrieder, 15 years ago

Merged resource branch back to the trunk. Changes:

  • Automated graphics loading by evaluating whether a GameState requires it
  • Using native Tcl library (x3n)

Windows users: Update your dependency package!

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