Skateside / pocket-grimoire

A mobile version of the Blood on the Clocktower grimoire
https://www.pocketgrimoire.co.uk
GNU General Public License v3.0
34 stars 14 forks source link

Localise the Pocket Grimoire #29

Closed Skateside closed 2 years ago

Skateside commented 2 years ago

There are 2 steps to this:

Here's some basic code to convert RealVidy's Google doc into JSON which can be converted into character JSON (falling back to English)

function createLangJSON(lang) {

    const rows = lang.trim().split("\n").map((row) => row.split("\t"));
    const keys = [
        "id",
        "name",
        "ability",
        "firstNightReminder",
        "otherNightReminder",
        "remindersGlobal",
        "reminders"
    ];
    const data = rows.map((row) => row.reduce((data, cell, i) => {
        return Object.assign(data, { [keys[i]]: cell });
    }, {}));
    const complete = data.map((row) => {
        row.reminders = (
            row.reminders
            ? row.reminders.split(/\s*,\s*/)
            : []
        );
        row.remindersGlobal = (
            row.remindersGlobal
            ? row.remindersGlobal.split(/\s*,\s*/)
            : []
        );
        return row;
    });

    // copy(JSON.stringify(complete, null, "    "));
    return complete;

}

copy(JSON.stringify(createLangJSON(``), null, "    "));
<?php

function array_find_index(array $array, callable $callable) {

    foreach ($array as $index => $item) {

        if (call_user_func($callable, $item, $index) === true) {
            return $index;
        }

    }

    return false;

}

function array_find(array $array, callable $callable) {

    $index = array_find_index($array, $callable);

    return $index === false ? null : $array[$index];

}

$charactersRaw = file_get_contents('./characters.json');
$characters = json_decode($charactersRaw, true);

$filename = "./{$_GET['lang']}.json";

if (file_exists($filename)) {

    $langRaw = file_get_contents($filename);
    $lang = json_decode($langRaw, true);

    foreach ($characters as &$character) {

        $id = $character['id'];
        $localised = array_find($lang, function ($item, $index) use ($id) {
            return $item['id'] === $id;
        });

        if (is_null($localised)) {
            continue;
        }

        $character = array_merge($character, $localised);

    }

}

header('Content-Type: application/json');
echo json_encode($characters);
exit();
Skateside commented 2 years ago

This was fixed in commit 31ca4b8ee4f25049578e9d6afae1d797ce83b575