Skip to content

Commit

Permalink
Add gettext / multi-language support
Browse files Browse the repository at this point in the history
  • Loading branch information
paradust7 committed Jan 5, 2024
1 parent fce4a97 commit 3355f86
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 15 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ RUN \
cmake \
tclsh \
zip \
zstd
zstd \
gettext

COPY . /minetest-wasm

Expand Down
2 changes: 1 addition & 1 deletion build_minetest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if ! $INCREMENTAL; then
emcmake cmake \
-DCMAKE_VERBOSE_MAKEFILE=ON \
-DENABLE_SYSTEM_GMP=OFF \
-DENABLE_GETTEXT=FALSE \
-DENABLE_GETTEXT=TRUE \
-DRUN_IN_PLACE=TRUE \
-DENABLE_GLES=TRUE \
-DCMAKE_BUILD_TYPE="$MINETEST_BUILD_TYPE" \
Expand Down
2 changes: 1 addition & 1 deletion fetch_sources.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ getrepo minetest_game "https://github.com/minetest/minetest_game.git" a3b171e317

# These repos are part of the fork
getrepo webshims "https://github.com/paradust7/webshims.git" 91c3fe85d2cb7f85cc8e19d3f53dc8f252a69ff7
getrepo minetest "https://github.com/paradust7/minetest.git" bfe2f41d7e830df08814f6ec6c0ec686e3b0d20b
getrepo minetest "https://github.com/paradust7/minetest.git" 943e0e9f99245aaf61a3e3967d53f807c70492e6
getrepo irrlichtmt "https://github.com/paradust7/irrlicht.git" bcada2f995d7b927f6c67c755fb32a2a4da5b236

# Make irrlichtmt symlink
Expand Down
34 changes: 27 additions & 7 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
<!-- filled by javascript -->
</select>

<br/>

<b>Select language:</b>
<select id="select_language">
<!-- filled by javascript -->
</select>

<hr>
<button id="launch" style="width: 300px; height: 50px; font-size: 20pt;">Launch Minetest</button>
<br/>
Expand Down Expand Up @@ -45,12 +52,13 @@
</div>
<script type="text/javascript" src="%__RELEASE_UUID__%/launcher.js"></script>
<script type="text/javascript">

// Reduce memory and network usage
const memorySavingConfig = `
viewing_range = 85
max_block_send_distance = 5
max_block_generate_distance = 5
`;
function enableMemorySaving(mtl) {
mtl.setConf('viewing_range', 85);
mtl.setConf('max_block_send_distance', 5);
mtl.setConf('max_block_generate_distance', 5);
}

const launchForm = document.getElementById('launch_form');
const joinForm = document.getElementById('join_form');
Expand Down Expand Up @@ -97,12 +105,22 @@
select_proxy.appendChild(opt);
});

const select_language = document.getElementById('select_language');
SUPPORTED_LANGUAGES.forEach(entry => {
const opt = document.createElement('option');
opt.value = entry[0];
opt.innerText = entry[1];
select_language.appendChild(opt);
});
select_language.value = getDefaultLanguage();

const launch = document.getElementById('launch');
launch.addEventListener('click', () => {
const proxyIndex = document.getElementById('select_proxy').value;
const proxy = proxies[proxyIndex][0];
wipePage();
mtl.setProxy(proxy);
mtl.setLang(select_language.value);
mtl.launch(args);
});

Expand Down Expand Up @@ -134,7 +152,8 @@
args.go = true;
history.pushState({}, "", `?proxy=${proxyIndex}&game=${game}&join=${clientCode}`);
mtl.setVPN(serverCode, clientCode);
mtl.setMinetestConf(memorySavingConfig);
enableMemorySaving(mtl);
mtl.setLang(select_language.value);
mtl.launch(args);
});

