commit 5103b2b86690b63b4eb618cc79503019582de63a Author: Miguel Nogueira Date: Thu Jan 14 22:49:48 2021 +0000 Create JSONify 0.1.0 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..974894e --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2021 Miguel N. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e74278c --- /dev/null +++ b/README.md @@ -0,0 +1,37 @@ +# Jsonify 0.1.0 + +## What is it? + +``Jsonify`` is a simple CLI command to turn word lists into JSON documents. It takes all lines in a text file, and turns them into JSON keys, +filling them with a placeholder string. After that, it writes the pretty-print'ed output to disk, so that you can begin translating it. + + +## Requirements + +None, other than PHP7 installed in the system. JSONify doesn't use any fancy libraries. Just pure raw PHP. + + +## Use cases + +Sometimes, you may have a list of strings that you need to translate, and the translation file needs to be in JSON. You can use this script +to easily turn that string list into a JSON document, complete with placeholders so you don't have to place them manually. + +For instance, the Laravel framework accepts translation files in JSON - it matches the keys in it with ktranslation calls in Blade templates. + + +## Usage + +``sudo jsonify [/path/to/filename-to-jsonify.txt]`` + + +## Installation + +To install this, simply copy the script to the directory where binaries usually live: + + - ``sudo cp jsonify.php /usr/local/bin/jsonify`` + - ``sudo chmod +x /usr/local/bin/jsonify`` + + +## Compatibiliy + +It hasn't been tested on Windows and probably won't work there. If you find any bugs, feel free to report them, or squash them yourself. diff --git a/jsonify.php b/jsonify.php new file mode 100644 index 0000000..c5875fb --- /dev/null +++ b/jsonify.php @@ -0,0 +1,60 @@ +> " . $message . PHP_EOL); + exit(1); + } + + echo $level . " >> " . $message . PHP_EOL; + +} + +$addPlaceholders = $argv[2] ?? false; + +if (!isset($argv[1])) +{ + logMessage("No file name provided. Please provide a filename to continue. It must be a full path to the file.", "ERROR"); +} + +$keys = []; + +if ($file = fopen($argv[1], "r")) +{ + // keep reading until end of file + while (!feof($file)) + { + $str = fgets($file, 8192); + // read unti end of line + if ($str !== false) + { + array_push($keys, trim($str)); + } + } + + // An use of this is a translation file, where all words for translation need to have a translated key + logMessage("Filling with placeholder strings!"); + $keys = array_fill_keys($keys, "translatemenow"); + + fclose($file); + + +} else { + logMessage("Unable to open file $file for reading.", "ERROR"); +} + +file_put_contents($_SERVER['HOME'] . "/jsonify.out.json", json_encode($keys, JSON_PRETTY_PRINT)); +logMessage("Successfully written JSONified output to disk. You can find it at " . $_SERVER['HOME'] . "/jsonify.out.json"); + +exit(0);