2010-04-06 09:11:58 -06:00
|
|
|
/*
|
2011-06-16 15:53:39 -06:00
|
|
|
https://github.com/peterix/dfhack
|
2012-09-29 20:03:37 -06:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2010-05-26 04:24:45 -06:00
|
|
|
#include "Internal.h"
|
2011-04-10 02:19:15 -06:00
|
|
|
|
|
|
|
#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"
|
2011-03-18 01:53:59 -06:00
|
|
|
#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;
|
2012-01-27 20:36:01 -07:00
|
|
|
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"
|
2012-01-27 20:36:01 -07:00
|
|
|
#include "df/d_init.h"
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2012-01-24 09:54:12 -07:00
|
|
|
using df::global::world;
|
2012-01-27 20:36:01 -07:00
|
|
|
using df::global::d_init;
|
2014-07-21 12:26:34 -06:00
|
|
|
using df::global::gametype;
|
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
|
|
|
{
|
2012-01-27 21:01:59 -07:00
|
|
|
return (world && (world->raws.language.words.size() > 0) && (world->raws.language.translations.size() > 0));
|
2011-06-17 07:02:43 -06:00
|
|
|
}
|
|
|
|
|
2012-01-21 12:12:40 -07:00
|
|
|
bool Translation::readName(t_name & name, df::language_name * source)
|
2011-06-17 07:02:43 -06:00
|
|
|
{
|
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;
|
2011-06-17 07:02:43 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-21 12:12:40 -07:00
|
|
|
bool Translation::copyName(df::language_name * source, df::language_name * target)
|
2011-06-17 07:02:43 -06:00
|
|
|
{
|
2011-09-18 05:49:10 -06:00
|
|
|
if (source == target)
|
2011-06-17 07:02:43 -06:00
|
|
|
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;
|
2011-06-17 07:02:43 -06:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-04-23 11:30:53 -06:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-01-27 20:46:52 -07:00
|
|
|
void addNameWord (string &out, const string &word)
|
|
|
|
{
|
|
|
|
if (word.empty())
|
|
|
|
return;
|
|
|
|
if (out.length() > 0)
|
|
|
|
out.append(" ");
|
2012-04-23 11:30:53 -06:00
|
|
|
out.append(Translation::capitalize(word));
|
2012-01-27 20:46:52 -07:00
|
|
|
}
|
|
|
|
|
2012-04-07 09:08:30 -06:00
|
|
|
void Translation::setNickname(df::language_name *name, std::string nick)
|
|
|
|
{
|
|
|
|
CHECK_NULL_POINTER(name);
|
|
|
|
|
|
|
|
if (!name->has_name)
|
|
|
|
{
|
2012-12-08 09:39:57 -07:00
|
|
|
if (nick.empty())
|
|
|
|
return;
|
|
|
|
|
2012-04-07 09:08:30 -06:00
|
|
|
*name = df::language_name();
|
|
|
|
|
|
|
|
name->language = 0;
|
|
|
|
name->has_name = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
name->nickname = nick;
|
2012-12-08 09:39:57 -07:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2012-04-07 09:08:30 -06:00
|
|
|
}
|
|
|
|
|
2012-03-17 07:41:56 -06:00
|
|
|
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;
|
2012-01-27 20:46:52 -07:00
|
|
|
string word;
|
2010-08-20 06:10:05 -06:00
|
|
|
|
2012-03-17 07:41:56 -06:00
|
|
|
if (!onlyLastPart) {
|
|
|
|
if (!name->first_name.empty())
|
|
|
|
addNameWord(out, name->first_name);
|
2012-01-27 20:36:01 -07:00
|
|
|
|
2012-03-17 07:41:56 -06:00
|
|
|
if (!name->nickname.empty())
|
2012-01-27 20:36:01 -07:00
|
|
|
{
|
2012-03-17 07:41:56 -06:00
|
|
|
word = "`" + name->nickname + "'";
|
2014-07-21 12:26:34 -06:00
|
|
|
switch ((d_init && gametype) ? d_init->nickname[*gametype] : d_init_nickname::CENTRALIZE)
|
2012-03-17 07:41:56 -06:00
|
|
|
{
|
|
|
|
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);
|
2012-01-27 20:36:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2012-01-27 20:36:01 -07:00
|
|
|
word.clear();
|
2012-01-24 09:54:12 -07:00
|
|
|
if (name->words[0] >= 0)
|
2012-01-27 20:36:01 -07:00
|
|
|
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)
|
2012-01-27 20:36:01 -07:00
|
|
|
word.append(*world->raws.language.translations[name->language]->words[name->words[1]]);
|
2012-01-27 20:46:52 -07:00
|
|
|
addNameWord(out, word);
|
2010-04-06 09:11:58 -06:00
|
|
|
}
|
2020-04-01 03:16:24 -06:00
|
|
|
word.clear();
|
|
|
|
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);
|
2012-01-24 09:54:12 -07:00
|
|
|
if (name->words[6] >= 0)
|
2010-04-06 09:11:58 -06:00
|
|
|
{
|
2012-01-27 20:36:01 -07:00
|
|
|
word.clear();
|
|
|
|
word.append(*world->raws.language.translations[name->language]->words[name->words[6]]);
|
2012-01-27 20:46:52 -07:00
|
|
|
addNameWord(out, word);
|
2010-04-06 09:11:58 -06:00
|
|
|
}
|
2010-08-20 06:10:05 -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
|
|
|
{
|
2012-01-27 20:36:01 -07:00
|
|
|
word.clear();
|
2012-01-24 09:54:12 -07:00
|
|
|
if (name->words[0] >= 0)
|
2020-02-18 15:39:39 -07:00
|
|
|
word.append(world->raws.language.words[name->words[0]]->forms[name->parts_of_speech[0]]);
|
2012-01-24 09:54:12 -07:00
|
|
|
if (name->words[1] >= 0)
|
2020-02-18 15:39:39 -07:00
|
|
|
word.append(world->raws.language.words[name->words[1]]->forms[name->parts_of_speech[1]]);
|
2012-01-27 20:46:52 -07:00
|
|
|
addNameWord(out, word);
|
2010-04-06 09:11:58 -06:00
|
|
|
}
|
2020-04-01 03:16:24 -06:00
|
|
|
if (name->words[2] >= 0 || name->words[3] >= 0 || name->words[4] >= 0 || 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");
|
2020-04-01 03:16:24 -06:00
|
|
|
}
|
|
|
|
for (int i = 2; i <= 5; i++)
|
|
|
|
{
|
|
|
|
if (name->words[i] >= 0)
|
|
|
|
addNameWord(out, world->raws.language.words[name->words[i]]->forms[name->parts_of_speech[i]]);
|
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
|
|
|
|
2020-02-18 15:39:39 -07:00
|
|
|
addNameWord(out, world->raws.language.words[name->words[6]]->forms[name->parts_of_speech[6]]);
|
2010-04-06 09:11:58 -06:00
|
|
|
}
|
|
|
|
}
|
2012-01-27 20:36:01 -07:00
|
|
|
|
2010-04-06 09:11:58 -06:00
|
|
|
return out;
|
2010-05-01 18:38:18 -06:00
|
|
|
}
|