aboutsummaryrefslogtreecommitdiff
path: root/plugin.php
blob: 81c8b10145b028f9f7b4535459cab426cc8ae902 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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"));
	}
}

?>