Passed
Push — master ( bb74df...01777c )
by Jakub
02:24
created

src/Translator.php (6 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
declare(strict_types=1);
3
4
namespace Nexendrie\Translation;
5
6
use Nette\Utils\Arrays,
7
    Nette\Utils\Strings,
8
    Nette\Localization\ITranslator,
9
    Nexendrie\Translation\Loaders\ILoader,
10
    Nexendrie\Utils\Intervals;
11
12
/**
13
 * Translator
14
 *
15
 * @author Jakub Konečný
16
 * @property string $lang
17
 * @property-read string[] $untranslated
18
 * @method void onUntranslated(string $message)
19
 */
20 1
class Translator implements ITranslator {
21 1
  use \Nette\SmartObject;
22
  
23
  /** @var ILoader */
24
  protected $loader;
25
  /** @var string[] */
26
  protected $untranslated = [];
27
  /** @var callable[] */
28
  public $onUntranslated = [];
29
  
30
  function __construct(ILoader $loader) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
31 1
    $this->loader = $loader;
32 1
  }
33
  
34
  function getLang(): string {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
35 1
    return $this->loader->getLang();
36
  }
37
  
38
  function setLang(string $lang) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
39 1
    $this->loader->setLang($lang);
40 1
  }
41
  
42
  /**
43
   * @return string[]
44
   */
45
  function getUntranslated(): array {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
46 1
    return $this->untranslated;
47
  }
48
  
49
  /**
50
   * @param string $message
51
   * @return string[]
52
   */
53
  protected function extractDomainAndMessage(string $message): array {
54 1
    if(!Strings::contains($message, ".")) {
55 1
      return ["messages", $message];
56
    }
57 1
    return explode(".", $message, 2);
58
  }
59
  
60
  /**
61
   * Translate multi-level message
62
   */
63
  protected function multiLevelTrans(array $message, array $texts): string {
64 1
    $text = $texts;
65 1
    foreach($message as $part) {
66 1
      $text = Arrays::get($text, $part, "");
67 1
      if($text === "") {
68 1
        break;
69
      }
70
    }
71 1
    return $text;
72
  }
73
  
74
  /**
75
   * Choose correct variant of message depending on $count
76
   */
77
  protected function multiChoiceTrans(string $message, int $count): string {
78 1
    $variants = explode("|", $message);
79 1
    foreach($variants as $variant) {
80 1
      $interval = Intervals::findInterval($variant);
81 1
      if(is_string($interval) AND Intervals::isInInterval($count, $interval)) {
82 1
        return Strings::trim(Strings::after($variant, $interval));
83
      }
84
    }
85 1
    return "";
86
  }
87
  
88
  function logUntranslatedMessage(string $message): void {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
89 1
    $this->untranslated[] = $message;
90 1
  }
91
  
92
  /**
93
   * Translate the string
94
   *
95
   * @param string $message
96
   * @param int $count
97
   * @param array $params
98
   * @return string
99
   */
100
  function translate($message, $count = 0, $params = []) {
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
101 1
    list($domain, $m) = $this->extractDomainAndMessage($message);
102 1
    $texts = Arrays::get($this->loader->getTexts(), $domain, []);
103 1
    $parts = explode(".", $m);
104 1
    if(count($parts) === 1) {
105 1
      $parts = [$m];
106
    }
107 1
    $text = $this->multiLevelTrans($parts, $texts);
108 1
    if($text === "") {
109 1
      $this->onUntranslated($message);
110 1
      return $message;
111
    }
112 1
    if(is_string(Intervals::findInterval($text))) {
113 1
      $text = $this->multiChoiceTrans($text, $count);
114
    }
115 1
    $params["count"] = $count;
116 1
    foreach($params as $key => $value) {
117 1
      $text = str_replace("%$key%", $value, $text);
118
    }
119 1
    return $text;
120
  }
121
}
122
?>