Completed
Push — master ( 4e569b...f4448f )
by Filip
03:08
created

PrefixedTranslator::overrideTemplateTranslator()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 14
rs 8.8571
cc 5
eloc 8
nc 4
nop 2
1
<?php
2
3
/**
4
 * This file is part of the Kdyby (http://www.kdyby.org)
5
 *
6
 * Copyright (c) 2008 Filip Procházka ([email protected])
7
 *
8
 * For the full copyright and license information, please view the file license.txt that was distributed with this source code.
9
 */
10
11
namespace Kdyby\Translation;
12
13
use Kdyby;
14
use Latte;
15
use Nette;
16
use Nette\Utils\Strings;
17
18
19
20
/**
21
 * @author Filip Procházka <[email protected]>
22
 */
23
class PrefixedTranslator extends Nette\Object implements ITranslator
24
{
25
26
	/**
27
	 * @var \Nette\Localization\ITranslator
28
	 */
29
	private $translator;
30
31
	/**
32
	 * @var string
33
	 */
34
	private $prefix;
35
36
37
38
	/**
39
	 * @param string $prefix
40
	 * @param ITranslator $translator
41
	 */
42
	public function __construct($prefix, ITranslator $translator)
43
	{
44
		if ($translator instanceof PrefixedTranslator) { // todo: this is just an experiment
45
			$translator = $translator->unwrap();
46
		}
47
48
		$this->translator = $translator;
49
		$this->prefix = rtrim($prefix, '.');
50
	}
51
52
53
54
	/**
55
	 * @param string $message
56
	 * @param int|array|NULL $count
57
	 * @param array|string|NULL $parameters
58
	 * @param string|NULL $domain
59
	 * @param string|NULL $locale
60
	 * @return string
61
	 */
62
	public function translate($message, $count = NULL, $parameters = [], $domain = NULL, $locale = NULL)
63
	{
64
		$translationString = ($message instanceof Phrase ? $message->message : $message);
65
		$prefix = $this->prefix . '.';
66
67
		if (Strings::startsWith($message, '//')) {
68
			$prefix = NULL;
69
			$translationString = Strings::substring($translationString, 2);
70
		}
71
72
		if ($message instanceof Phrase) {
73
			return $this->translator->translate(new Phrase($prefix . $translationString, $message->count, $message->parameters, $message->domain, $message->locale));
74
		}
75
76 View Code Duplication
		if (is_array($count)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
			$locale = $domain ?: NULL;
78
			$domain = $parameters ?: NULL;
79
			$parameters = $count ?: [];
80
			$count = NULL;
81
		}
82
83
		return $this->translator->translate($prefix . $translationString, $count, (array) $parameters, $domain, $locale);
0 ignored issues
show
Unused Code introduced by
The call to ITranslator::translate() has too many arguments starting with (array) $parameters.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
84
	}
85
86
87
88
	/**
89
	 * @return ITranslator
90
	 */
91
	public function unwrap()
92
	{
93
		return $this->translator;
94
	}
95
96
97
98
	/**
99
	 * @param $template
100
	 * @return ITranslator
101
	 */
102
	public function unregister($template)
103
	{
104
		return self::overrideTemplateTranslator($template, $this->unwrap());
105
	}
106
107
108
109
	/**
110
	 * @param Latte\Template|\Nette\Bridges\ApplicationLatte\Template|\Nette\Templating\Template $template
111
	 * @param string $prefix
112
	 * @return ITranslator
113
	 * @throws InvalidArgumentException
114
	 */
115
	public static function register($template, $prefix)
116
	{
117
		$translator = new static($prefix, $template->global->translator);
118
		return self::overrideTemplateTranslator($template, $translator);
119
	}
120
121
122
123
	/**
124
	 * @param Latte\Template|\Nette\Bridges\ApplicationLatte\Template|\Nette\Templating\Template $template
125
	 * @param string $prefix
126
	 * @return ITranslator
127
	 * @throws InvalidArgumentException
128
	 */
129
	public static function register23($template, $prefix)
130
	{
131
		try {
132
			$translator = $template->getTranslator();
133
134
		} catch (\LogicException $e) {
135
			throw new InvalidArgumentException('Please register helpers from \Kdyby\Translation\TemplateHelpers before using translator prefixes.', 0, $e);
136
		}
137
138
		/** @var ITranslator $translator */
139
		$translator = new static($prefix, $translator);
140
		return self::overrideTemplateTranslator($template, $translator);
141
	}
142
143
144
145
	/**
146
	 * @param Latte\Template|Latte\Runtime\Template|\Nette\Bridges\ApplicationLatte\Template|\Nette\Templating\Template $template
147
	 * @param ITranslator $translator
148
	 */
149
	private static function overrideTemplateTranslator($template, ITranslator $translator)
150
	{
151
		if ($template instanceof Latte\Runtime\Template || $template instanceof Latte\Template) {
0 ignored issues
show
Bug introduced by
The class Latte\Template does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
152
			$template->getEngine()->addFilter('translate', [new TemplateHelpers($translator), 'translate']);
153
154
		} elseif ($template instanceof \Nette\Bridges\ApplicationLatte\Template) {
155
			$template->getLatte()->addFilter('translate', [new TemplateHelpers($translator), 'translate']);
156
157
		} elseif ($template instanceof \Nette\Templating\Template) {
158
			$template->registerHelper('translate', [new TemplateHelpers($translator), 'translate']);
159
		}
160
161
		return $translator;
162
	}
163
164
}
165