mirror of
https://gitlab.com/OpenMW/openmw.git
synced 2025-04-30 05:47:57 +03:00
Imported Upstream version 0.26.0
This commit is contained in:
commit
9a2b6c69b6
1398 changed files with 212217 additions and 0 deletions
115
components/translation/translation.cpp
Normal file
115
components/translation/translation.cpp
Normal file
|
@ -0,0 +1,115 @@
|
|||
#include "translation.hpp"
|
||||
#include <components/misc/stringops.hpp>
|
||||
|
||||
#include <fstream>
|
||||
|
||||
namespace Translation
|
||||
{
|
||||
void Storage::loadTranslationData(const Files::Collections& dataFileCollections,
|
||||
const std::string& esmFileName)
|
||||
{
|
||||
std::string esmNameNoExtension(Misc::StringUtils::lowerCase(esmFileName));
|
||||
//changing the extension
|
||||
size_t dotPos = esmNameNoExtension.rfind('.');
|
||||
if (dotPos != std::string::npos)
|
||||
esmNameNoExtension.resize(dotPos);
|
||||
|
||||
loadData(mCellNamesTranslations, esmNameNoExtension, ".cel", dataFileCollections);
|
||||
loadData(mPhraseForms, esmNameNoExtension, ".top", dataFileCollections);
|
||||
loadData(mTopicIDs, esmNameNoExtension, ".mrk", dataFileCollections);
|
||||
}
|
||||
|
||||
void Storage::loadData(ContainerType& container,
|
||||
const std::string& fileNameNoExtension,
|
||||
const std::string& extension,
|
||||
const Files::Collections& dataFileCollections)
|
||||
{
|
||||
std::string fileName = fileNameNoExtension + extension;
|
||||
|
||||
if (dataFileCollections.getCollection (extension).doesExist (fileName))
|
||||
{
|
||||
std::string path = dataFileCollections.getCollection (extension).getPath (fileName).string();
|
||||
|
||||
std::ifstream stream (path.c_str());
|
||||
|
||||
if (!stream.is_open())
|
||||
throw std::runtime_error ("failed to open translation file: " + fileName);
|
||||
|
||||
loadDataFromStream(container, stream);
|
||||
}
|
||||
}
|
||||
|
||||
void Storage::loadDataFromStream(ContainerType& container, std::istream& stream)
|
||||
{
|
||||
std::string line;
|
||||
while (!stream.eof())
|
||||
{
|
||||
std::getline( stream, line );
|
||||
if (!line.empty() && *line.rbegin() == '\r')
|
||||
line.resize(line.size() - 1);
|
||||
|
||||
if (!line.empty())
|
||||
{
|
||||
line = mEncoder->getUtf8(line);
|
||||
|
||||
size_t tab_pos = line.find('\t');
|
||||
if (tab_pos != std::string::npos && tab_pos > 0 && tab_pos < line.size() - 1)
|
||||
{
|
||||
std::string key = line.substr(0, tab_pos);
|
||||
std::string value = line.substr(tab_pos + 1);
|
||||
|
||||
if (!key.empty() && !value.empty())
|
||||
container.insert(std::make_pair(key, value));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string Storage::translateCellName(const std::string& cellName) const
|
||||
{
|
||||
std::map<std::string, std::string>::const_iterator entry =
|
||||
mCellNamesTranslations.find(cellName);
|
||||
|
||||
if (entry == mCellNamesTranslations.end())
|
||||
return cellName;
|
||||
|
||||
return entry->second;
|
||||
}
|
||||
|
||||
std::string Storage::topicID(const std::string& phrase) const
|
||||
{
|
||||
std::string result = topicStandardForm(phrase);
|
||||
|
||||
//seeking for the topic ID
|
||||
std::map<std::string, std::string>::const_iterator topicIDIterator =
|
||||
mTopicIDs.find(result);
|
||||
|
||||
if (topicIDIterator != mTopicIDs.end())
|
||||
result = topicIDIterator->second;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string Storage::topicStandardForm(const std::string& phrase) const
|
||||
{
|
||||
std::map<std::string, std::string>::const_iterator phraseFormsIterator =
|
||||
mPhraseForms.find(phrase);
|
||||
|
||||
if (phraseFormsIterator != mPhraseForms.end())
|
||||
return phraseFormsIterator->second;
|
||||
else
|
||||
return phrase;
|
||||
}
|
||||
|
||||
void Storage::setEncoder(ToUTF8::Utf8Encoder* encoder)
|
||||
{
|
||||
mEncoder = encoder;
|
||||
}
|
||||
|
||||
bool Storage::hasTranslation() const
|
||||
{
|
||||
return !mCellNamesTranslations.empty() ||
|
||||
!mTopicIDs.empty() ||
|
||||
!mPhraseForms.empty();
|
||||
}
|
||||
}
|
42
components/translation/translation.hpp
Normal file
42
components/translation/translation.hpp
Normal file
|
@ -0,0 +1,42 @@
|
|||
#ifndef COMPONENTS_TRANSLATION_DATA_H
|
||||
#define COMPONENTS_TRANSLATION_DATA_H
|
||||
|
||||
#include <components/to_utf8/to_utf8.hpp>
|
||||
#include <components/files/collections.hpp>
|
||||
|
||||
namespace Translation
|
||||
{
|
||||
class Storage
|
||||
{
|
||||
public:
|
||||
|
||||
void loadTranslationData(const Files::Collections& dataFileCollections,
|
||||
const std::string& esmFileName);
|
||||
|
||||
std::string translateCellName(const std::string& cellName) const;
|
||||
std::string topicID(const std::string& phrase) const;
|
||||
|
||||
// Standard form usually means nominative case
|
||||
std::string topicStandardForm(const std::string& phrase) const;
|
||||
|
||||
void setEncoder(ToUTF8::Utf8Encoder* encoder);
|
||||
|
||||
bool hasTranslation() const;
|
||||
|
||||
private:
|
||||
typedef std::map<std::string, std::string> ContainerType;
|
||||
|
||||
void loadData(ContainerType& container,
|
||||
const std::string& fileNameNoExtension,
|
||||
const std::string& extension,
|
||||
const Files::Collections& dataFileCollections);
|
||||
|
||||
void loadDataFromStream(ContainerType& container, std::istream& stream);
|
||||
|
||||
|
||||
ToUTF8::Utf8Encoder* mEncoder;
|
||||
ContainerType mCellNamesTranslations, mTopicIDs, mPhraseForms;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue