diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | CONTRIBUTORS | 6 | ||||
-rw-r--r-- | COPYING | 18 | ||||
-rw-r--r-- | README.md | 4 | ||||
-rwxr-xr-x | build.sh | 5 | ||||
-rw-r--r-- | help.html | 28 | ||||
-rw-r--r-- | plugin.php | 113 | ||||
-rw-r--r-- | tpls/backend.html | 43 |
8 files changed, 218 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..63ee5c4 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +custom_translation.rpk
\ No newline at end of file diff --git a/CONTRIBUTORS b/CONTRIBUTORS new file mode 100644 index 0000000..fb84f90 --- /dev/null +++ b/CONTRIBUTORS @@ -0,0 +1,6 @@ +People who have worked on custom_translation +============================================ + +If you modified something, feel free to append your name to this list. + +* Kevin Chabowski <kevin@kch42.de> @@ -0,0 +1,18 @@ +custom_translation: Copyright (c) 2012 The Ratatöskr Team + +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..06241fc --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +custom_translation +================== + +A Plugin for Ratatöskr that makes it possible to internationalize your templates.
\ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..b15ca53 --- /dev/null +++ b/build.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +rm -f tpls/*~ + +r7r-plugin-packer --output=custom_translation.rpk --codefile=plugin.php --classname=custom_translation --pluginname=custom_translation --author='The Ratatöskr Team' --versiontext="0.5" --versioncount=3 --api=5 --shortdesc="Your own translations, so you can easilly internationalize your templates!" --helpfile=help.html --licensefile=COPYING --tpldir=tpls diff --git a/help.html b/help.html new file mode 100644 index 0000000..a482772 --- /dev/null +++ b/help.html @@ -0,0 +1,28 @@ +<h2>custom_translation</h2> + +<p>Your own translations, so you can easilly internationalize your templates!</p> + +<p>custom_translation gives you an STE tag called <code><ste:custom_translation /></code> to include translations to your plugin and a plugin page in the backend where you can set your translations.</p> + +<h3>Usage of the STE Tag</h3> + +<code><ste:custom_translation /></code> is a self-closing tag and uses these parameters: + +<table class="listtab"> + <thead> + <tr> + <th>Parameter</th> + <th>Description</th> + </tr> + </thead> + <tbody> + <tr> + <td><code>for</code></td> + <td>The translation key to lookup.</td> + </tr> + <tr> + <td><code>raw</code></td> + <td>If not empty, it will return the raw translation, otherwise it will be already HTML escaped.</td> + </tr> + </tbody> +</table> diff --git a/plugin.php b/plugin.php new file mode 100644 index 0000000..81c8b10 --- /dev/null +++ b/plugin.php @@ -0,0 +1,113 @@ +<?php + +class custom_translation extends RatatoeskrPlugin +{ + public $current_language; + public $current_translation; + + public function ste_tag_cunstom_translation($ste, $params, $sub) + { + $this->load_language($ste->vars["language"]); + if($ste->evalbool($params["raw"])) + return (string) $this->current_translation[$params["for"]]; + else + return htmlesc((string) $this->current_translation[$params["for"]]); + } + + public function pluginpage(&$data, $url_now, &$url_next) + { + global $languages; + $this->prepare_backend_pluginpage(); + + $tr_langs = $this->kvstorage["languages"]; + $translation_keys = $this->kvstorage["translation_keys"]; + + $translations = array(); + foreach($tr_langs as $l) + $translations[$l] = $this->kvstorage["translation_$l"]; + + if(isset($_POST["tr_translation_enter"]) and (!empty($_POST["tr_translation_key"])) and (!empty($_POST["tr_lang"]))) + { + if(!isset($translations[$_POST["tr_lang"]])) + { + $tr_langs[] = $_POST["tr_lang"]; + $translation = array(); + } + else + $translation = $translations[$_POST["tr_lang"]]; + + if(empty($_POST["tr_translation"])) + { + if(in_array($_POST["tr_translation_key"], $translation_keys)) + { + unset($translation[$_POST["tr_translation_key"]]); + if(empty($translation)) + { + $tr_langs = array_filter($tr_langs, function($l) { return $l != $_POST["tr_lang"]; }); + $this->kvstorage["languages"] = $tr_langs; + } + } + } + else + { + $translation[$_POST["tr_translation_key"]] = $_POST["tr_translation"]; + if(!in_array($_POST["tr_translation_key"], $translation_keys)) + $translation_keys[] = $_POST["tr_translation_key"]; + } + + $translations[$_POST["tr_lang"]] = $translation; + $this->kvstorage["translation_" . $_POST["tr_lang"]] = $translation; + $this->kvstorage["translation_keys"] = $translation_keys; + } + + sort($tr_langs); + + $this->kvstorage["languages"] = $tr_langs; + + $this->ste->vars["tr_langs"] = array_map(function($l) use ($languages) { return $languages[$l]["language"]; }, $tr_langs); + $this->ste->vars["tr_translations"] = array(); + foreach($translation_keys as $tk) + { + $this->ste->vars["tr_translations"][$tk] = array(); + $nothinghere = True; + foreach($tr_langs as $lang) + { + $translation = (string) @$translations[$lang][$tk]; + $this->ste->vars["tr_translations"][$tk][] = $translation; + if(!empty($translation)) + $nothinghere = False; + } + if($nothinghere) + { + unset($this->ste->vars["tr_translations"][$tk]); + $translation_keys = array_filter($translation_keys, function($k) use ($tk) { return $tk != $k; }); + $this->kvstorage["translation_keys"] = $translation_keys; + } + } + + echo $this->ste->exectemplate($this->get_template_dir() . "/backend.html"); + } + + private function load_language($langcode) + { + if($langcode == $this->current_language) + return; + + $this->current_translation = (array) $this->kvstorage["translation_$langcode"]; + $this->current_language = $langcode; + } + + public function install() + { + $this->kvstorage["languages"] = array(); + $this->kvstorage["translation_keys"] = array(); + } + + public function init() + { + $this->ste->register_tag("custom_translation", array($this, "ste_tag_cunstom_translation")); + $this->register_backend_pluginpage("Custom Translations", array($this, "pluginpage")); + } +} + +?> diff --git a/tpls/backend.html b/tpls/backend.html new file mode 100644 index 0000000..948d3f8 --- /dev/null +++ b/tpls/backend.html @@ -0,0 +1,43 @@ +<ste:load name="/systemtemplates/master.html" /> +<ste:block name="content"> + <ste:default_error /> + <ste:default_success /> + + <h2>New Translation / Edit Translations</h2> + <form action="$rel_path_to_pluginpage" method="POST" accepted-charset="UTF-8"> + <p> + Language: + <select name="tr_lang"> + <ste:foreach array="all_languages" key="langcode" value="lang"> + <option value="$langcode">$langcode: <ste:escape>$lang</ste:escape></option> + </ste:foreach> + </select> + </p> + <p>Translation key: <input type="text" name="tr_translation_key" /></p> + <p>Translation: <input type="text" name="tr_translation" /></p> + <p><input type="submit" name="tr_translation_enter" /></p> + <p>If you want to delete a translation, just set to an empty translation</p> + </form> + + <h2>Translations</h2> + <table class="listtab"> + <thead> + <tr> + <th> </th> + <ste:foreach array="tr_langs" value="tr_lang"> + <th><ste:escape>$tr_lang</ste:escape></th> + </ste:foreach> + </tr> + </thead> + <tbody> + <ste:foreach array="tr_translations" key="tr_transl_name" value="tr_transl_data"> + <tr> + <td><strong><ste:escape>$tr_transl_name</ste:escape></strong></td> + <ste:foreach array="tr_transl_data" value="tr_transl_t"> + <td><ste:escape>$tr_transl_t</ste:escape></td> + </ste:foreach> + </tr> + </ste:foreach> + </tbody> + </table> +</ste:block> |