Create JSONify 0.1.0

This commit is contained in:
Miguel Nogueira 2021-01-14 22:49:48 +00:00
commit 5103b2b866
3 changed files with 104 additions and 0 deletions

7
LICENSE Normal file
View File

@ -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.

37
README.md Normal file
View File

@ -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.

60
jsonify.php Normal file
View File

@ -0,0 +1,60 @@
<?php
/**
*
* JSONify v0.1.0 - Convert word lists into JSON documents
*
*/
echo "Jsonify 0.1.0 - miguel456@spacejewel-hosting.com " . PHP_EOL;
// debug
function logMessage($message, $level = "INFO", $stderr = true)
{
if ($level == "ERROR" && $stderr)
{
fwrite(STDERR, $level . " >> " . $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);