// NeL - MMORPG Framework
// Copyright (C) 2010 Winch Gate Property Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
#include "stdmisc.h"
#include "nel/misc/path.h"
#include "nel/misc/i18n.h"
using namespace std;
namespace NLMISC {
CI18N::StrMapContainer CI18N::_StrMap;
CI18N::StrMapContainer CI18N::_StrMapFallback;
bool CI18N::_StrMapLoaded = false;
const ucstring CI18N::_NotTranslatedValue("");
bool CI18N::_LanguagesNamesLoaded = false;
string CI18N::_SelectedLanguageCode;
CI18N::ILoadProxy *CI18N::_LoadProxy = 0;
vector CI18N::_LanguageCodes;
vector CI18N::_LanguageNames;
void CI18N::setLoadProxy(ILoadProxy *loadProxy)
{
_LoadProxy = loadProxy;
}
void CI18N::initLanguages()
{
if (!_LanguagesNamesLoaded)
{
_LanguageCodes.push_back("en");
_LanguageCodes.push_back("fr");
_LanguageCodes.push_back("de");
_LanguageCodes.push_back("ru");
_LanguageNames.push_back(ucstring("English"));
_LanguageNames.push_back(ucstring("French"));
_LanguageNames.push_back(ucstring("German"));
_LanguageNames.push_back(ucstring("Russian"));
_LanguagesNamesLoaded = true;
}
}
const std::vector &CI18N::getLanguageNames()
{
initLanguages();
return _LanguageNames;
}
const std::vector &CI18N::getLanguageCodes()
{
initLanguages();
return _LanguageCodes;
}
void CI18N::load (const string &languageCode, const string &fallbackLanguageCode)
{
if (_StrMapLoaded) _StrMap.clear ();
else _StrMapLoaded = true;
_SelectedLanguageCode = languageCode;
loadFileIntoMap(languageCode + ".uxt", _StrMap);
_StrMapFallback.clear();
if(!fallbackLanguageCode.empty())
{
loadFileIntoMap(fallbackLanguageCode + ".uxt", _StrMapFallback);
}
}
bool CI18N::loadFileIntoMap(const string &fileName, StrMapContainer &destMap)
{
ucstring text;
// read in the text
if (_LoadProxy)
_LoadProxy->loadStringFile(fileName, text);
else
readTextFile(fileName, text);
// remove any comment
removeCComment(text);
ucstring::const_iterator first(text.begin()), last(text.end());
string lastReadLabel("nothing");
while (first != last)
{
skipWhiteSpace(first, last);
string label;
ucstring ucs;
if (!parseLabel(first, last, label))
{
nlwarning("I18N: Error reading label field in %s. Stop reading after %s.", fileName.c_str(), lastReadLabel.c_str());
return false;
}
lastReadLabel = label;
skipWhiteSpace(first, last);
if (!parseMarkedString('[', ']', first, last, ucs))
{
nlwarning("I18N: Error reading text for label %s in %s. Stop reading.", label.c_str(), fileName.c_str());
return false;
}
// ok, a line read.
pair