Skip to content
Permalink
Browse files

Fix language loading to properly default to en-US when translations a…

…re missing. Prioritize untranslated text over incorrect text.

Modders, you can stop copy-pasting en-US to en-GB. Please.
  • Loading branch information
SupSuper committed Feb 10, 2019
1 parent c61f43a commit 379410edaf56555237f5faa30563709d8e8bdf5a
Showing with 50 additions and 48 deletions.
  1. +18 −39 src/Engine/Game.cpp
  2. +0 −2 src/Engine/Game.h
  3. +27 −4 src/Engine/Language.cpp
  4. +5 −3 src/Engine/Language.h
@@ -450,42 +450,6 @@ Language *Game::getLanguage() const
return _lang;
}

/**
* Changes the language currently in use by the game.
* @param filename Filename of the language file.
*/
void Game::loadLanguage(const std::string &filename)
{
std::ostringstream ss;
ss << "/Language/" << filename << ".yml";
std::string path = CrossPlatform::searchDataFile("common" + ss.str());
try
{
_lang->load(path);
}
catch (YAML::Exception &e)
{
throw Exception(path + ": " + std::string(e.what()));
}

std::vector<const ModInfo*> activeMods = Options::getActiveMods();
for (std::vector<const ModInfo*>::const_iterator i = activeMods.begin(); i != activeMods.end(); ++i)
{
std::string file = (*i)->getPath() + ss.str();
if (CrossPlatform::fileExists(file))
{
_lang->load(file);
}
}

const std::map<std::string, ExtraStrings*> &extraStrings = _mod->getExtraStrings();
std::map<std::string, ExtraStrings*>::const_iterator it = extraStrings.find(filename);
if (it != extraStrings.end())
{
_lang->load(it->second);
}
}

/**
* Returns the saved game currently in use by the game.
* @return Pointer to the saved game.
@@ -613,13 +577,28 @@ void Game::loadLanguages()
currentLang = defaultLang;
}
}
Options::language = currentLang;

loadLanguage(defaultLang);
// Load default and current language
std::ostringstream ssDefault, ssCurrent;
ssDefault << "/Language/" << defaultLang << ".yml";
ssCurrent << "/Language/" << currentLang << ".yml";

_lang->loadFile(CrossPlatform::searchDataFile("common" + ssDefault.str()));
if (currentLang != defaultLang)
_lang->loadFile(CrossPlatform::searchDataFile("common" + ssCurrent.str()));

std::vector<const ModInfo*> activeMods = Options::getActiveMods();
for (std::vector<const ModInfo*>::const_iterator i = activeMods.begin(); i != activeMods.end(); ++i)
{
loadLanguage(currentLang);
_lang->loadFile((*i)->getPath() + ssDefault.str());
if (currentLang != defaultLang)
_lang->loadFile((*i)->getPath() + ssCurrent.str());
}
Options::language = currentLang;

_lang->loadRule(_mod->getExtraStrings(), defaultLang);
if (currentLang != defaultLang)
_lang->loadRule(_mod->getExtraStrings(), currentLang);
}

/**
@@ -53,8 +53,6 @@ class Game
bool _mouseActive;
static const double VOLUME_GRADIENT;

/// Loads a new language for the game.
void loadLanguage(const std::string &filename);
public:
/// Creates a new game and initializes SDL.
Game(const std::string &title);
@@ -27,6 +27,7 @@
#include "Options.h"
#include "LanguagePlurality.h"
#include "Unicode.h"
#include "Exception.h"
#include "../Mod/ExtraStrings.h"

namespace OpenXcom
@@ -196,14 +197,36 @@ void Language::load(const std::string &filename)
}
}

/**
* Loads a language file from an external path.
* @param path Language file path.
*/
void Language::loadFile(const std::string &path)
{
try
{
if (CrossPlatform::fileExists(path))
{
load(path);
}
}
catch (YAML::Exception &e)
{
throw Exception(path + ": " + std::string(e.what()));
}
}

/**
* Loads a language file from a mod's ExtraStrings.
* @param extras Pointer to extra strings from ruleset.
* @param extraStrings List of ExtraStrings.
* @param id Language ID.
*/
void Language::load(ExtraStrings *extras)
void Language::loadRule(const std::map<std::string, ExtraStrings*> &extraStrings, const std::string &id)
{
if (extras)
std::map<std::string, ExtraStrings*>::const_iterator it = extraStrings.find(id);
if (it != extraStrings.end())
{
ExtraStrings *extras = it->second;
for (std::map<std::string, std::string>::const_iterator i = extras->getStrings()->begin(); i != extras->getStrings()->end(); ++i)
{
_strings[i->first] = loadString(i->second);
@@ -366,7 +389,7 @@ void Language::toHtml(const std::string &filename) const
std::string s = i->second;
for (std::string::const_iterator j = s.begin(); j != s.end(); ++j)
{
if (*j == 2 || *j == '\n')
if (*j == Unicode::TOK_NL_SMALL || *j == '\n')
{
htmlFile << "<br />";
}
@@ -50,17 +50,19 @@ class Language

/// Parses a text string loaded from an external file.
std::string loadString(const std::string &s) const;
/// Loads the language from a YAML file.
void load(const std::string &filename);
public:
/// Creates a blank language.
Language();
/// Cleans up the language.
~Language();
/// Gets list of languages in the data directory.
static void getList(std::vector<std::string> &files, std::vector<std::string> &names);
/// Loads the language from a YAML file.
void load(const std::string &filename);
/// Loads the language from an external file.
void loadFile(const std::string &filename);
/// Loads the language from a ruleset file.
void load(ExtraStrings *strings);
void loadRule(const std::map<std::string, ExtraStrings*> &extraStrings, const std::string &id);
/// Gets the language's ID.
std::string getId() const;
/// Gets the language's name.

0 comments on commit 379410e

Please sign in to comment.