Easy way to create the translations of your Plugin

Post Reply
SamBrishes
Master Bludit
Posts: 106
Joined: Tue Dec 25, 2018 8:07 pm
Been thanked: 3 times

Hellow,

I'm using a small script to create and update the translation files of my plugin. I thought it could help other developers too, so I adapted the script to Bludit's core translation system (since I'm using custom translation functions on my plugins).

Source Code
The script below fetch all 4 translation functions ("get", "g", "printMe", "p") used on "$language" or "$L", creates the respective translation keys and updates all available JSON files in your plugin folder. Already translated strings are preserved, of course.

Code: Select all

<?php

    /*
     |  FETCH LANGUAGE STRINGS RECURSIVE
     |
     |  @param  string  The absolute base directory of your plugin (incl. the plugin folder).
     |  @param  string  The additonal subdirectory, used for recursive calls only!
     |
     |  @return bool    TRUE if everything is fluffy, FALSE if not.
     */
    function fetchStrings(string $base, string $dir = ""): bool {
        static $translations = [];

        // Fett Full Path
        $fullpath = rtrim($base, DIRECTORY_SEPARATOR) . $dir;

        // Fetch Strings
        if(is_file($fullpath)) {
            if(strpos($fullpath, ".php") !== strlen($fullpath) - 4 || $fullpath === __FILE__) {
                return false;
            }

            $content = explode("\n", file_get_contents($fullpath));
            foreach($content AS $line) {
                preg_match_all('/(?:\$language|\$L)\-\>(?:get|g|printMe|p)\s*\(\s*([\'"])(.+?)\1\s*\)\s*/', $line, $matches, PREG_SET_ORDER);
                if(empty($matches)) {
                    continue;
                }

                foreach($matches AS $match) {
                    if(count($match) < 2) {
                        continue;
                    }

                    $key = mb_strtolower($match[2], "UTF-8");
                    $key = str_replace(" ", "-", $key);
                    $key = str_replace(".", "", $key);
                    if(strlen(trim($match[2])) > 0 && !isset($translations[$key])) {
                        $translations[$key] = trim($match[2]);
                    }
                }
            }
            return true;
        }

        // Recursive Walker
        if(is_dir($fullpath)) {
            if($handle = opendir($fullpath)) {
                while(($file = readdir($handle)) !== false) {
                    if(in_array($file, [".", "..", "languages"])) {
                        continue;
                    }
                    fetchStrings($base, $dir . DIRECTORY_SEPARATOR . $file);
                }
                closedir($handle);
            }
            if(!empty($dir)) {
                return true;
            }
        }

        // Write Translation Strings
        $langpath = rtrim($base, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . "languages" . DIRECTORY_SEPARATOR;
        if($handle = opendir($langpath)) {
            while(($file = readdir($handle)) !== false) {
                if(in_array($file, [".", ".."])) {
                    continue;
                }
                if(is_dir($langpath . $file) || strpos($file, ".json") !== strlen($file) - 5) {
                    continue;
                }

                $content = file_get_contents($langpath . $file);
                $content = json_decode($content, true);

                foreach($translations AS $key => $value) {
                    if(array_key_exists($key, $content)) {
                        continue;
                    }
                    $content[$key] = $value;
                }

                $content = json_encode($content, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
                file_put_contents($langpath . $file, $content);
            }
            closedir($handle);
        }
        return true;
    }
How to use it

Just pass the absolute path of your plugin (incl. the name of your plugin folder too, of course) as first parameter on the function. The rest is done by the script itself.

Keep in mind
  • The script doesn't support line breaks within the respective translation functions!
  • The function is hardcoded to UTF-8
  • The script doesn't create any language file itself, it just adds the translation key => string pairs to the existing ones!
~ Sam.
Post Reply