Something went wrong. Try again.
OpenMW is an open implementation of the Morrowind engine
Something went wrong. Try again.
3.3 kB · 107 lines
C++
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108#include "translation.hpp"
#include <fstream>
#include <components/misc/pathhelpers.hpp>
namespace Translation{ Storage::Storage() : mEncoder(nullptr) { }
void Storage::loadTranslationData(const Files::Collections& dataFileCollections, std::string_view esmFileName) { std::string_view esmNameNoExtension = Misc::stemFile(esmFileName);
loadData(mCellNamesTranslations, esmNameNoExtension, "cel", dataFileCollections); loadData(mPhraseForms, esmNameNoExtension, "top", dataFileCollections); loadData(mKeywords, esmNameNoExtension, "mrk", dataFileCollections); }
void Storage::loadData(ContainerType& container, std::string_view fileNameNoExtension, std::string_view extension, const Files::Collections& dataFileCollections) { std::string fileName(fileNameNoExtension); fileName += '.'; fileName += extension;
const Files::MultiDirCollection& collection = dataFileCollections.getCollection(extension); if (collection.doesExist(fileName)) { std::ifstream stream(collection.getPath(fileName));
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() && !stream.fail()) { std::getline(stream, line); if (!line.empty() && *line.rbegin() == '\r') line.resize(line.size() - 1);
if (!line.empty()) { const std::string_view utf8 = mEncoder->getUtf8(line);
size_t tabPos = utf8.find('\t'); if (tabPos != std::string::npos && tabPos > 0 && tabPos < utf8.size() - 1) { const std::string_view key = utf8.substr(0, tabPos); const std::string_view value = utf8.substr(tabPos + 1);
if (!key.empty() && !value.empty()) container.emplace(key, value); } } } }
std::string_view Storage::translateCellName(std::string_view cellName) const { auto entry = mCellNamesTranslations.find(cellName);
if (entry == mCellNamesTranslations.end()) return cellName;
return entry->second; }
std::string_view Storage::topicStandardForm(std::string_view phrase) const { auto phraseFormsIterator = mPhraseForms.find(phrase);
if (phraseFormsIterator != mPhraseForms.end()) return phraseFormsIterator->second; else return phrase; }
std::string_view Storage::topicKeyword(std::string_view phrase) const { auto entry = mKeywords.find(phrase);
if (entry == mKeywords.end()) return phrase;
return entry->second; }
void Storage::addPhraseForm(std::string_view phrase, std::string_view topicId) { mPhraseForms.emplace(phrase, topicId); }
void Storage::setEncoder(ToUTF8::Utf8Encoder* encoder) { mEncoder = encoder; }}