Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translated the project to english #8

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

detain
Copy link

@detain detain commented Oct 20, 2022

Pretty sure this will be denied but if anyone asks in the future for an english version can just refer back to this.

forked version of admin plugin for webman changing all the text from chinese to english.

The translation was done with using 2 scripts I made to find and translate the text (code below)

  1. ran php find_chinese_strings.php -v admin/ --exclude=/assets/
  2. copied the contents of the strings_only.json file and pasted it into https://translate.google.com/
  3. copied the translated text into a file translated.json
  4. ran php replace_chinese_strings.php (code below)

find_chinese_strings.php

<?php
$listOnly = false;
$verbose = false;
$includes = [];
$excludes = [];
$strings = [];
function dieHelp() {
    die("syntax find_chinese_strings.php [-h] [-v] [-l] [--exclude=dir] <dir1> [dir2]\n\n-h    help screen (this page)\n-v    verbose mode\n-l    list-only (does not process files, only findes and displays them)\n--exclude=<dir>    skips this in the results\n\n");
}
if ($_SERVER['argc'] == 1) {
    dieHelp();
}
for ($idx = 1; $idx < $_SERVER['argc']; $idx++) {
    $arg = $_SERVER['argv'][$idx];
    if ($arg == '-h') {
        dieHelp();
    } elseif ($arg == '-v') {
        $verbose = true;
    } elseif ($arg == '-l') {
        $listOnly = true;
    } elseif (preg_match('/^--exclude=(.*)$/', $arg, $matches)) {
        echo "Excluding Dir {$matches[1]}\n";
        $excludes[] = $matches[1];
    } else {
        $includes[] = $arg;
    }
}
if (count($includes) == 0) {
    die("No paths passed to scour\n");
}
$cmd = 'find';
foreach ($includes as $includeDir) {
    $cmd .= ' '.escapeshellarg($includeDir);
}
$cmd .= ' -type f|xargs -n 1 file';
if (count($excludes) > 0) {
    $cmd .= '|grep -a -v';
    foreach ($excludes as $excludeDir) {
        $cmd .= ' -e '.escapeshellarg($excludeDir);
    }
}
$cmd .= '|grep -a -e Unicode -e UTF|cut -d: -f1';
$out = trim(`{$cmd}`);
if ($verbose)
    echo "CMD: {$cmd}\n";
if ($out != '') {
    $files = explode("\n", $out);
    echo 'Found '.count($files).' with Unicode characters in them'.PHP_EOL;
    foreach ($files as $fileName) {
        $fileStr = file_get_contents($fileName);
        $pathInfo = pathinfo($fileName);
        if (isset($pathInfo['extension']) && $pathInfo['extension'] == 'sql') {
            $fileStr = str_replace(',', ",\n", $fileStr);
            if (preg_match_all('/[\x{4e00}-\x{9fa5}]+[^\'"]*[\x{4e00}-\x{9fa5}]/u', $fileStr, $matches)) {
                echo 'Found '.count($matches[0]).' chinese strings in '.$fileName.PHP_EOL;
                foreach ($matches[0] as $utf8Str) {
                    if (!array_key_exists($utf8Str, $strings)) {
                        $strings[$utf8Str] = [];
                    }
                    if (!in_array($fileName, $strings[$utf8Str])) {
                        $strings[$utf8Str][] = $fileName;
                    }
                }
            }
        } elseif (preg_match_all('/[\x{4e00}-\x{9fa5}]+.*[\x{4e00}-\x{9fa5}]/u', $fileStr, $matches)) {
            echo 'Found '.count($matches[0]).' chinese strings in '.$fileName.PHP_EOL;
            foreach ($matches[0] as $utf8Str) {
                if (!array_key_exists($utf8Str, $strings)) {
                    $strings[$utf8Str] = [];
                }
                if (!in_array($fileName, $strings[$utf8Str])) {
                    $strings[$utf8Str][] = $fileName;
                }
            }

        }
    }
}
uksort($strings, function($a, $b) {
    return mb_strlen($b) <=> mb_strlen($a);
});
file_put_contents('strings.json', json_encode($strings, JSON_PRETTY_PRINT |  JSON_UNESCAPED_UNICODE |  JSON_UNESCAPED_SLASHES));
file_put_contents('string_only.json', json_encode(array_keys($strings), JSON_PRETTY_PRINT |  JSON_UNESCAPED_UNICODE |  JSON_UNESCAPED_SLASHES));

replace_chinese_strings.php

<?php
$changed = 0;
$strings = json_decode(file_get_contents('strings.json'), true);
$translated = json_decode(file_get_contents('translated.json'), true);
$untranslated = array_keys($strings);
foreach ($untranslated as $idx => $oldStr) {
    $newStr = $translated[$idx];
    echo "Replacing '{$oldStr}' with '{$newStr}'\n";
    foreach ($strings[$oldStr] as $fileName) {
        echo "  in {$fileName}\n";
        $cmd = 'sed s#'.escapeshellarg($oldStr).'#'.escapeshellarg($newStr).'#g -i '.$fileName;
        echo "CMD: {$cmd}\n";
        echo `{$cmd}`;
    }
}
echo "Finished, {$changed} strings translated\n";

@walkor
Copy link
Contributor

walkor commented Oct 20, 2022

Thank you very much for your PR. Although this PR will not be merged, it will always be retained to make it easier for more people to find it. 🤝

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants