AGS: Skeleton for converting UTF-8 translation keys to ASCII

From upstream e04f86a91b248f598fccf04369e0874393a9a45d

Upstream uses locale, but since that isn't available to a
ScummVM engine, for now it's just a skeleton implementation
This commit is contained in:
Paul Gilbert
2021-07-10 14:33:36 -07:00
parent 4cb95e324e
commit 5dec9be2e9
2 changed files with 26 additions and 0 deletions
+24
View File
@@ -20,6 +20,7 @@
*
*/
#include "common/language.h"
#include "ags/engine/ac/asset_helper.h"
#include "ags/shared/ac/common.h"
#include "ags/engine/ac/game_setup.h"
@@ -40,6 +41,13 @@ namespace AGS3 {
using namespace AGS::Shared;
// TODO: Since ScummVM can't use uconvert, this is a dummy implementation for now
const char *convert_utf8_to_ascii(const char *mbstr, const char *loc_name) {
_G(mbbuf).resize(strlen(mbstr) + 1);
strcpy(&_G(mbbuf)[0], mbstr);
return &_G(mbbuf)[0];
}
void close_translation() {
_GP(transtree).clear();
_GP(trans) = Translation();
@@ -111,6 +119,22 @@ bool init_translation(const String &lang, const String &fallback_lang, bool quit
else
set_uformat(U_ASCII);
// Mixed encoding support:
// original text unfortunately may contain extended ASCII chars (> 127);
// if translation is UTF-8 but game is extended ASCII, then the translation
// dictionary keys won't match.
// With that assumption we must convert dictionary keys into ASCII using
// provided locale hint.
String key_enc = _GP(trans).StrOptions["gameencoding"];
if (!key_enc.IsEmpty()) {
StringMap conv_map;
for (const auto &item : _GP(trans).Dict) {
String key = convert_utf8_to_ascii(item._key.GetCStr(), key_enc.GetCStr());
conv_map.insert(std::make_pair(key, item._value));
}
_GP(trans).Dict = conv_map;
}
Debug::Printf("Translation initialized: %s", _G(trans_filename).GetCStr());
return true;
}
+2
View File
@@ -1319,6 +1319,8 @@ public:
String _trans_name, _trans_filename;
long _lang_offs_start = 0;
char _transFileName[MAX_PATH] = { 0 };
std::vector<uint16> _wcsbuf; // widechar buffer
std::vector<char> _mbbuf; // utf8 buffer
/**@}*/