1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\ManageBlock;
13
14 use ICanBoogie\I18n;
15 use ICanBoogie\Module;
16
17 18 19
20 class Translator
21 {
22 protected $module;
23
24 public function __construct(Module $module)
25 {
26 $this->module = $module;
27 }
28
29 public function __invoke($native, array $args=array(), array $options=array())
30 {
31 $module = $this->module;
32
33 $user_scope = isset($options['scope']) ? $options['scope'] : null;
34 $user_scope_dotted = $user_scope ? "{$user_scope}." : '';
35 $user_default = isset($options['default']) ? $options['default'] : null;
36
37 $options['scope'] = "{$this->module->flat_id}.manage" . ($user_scope ? ".$user_scope" : '');
38
39 $options['default'] = function(\ICanBoogie\I18n\Translator $translator, $native) use($module, $user_scope_dotted, $user_default) {
40
41 while ($module = $module->parent)
42 {
43 $try = "$module->flat_id.manage.{$user_scope_dotted}$native";
44 $translated = $translator[$try];
45
46 if ($translated)
47 {
48 return $translated;
49 }
50 }
51
52 return $translator["manage.{$user_scope_dotted}$native"]
53 ?: $translator["{$user_scope_dotted}$native"]
54 ?: ($user_default instanceof \Closure ? $user_default($translator, $native) : $translator[$user_default])
55 ?: $user_default;
56 };
57
58 return I18n\t($native, $args, $options);
59 }
60 }