From c101c845587a02afeb591620c95d10fb7a428796 Mon Sep 17 00:00:00 2001 From: Kevin Chabowski Date: Sat, 3 Mar 2012 20:19:53 +0100 Subject: Initial commit. --- .gitignore | 1 + CONTRIBUTORS | 6 +++ COPYING | 18 +++++++++ README.md | 4 ++ build.sh | 5 +++ help.html | 28 ++++++++++++++ plugin.php | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tpls/backend.html | 43 +++++++++++++++++++++ 8 files changed, 218 insertions(+) create mode 100644 .gitignore create mode 100644 CONTRIBUTORS create mode 100644 COPYING create mode 100644 README.md create mode 100755 build.sh create mode 100644 help.html create mode 100644 plugin.php create mode 100644 tpls/backend.html 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 diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..45a90d2 --- /dev/null +++ b/COPYING @@ -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 @@ +

custom_translation

+ +

Your own translations, so you can easilly internationalize your templates!

+ +

custom_translation gives you an STE tag called <ste:custom_translation /> to include translations to your plugin and a plugin page in the backend where you can set your translations.

+ +

Usage of the STE Tag

+ +<ste:custom_translation /> is a self-closing tag and uses these parameters: + + + + + + + + + + + + + + + + + + +
ParameterDescription
forThe translation key to lookup.
rawIf not empty, it will return the raw translation, otherwise it will be already HTML escaped.
diff --git a/plugin.php b/plugin.php new file mode 100644 index 0000000..81c8b10 --- /dev/null +++ b/plugin.php @@ -0,0 +1,113 @@ +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 @@ + + + + + +

New Translation / Edit Translations

+
+

+ Language: + +

+

Translation key:

+

Translation:

+

+

If you want to delete a translation, just set to an empty translation

+
+ +

Translations

+ + + + + + + + + + + + + + + + + + + +
 $tr_lang
$tr_transl_name$tr_transl_t
+
-- cgit v1.2.3-70-g09d2