aboutsummaryrefslogtreecommitdiffstats
path: root/src/base/language.cpp
blob: 9112c388d84420505e30e7b5f7fb6170a29c7ec7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * UltraStar Deluxe - Karaoke Game
 *
 * UltraStar Deluxe is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING. If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 */

#include <exception>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>

#include "language.hpp"
#include "utils/file.hpp"

// namespace alias for shorter class names
namespace fs = boost::filesystem;

namespace usdx
{
	Language* Language::instance = NULL;

	log4cxx::LoggerPtr Language::log =
		log4cxx::Logger::getLogger("usdx.base.language");

	LanguageNotFound::LanguageNotFound(const std::wstring& language) :
		BaseException("Language not found!"),
		language(language)
	{
	}

	const std::wstring& LanguageNotFound::get_language() const
	{
		return language;
	}


	Language::Language(void) : current_translation(NULL), default_translation(NULL)
	{
	}

	void remove_element(std::pair<std::wstring, std::map<std::wstring, std::wstring>*> element)
	{
		if (element.second) {
			element.second->clear();
		}

		delete element.second;
		element.second = NULL;
	}

	Language::~Language(void)
	{
		// clear memory for all elements in the map
		std::for_each(translations.begin(), translations.end(),
			      remove_element);
		translations.clear();

		// remove reference from singleton to make regeneration possible
		instance = NULL;
	}

	Language* Language::get_instance(void)
	{
		if (instance == NULL) {
			instance = new Language();
		}

		return instance;
	}

	void Language::parse_translation(const std::wstring& line, std::map<std::wstring, std::wstring>& map)
	{
		size_t found = line.find(L"=");
		if (found != std::wstring::npos) {
			// copy the substring until '=' and
			// transform to upper case
			std:: wstring key(found, L' ');
			std::transform(line.begin(), line.begin() + found,
				       key.begin(), toupper);
			boost::trim(key);

			std::wstring value(line.substr(found + 1));
			boost::trim_if(value, boost::is_cntrl());
			boost::trim(value);

			LOG4CXX_TRACE(log, L"Found translation from " <<
				      key << L" to " << value);

			map[key] = value;
		}
	}

	void Language::load_language(const std::wstring& language)
	{
		fs::wpath source_path = language_dir / (language + L".ini");

		if (exists(source_path)) {
			std::map<std::wstring, std::wstring> *map =
				new std::map<std::wstring, std::wstring>();

			File source(source_path);

			bool started = false;
			std::wstring line;

			while (source.stream().good()) {
				getline(source.stream(), line);

				if (started) {
					if (line[0] == L'[' && line[line.length() - 1] == L']') {
						break;
					}

					parse_translation(line, *map);
				}
				else {
					if (line.compare(L"[Text]")) {
						started = true;
					}
				}
			}

			translations[basename(source_path)] = map;
		}
	}

	void Language::load_default_language(const std::wstring& default_language)
	{
		std::map<std::wstring, std::map<std::wstring, std::wstring>*>::iterator it = translations.find(default_language);
		if (it != translations.end()) {
			LOG4CXX_DEBUG(log, L"Loading default language: " << default_language);
			load_language(default_language);
			current_translation = default_translation = translations[default_language];
			current_language = default_language;
		}
	}

	void Language::init(const fs::wpath& language_dir, const std::wstring& default_language)
	{
		if (!is_directory(language_dir)) {
			throw std::invalid_argument("language_dir should be a directory");
		}

		this->language_dir = language_dir;

		// default construction yields past-the-end
		fs::wdirectory_iterator end_it;
		for (fs::wdirectory_iterator it(language_dir); it != end_it; ++it)
		{
			if (!is_directory(it->status()) &&
			    extension(it->path()) == L".ini")
			{
				translations[basename(it->path())] = NULL;
			}
		}

		load_default_language(default_language);
	}

	void Language::set_language(const std::wstring& language)
	{
		std::map<std::wstring, std::map<std::wstring, std::wstring>*>::iterator it = translations.find(language);
		if (it == translations.end()) {
			throw LanguageNotFound(language);
		}

		if (it->second == NULL) {
			load_language(language);
		}

		current_language = std::wstring(language);
		current_translation = translations[current_language];
	}

	const std::wstring& Language::get_language(void) const
	{
		return current_language;
	}

	const std::wstring& Language::translate(const std::wstring& source) const
	{
		if (current_translation) {
			std::map<std::wstring, std::wstring>::iterator it = current_translation->find(source);
			if (it != current_translation->end()) {
				return it->second;
			}
		}

		if (default_translation) {
			std::map<std::wstring, std::wstring>::iterator it = default_translation->find(source);
			if (it != default_translation->end()) {
				return it->second;
			}
		}


		return source;
	}
};