// NeL - MMORPG Framework
// Copyright (C) 2010-2019 Winch Gate Property Limited
//
// This source file has been modified by the following contributors:
// Copyright (C) 2012 Laszlo KIS-ADAM (dfighter)
// Copyright (C) 2014 Matthew LAGOE (Botanic)
// Copyright (C) 2020 Jan BOON (Kaetemi)
//
// 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"
#include
#ifdef NL_OS_MAC
#include
#endif
using namespace std;
#ifdef DEBUG_NEW
#define new DEBUG_NEW
#endif
namespace NLMISC {
CI18N::StrMapContainer CI18N::_StrMap;
CI18N::StrMapContainer CI18N::_StrMapFallback;
CI18N::StrMapContainer16 CI18N::_StrMap16;
CI18N::StrMapContainer16 CI18N::_StrMapFallback16;
bool CI18N::_StrMapLoaded = false;
const ucstring CI18N::_NotTranslatedValue16("");
const std::string CI18N::_NotTranslatedValue("");
bool CI18N::_LanguagesNamesLoaded = false;
string CI18N::_SelectedLanguageCode;
CI18N::ILoadProxy *CI18N::_LoadProxy = 0;
vector CI18N::_LanguageCodes;
vector CI18N::_LanguageNames;
std::string CI18N::_SystemLanguageCode;
bool CI18N::noResolution = false;
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");
_LanguageCodes.push_back("es");
_LanguageNames.push_back("English");
_LanguageNames.push_back("French");
_LanguageNames.push_back("German");
_LanguageNames.push_back("Russian");
_LanguageNames.push_back("Spanish");
_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();
_StrMap16.clear();
}
else
{
_StrMapLoaded = true;
}
_SelectedLanguageCode = languageCode;
loadFileIntoMap(languageCode + ".uxt", _StrMap, _StrMap16);
_StrMapFallback.clear();
_StrMapFallback16.clear();
if(!fallbackLanguageCode.empty())
{
loadFileIntoMap(fallbackLanguageCode + ".uxt", _StrMapFallback, _StrMapFallback16);
}
}
bool CI18N::loadFileIntoMap(const string &fileName, StrMapContainer &destMap, StrMapContainer16 &destMap16)
{
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