dfhack/library/modules/Translation.cpp

234 lines
6.5 KiB
C++

2010-04-06 09:11:58 -06:00
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2012 Petr Mrázek (peterix@gmail.com)
2010-04-06 09:11:58 -06:00
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "Internal.h"
#include <string>
#include <vector>
#include <map>
using namespace std;
2011-12-31 04:48:42 -07:00
#include "modules/Translation.h"
#include "VersionInfo.h"
#include "MemAccess.h"
#include "Types.h"
#include "ModuleFactory.h"
2011-12-31 04:48:42 -07:00
#include "Core.h"
2012-04-06 01:21:28 -06:00
#include "Error.h"
2012-01-21 12:12:40 -07:00
2010-04-06 09:11:58 -06:00
using namespace DFHack;
using namespace df::enums;
2010-04-06 09:11:58 -06:00
2012-01-24 09:54:12 -07:00
#include "DataDefs.h"
#include "df/world.h"
#include "df/d_init.h"
2012-01-24 09:54:12 -07:00
using df::global::world;
using df::global::d_init;
2010-04-06 09:11:58 -06:00
2012-01-24 09:54:12 -07:00
bool Translation::IsValid ()
2010-04-06 09:11:58 -06:00
{
return (world && (world->raws.language.words.size() > 0) && (world->raws.language.translations.size() > 0));
}
2012-01-21 12:12:40 -07:00
bool Translation::readName(t_name & name, df::language_name * source)
{
2011-09-18 05:49:10 -06:00
strncpy(name.first_name,source->first_name.c_str(),127);
2012-01-21 12:12:40 -07:00
strncpy(name.nickname,source->nickname.c_str(),127);
2011-09-18 05:49:10 -06:00
memcpy(&name.parts_of_speech, &source->parts_of_speech, sizeof (source->parts_of_speech));
memcpy(&name.words, &source->words, sizeof (source->words));
name.language = source->language;
name.has_name = source->has_name;
return true;
}
2012-01-21 12:12:40 -07:00
bool Translation::copyName(df::language_name * source, df::language_name * target)
{
2011-09-18 05:49:10 -06:00
if (source == target)
return true;
2011-09-18 05:49:10 -06:00
target->first_name = source->first_name;
2012-01-21 12:12:40 -07:00
target->nickname = source->nickname;
2012-01-24 09:54:12 -07:00
for (int i = 0; i < 7; i++)
{
target->words[i] = source->words[i];
target->parts_of_speech[i] = source->parts_of_speech[i];
}
2011-09-18 05:49:10 -06:00
target->language = source->language;
target->unknown = source->unknown;
2012-01-24 09:54:12 -07:00
target->has_name = source->has_name;
return true;
}
std::string Translation::capitalize(const std::string &str, bool all_words)
{
string upper = str;
if (!upper.empty())
{
upper[0] = toupper(upper[0]);
if (all_words)
{
for (size_t i = 1; i < upper.size(); i++)
if (isspace(upper[i-1]))
upper[i] = toupper(upper[i]);
}
}
return upper;
}
void addNameWord (string &out, const string &word)
{
if (word.empty())
return;
if (out.length() > 0)
out.append(" ");
out.append(Translation::capitalize(word));
}
void Translation::setNickname(df::language_name *name, std::string nick)
{
CHECK_NULL_POINTER(name);
if (!name->has_name)
{
if (nick.empty())
return;
*name = df::language_name();
name->language = 0;
name->has_name = true;
}
name->nickname = nick;
// If the nick is empty, check if this made the whole name empty
if (name->nickname.empty() && name->first_name.empty())
{
bool has_words = false;
for (int i = 0; i < 7; i++)
if (name->words[i] >= 0)
has_words = true;
if (!has_words)
name->has_name = false;
}
}
string Translation::TranslateName(const df::language_name * name, bool inEnglish, bool onlyLastPart)
2010-04-06 09:11:58 -06:00
{
2012-04-06 01:21:28 -06:00
CHECK_NULL_POINTER(name);
2010-04-06 09:11:58 -06:00
string out;
string word;
if (!onlyLastPart) {
if (!name->first_name.empty())
addNameWord(out, name->first_name);
if (!name->nickname.empty())
{
word = "`" + name->nickname + "'";
switch (d_init ? d_init->nickname_dwarf : d_init_nickname::CENTRALIZE)
{
case d_init_nickname::REPLACE_ALL:
out = word;
return out;
case d_init_nickname::REPLACE_FIRST:
out = "";
break;
case d_init_nickname::CENTRALIZE:
break;
}
addNameWord(out, word);
}
}
if (!inEnglish)
2010-04-06 09:11:58 -06:00
{
2012-01-24 09:54:12 -07:00
if (name->words[0] >= 0 || name->words[1] >= 0)
2010-04-06 09:11:58 -06:00
{
word.clear();
2012-01-24 09:54:12 -07:00
if (name->words[0] >= 0)
word.append(*world->raws.language.translations[name->language]->words[name->words[0]]);
2012-01-24 09:54:12 -07:00
if (name->words[1] >= 0)
word.append(*world->raws.language.translations[name->language]->words[name->words[1]]);
addNameWord(out, word);
2010-04-06 09:11:58 -06:00
}
2012-01-24 09:54:12 -07:00
if (name->words[5] >= 0)
2010-04-06 09:11:58 -06:00
{
word.clear();
2012-01-24 09:54:12 -07:00
for (int i = 2; i <= 5; i++)
if (name->words[i] >= 0)
word.append(*world->raws.language.translations[name->language]->words[name->words[i]]);
addNameWord(out, word);
2010-04-06 09:11:58 -06:00
}
2012-01-24 09:54:12 -07:00
if (name->words[6] >= 0)
2010-04-06 09:11:58 -06:00
{
word.clear();
word.append(*world->raws.language.translations[name->language]->words[name->words[6]]);
addNameWord(out, word);
2010-04-06 09:11:58 -06:00
}
}
2010-04-06 09:11:58 -06:00
else
{
2012-01-24 09:54:12 -07:00
if (name->words[0] >= 0 || name->words[1] >= 0)
2010-04-06 09:11:58 -06:00
{
word.clear();
2012-01-24 09:54:12 -07:00
if (name->words[0] >= 0)
word.append(world->raws.language.words[name->words[0]]->forms[name->parts_of_speech[0].value]);
2012-01-24 09:54:12 -07:00
if (name->words[1] >= 0)
word.append(world->raws.language.words[name->words[1]]->forms[name->parts_of_speech[1].value]);
addNameWord(out, word);
2010-04-06 09:11:58 -06:00
}
2012-01-24 09:54:12 -07:00
if (name->words[5] >= 0)
2010-04-06 09:11:58 -06:00
{
2012-01-24 09:54:12 -07:00
if (out.length() > 0)
2010-04-06 09:11:58 -06:00
out.append(" the");
else
out.append("The");
2012-01-24 09:54:12 -07:00
for (int i = 2; i <= 5; i++)
2010-04-06 09:11:58 -06:00
{
2012-01-24 09:54:12 -07:00
if (name->words[i] >= 0)
addNameWord(out, world->raws.language.words[name->words[i]]->forms[name->parts_of_speech[i].value]);
2010-04-06 09:11:58 -06:00
}
}
2012-01-24 09:54:12 -07:00
if (name->words[6] >= 0)
2010-04-06 09:11:58 -06:00
{
2012-01-24 09:54:12 -07:00
if (out.length() > 0)
2010-04-06 09:11:58 -06:00
out.append(" of");
else
out.append("Of");
2012-01-24 09:54:12 -07:00
addNameWord(out, world->raws.language.words[name->words[6]]->forms[name->parts_of_speech[6].value]);
2010-04-06 09:11:58 -06:00
}
}
2010-04-06 09:11:58 -06:00
return out;
2010-05-01 18:38:18 -06:00
}