Expand All @@ -156,7 +175,8 @@
args.port = 30000;
args.go = true;
mtl.setVPN(null, join_code);
mtl.setMinetestConf(memorySavingConfig);
enableMemorySaving(mtl);
mtl.setLang(select_language.value);
mtl.launch(args);
});
}
Expand Down
140 changes: 135 additions & 5 deletions static/launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ class MinetestLauncher {
this.proxyUrl = "wss://minetest.dustlabs.io/proxy";
this.packsDir = DEFAULT_PACKS_DIR;
this.packsDirIsCors = false;
this.minetestConf = null;
this.minetestConf = new Map();

mtScheduler.addCondition("wasmReady", loadWasm);
mtScheduler.addCondition("launch_called");
Expand Down Expand Up @@ -614,8 +614,27 @@ class MinetestLauncher {
this.vpn = serverCode ? serverCode : clientCode;
}

setMinetestConf(contents) {
this.minetestConf = contents;
// Set a key/value pair in minetest.conf
// Overrides previous values of the same key
setConf(key, value) {
key = key.toString();
value = value.toString();
this.minetestConf.set(key, value);
}

#renderMinetestConf() {
let lines = [];
for (const [k, v] of this.minetestConf.entries()) {
lines.push(`${k} = ${v}\n`);
}
return lines.join('');
}

setLang(lang) {
if (!SUPPORTED_LANGUAGES_MAP.has(lang)) {
alert(`Invalid code in setLang: ${lang}`);
}
this.setConf("language", lang);
}

// Returns pack status:
Expand Down Expand Up @@ -732,8 +751,10 @@ class MinetestLauncher {
this.addPacks(this.args.packs);
activateBody();
fixGeometry();
if (this.minetestConf) {
const confBuf = stringToNewUTF8(this.minetestConf)
if (this.minetestConf.size > 0) {
const contents = this.#renderMinetestConf();
console.log("minetest.conf is: ", contents);
const confBuf = stringToNewUTF8(contents);
emloop_set_minetest_conf(confBuf);
_free(confBuf);
}
Expand All @@ -755,3 +776,112 @@ class MinetestLauncher {
mtScheduler.setCondition("launch_called");
}
}

// Pulled from builtin/mainmenu/settings/dlg_settings.lua
const SUPPORTED_LANGUAGES = [
['be', "Беларуская [be]"],
['bg', "Български [bg]"],
['ca', "Català [ca]"],
['cs', "Česky [cs]"],
['cy', "Cymraeg [cy]"],
['da', "Dansk [da]"],
['de', "Deutsch [de]"],
['el', "Ελληνικά [el]"],
['en', "English [en]"],
['eo', "Esperanto [eo]"],
['es', "Español [es]"],
['et', "Eesti [et]"],
['eu', "Euskara [eu]"],
['fi', "Suomi [fi]"],
['fil', "Wikang Filipino [fil]"],
['fr', "Français [fr]"],
['gd', "Gàidhlig [gd]"],
['gl', "Galego [gl]"],
['hu', "Magyar [hu]"],
['id', "Bahasa Indonesia [id]"],
['it', "Italiano [it]"],
['ja', "日本語 [ja]"],
['jbo', "Lojban [jbo]"],
['kk', "Қазақша [kk]"],
['ko', "한국어 [ko]"],
['ky', "Kırgızca / Кыргызча [ky]"],
['lt', "Lietuvių [lt]"],
['lv', "Latviešu [lv]"],
['mn', "Монгол [mn]"],
['mr', "मराठी [mr]"],
['ms', "Bahasa Melayu [ms]"],
['nb', "Norsk Bokmål [nb]"],
['nl', "Nederlands [nl]"],
['nn', "Norsk Nynorsk [nn]"],
['oc', "Occitan [oc]"],
['pl', "Polski [pl]"],
['pt', "Português [pt]"],
['pt_BR', "Português do Brasil [pt_BR]"],
['ro', "Română [ro]"],
['ru', "Русский [ru]"],
['sk', "Slovenčina [sk]"],
['sl', "Slovenščina [sl]"],
['sr_Cyrl', "Српски [sr_Cyrl]"],
['sr_Latn', "Srpski (Latinica) [sr_Latn]"],
['sv', "Svenska [sv]"],
['sw', "Kiswahili [sw]"],
['tr', "Türkçe [tr]"],
['tt', "Tatarça [tt]"],
['uk', "Українська [uk]"],
['vi', "Tiếng Việt [vi]"],
['zh_CN', "中文 (简体) [zh_CN]"],
['zh_TW', "正體中文 (繁體) [zh_TW]"],
];

const SUPPORTED_LANGUAGES_MAP = new Map(SUPPORTED_LANGUAGES);

function getDefaultLanguage() {
const fuzzy = [];

const url_params = new URLSearchParams(window.location.search);
if (url_params.has("lang")) {
const lang = url_params.get("lang");
if (SUPPORTED_LANGUAGES_MAP.has(lang)) {
return lang;
}
alert(`Invalid lang parameter: ${lang}`);
return 'en';
}

for (let candidate of navigator.languages) {
candidate = candidate.replaceAll('-', '_');

if (SUPPORTED_LANGUAGES_MAP.has(candidate)) {
return candidate;
}

// Try stripping off the country code
const parts = candidate.split('_');
if (parts.length > 2) {
const rcandidate = parts.slice(0, 2).join('_');
if (SUPPORTED_LANGUAGES_MAP.has(rcandidate)) {
return rcandidate;
}
}

// Try just matching the language code
if (parts.length > 1) {
if (SUPPORTED_LANGUAGES_MAP.has(parts[0])) {
return parts[0];
}
}

// Try fuzzy match (ignore country code of both)
for (let entry of SUPPORTED_LANGUAGES) {
if (entry[0].split('_')[0] == parts[0]) {
fuzzy.push(entry[0]);
}
}
}

if (fuzzy.length > 0) {
return fuzzy[0];
}

return 'en';
}

0 comments on commit 3355f86

Please sign in to comment.