2 Copyright 2007 Mark Calderbank
4 This file is part of VOMP.
6 VOMP is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 VOMP is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with VOMP; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
29 I18n::I18n(char* tconfigDir)
31 configDir = tconfigDir;
34 void I18n::findLanguages(void)
42 string l10nGlob = configDir;
43 l10nGlob += "/l10n/*";
44 glob(l10nGlob.c_str(), 0, NULL, &globbuf);
45 for (unsigned int i=0; i < globbuf.gl_pathc; i++)
47 FILE *f = fopen(globbuf.gl_pathv[i], "r");
50 while (fgets(line, 1000, f) && strncmp(line, "l10n-vomp:", 10) == 0)
52 string langline = line;
55 string::size_type pos_start, pos_end;
56 pos_start = langline.find_first_not_of(" \t\r\n", 10);
57 if (pos_start == string::npos) break;
58 pos_end = langline.find_first_of(" \t", pos_start);
59 if (pos_end == string::npos) break;
60 code = langline.substr(pos_start, pos_end - pos_start);
61 pos_start = langline.find_first_not_of(" \t\r\n", pos_end);
62 if (pos_start == string::npos) break;
63 pos_end = langline.find_last_not_of(" \t\r\n");
64 name = langline.substr(pos_start, pos_end + 1 - pos_start);
65 CodeList[code] = name;
66 FileList.insert(lang_file(code, globbuf.gl_pathv[i]));
74 I18n::trans_table I18n::getLanguageContent(const string code)
76 trans_table Translations;
77 if (CodeList.count(code) == 0) return Translations;
80 pair<lang_file_list::const_iterator, lang_file_list::const_iterator> range;
81 range = FileList.equal_range(code);
82 lang_file_list::const_iterator iter;
83 for (iter = range.first; iter != range.second; ++iter)
88 f = fopen((*iter).second.c_str(), "r");
91 while (fgets(line, 1000, f))
94 string::size_type offset = 0;
95 string fileline = line;
96 if (fileline.compare(0, 2, "x:") == 0)
97 { // New key to be translated
98 linetype = 1; offset = 2;
100 if (fileline.compare(0, code.size() + 1, code + ":") == 0)
101 { // Translation for previous key
102 if (key.empty()) continue; // Translation without preceding key
103 linetype = 2; offset = code.size() + 1;
107 string::size_type start, end;
108 start = fileline.find_first_not_of(" \t\r\n",offset);
109 if (start == string::npos)
111 if (linetype == 2) Translations[key].clear();
114 end = fileline.find_last_not_of(" \t\r\n");
115 string text = fileline.substr(start, end + 1 - start);
116 if (text.length() > 1) // Strip quotes if at both ends
118 if (text[0] == '"' && text[text.length()-1] == '"')
119 text = text.substr(1, text.length()-2);
121 if (linetype == 1) key = text;
122 if (linetype == 2) Translations[key] = text;
131 const I18n::lang_code_list& I18n::getLanguageList(void)