1 | <?php |
||
32 | class MultilingualContext extends RawMultilingualContext { |
||
33 | |||
34 | /** Multilanguage implementation */ |
||
35 | |||
36 | // Declaring translations variable to store all translations. |
||
37 | public $translations = array(); |
||
38 | |||
39 | // Declaring languages_iso_codes variable to store all languages ISO codes from json file. |
||
40 | public $languages_iso_codes = array(); |
||
41 | |||
42 | |||
43 | // Parse the YAML translations to PHP array variable. |
||
44 | public function parseTranslationFile() { |
||
45 | $base_path = $this->getMinkParameter('files_path'); |
||
46 | $base_path = $base_path."/"; |
||
47 | $file_path = $base_path.$this->multilingual_parameters['translations']; |
||
48 | $yaml = file_get_contents($file_path); |
||
49 | $yaml_parse_array_check = Yaml::parse($yaml); |
||
50 | if(is_array($yaml_parse_array_check)) { |
||
51 | $this->translations = $yaml_parse_array_check; |
||
52 | } |
||
53 | } |
||
54 | |||
55 | // Checks whether the translations file variable is set in the Behat profile and parses it to array. |
||
56 | public function initializeMultilanguage() { |
||
57 | if(isset($this->multilingual_parameters['translations'])) { |
||
58 | $this->parseTranslationFile(); |
||
59 | } |
||
60 | $this->parseLanguageCodes(); |
||
61 | } |
||
62 | |||
63 | //This function parses the languages_iso_codes.json file to an array. |
||
64 | public function parseLanguageCodes() { |
||
65 | $languages_iso_codes_string = file_get_contents("vendor/kolev/multilingual-extension/src/Resources/languages_iso_codes.json"); |
||
66 | $this->languages_iso_codes = json_decode($languages_iso_codes_string, true); |
||
|
|||
67 | } |
||
68 | |||
69 | /* |
||
70 | * This function detects site's language based on URL. If no URL language is detected |
||
71 | * the default_language is used. |
||
72 | */ |
||
73 | |||
74 | public function languageDetection() { |
||
75 | $current_url = $this->getSession()->getCurrentUrl(); |
||
76 | $base_url = $this->getMinkParameter('base_url'); |
||
77 | // Checks whether the last symbol in the base_url is /, if not it adds it |
||
78 | if (substr($base_url, -1) != "/") { |
||
79 | $base_url = $base_url."/"; |
||
80 | } |
||
81 | $base_url_length = strlen($base_url); |
||
82 | //Get the 2 symbols in current URL after the base_url when Clean URLs is enabled |
||
83 | $clean_url_language_code = substr($current_url,$base_url_length,2); |
||
84 | $not_clean_url_language_code = substr($current_url,$base_url_length+3,2); |
||
85 | if(in_array($clean_url_language_code,$this->languages_iso_codes) && substr($current_url,$base_url_length+2,1) == "/") { |
||
86 | return $clean_url_language_code; |
||
87 | } |
||
88 | else if (in_array($not_clean_url_language_code,$this->languages_iso_codes) && substr($current_url,$base_url_length+5,1) == "/"){ |
||
89 | return $not_clean_url_language_code; |
||
90 | } |
||
91 | else return $this->multilingual_parameters['default_language']; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * This function localizes the targeted string. It tries to find a definition of the provided text (in English) |
||
96 | * in the translations file that is provided within the profile parameters. If it fails to find translation |
||
97 | * for the requested language it falls back to English. If the string is not defined at all in the translations |
||
98 | * file there will be an exception thrown. |
||
99 | */ |
||
100 | |||
101 | public function localizeTarget($target) { |
||
102 | $translations = $this->multilingual_parameters['translations']; |
||
103 | if(isset($this->translations[$target][$this->multilingual_parameters['default_language']])){ |
||
104 | $target = $this->translations[$target][$this->languageDetection()]; |
||
105 | return $target; |
||
106 | } |
||
107 | elseif (isset($this->translations[$target])) { |
||
108 | return $target; |
||
109 | } |
||
110 | else throw new \Exception ("The text '$target'' is not defined in '$translations' translation file."); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * This function localizes the field based on Drupal standards. default_language variable is used as a base. |
||
115 | */ |
||
116 | |||
117 | public function localizeField($field) { |
||
118 | $re = "/(?:[-])(".$this->multilingual_parameters['default_language'].")(?:[-])/"; |
||
119 | $language = "-".$this->languageDetection()."-"; |
||
120 | $field = preg_replace($re, $language,$field); |
||
121 | return $field; |
||
122 | } |
||
123 | |||
124 | |||
125 | /** |
||
126 | * Initialize the multilingual context before the scenario. |
||
127 | * @BeforeScenario |
||
128 | * @Given /^I initialize multilingual context/ |
||
129 | */ |
||
130 | public function iInitializeMultilingualContext() { |
||
133 | |||
134 | /** |
||
135 | * |
||
136 | * @Given /^I follow localized "(?P<link>(?:[^"]|\\")*)"/ |
||
137 | */ |
||
138 | public function iFollowLocalized($target) { |
||
150 | |||
151 | /** |
||
152 | * |
||
153 | * @Given /^I follow second localized "(?P<link>(?:[^"]|\\")*)"/ |
||
154 | */ |
||
155 | public function iFollowLocalizedSecond($target) { |
||
159 | |||
160 | /** |
||
161 | * Click on some text. |
||
162 | * |
||
163 | * @When /^I click on the localized text "([^"]*)"$/ |
||
164 | */ |
||
165 | |||
166 | public function iClickOnTheLocalizedText($target) { |
||
167 | $target = $this->localizeTarget($target); |
||
168 | $this->iClickOnTheText($target); |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Checks, that page contains specified text in input |
||
173 | * |
||
174 | * @Then /^(?:|I )should see localized value "(?P<text>(?:[^"]|\\")*)" in input "([^"]*)"$/ |
||
175 | */ |
||
176 | |||
177 | public function iShouldSeeLocalizedValueInInput($value, $input) { |
||
181 | |||
182 | /** |
||
183 | * Checks, that page contains specified text |
||
184 | * |
||
185 | * @Then /^(?:|I )should see localized "(?P<text>(?:[^"]|\\")*)"$/ |
||
186 | */ |
||
187 | |||
188 | public function iShouldSeeLocalized($target) { |
||
192 | |||
193 | /** |
||
194 | * Checks, that page doesn't contain specified text |
||
195 | * |
||
196 | * @Then /^(?:|I )should not see localized "(?P<text>(?:[^"]|\\")*)"$/ |
||
197 | */ |
||
198 | public function iShouldNotSeeLocalized($target) |
||
203 | |||
204 | /** |
||
205 | * Waiting for text to appear on a page with certain execution time |
||
206 | * |
||
207 | * @When /^I wait for localized text "([^"]*)" to appear with max time "([^"]+)"(?: seconds)?$/ |
||
208 | */ |
||
209 | public function iWaitForLocalizedTextToAppearWithMaxTime($target, $maxExecutionTime){ |
||
213 | |||
214 | /** |
||
215 | * Fills in form field with specified id|name|label|value |
||
216 | * Example: When I fill in "username" with: "bwayne" |
||
217 | * Example: And I fill in "bwayne" for "username" |
||
218 | * |
||
219 | * @When /^(?:|I )fill in localized "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/ |
||
220 | * @When /^(?:|I )fill in localized "(?P<field>(?:[^"]|\\")*)" with:$/ |
||
221 | * @When /^(?:|I )fill in localized "(?P<value>(?:[^"]|\\")*)" for "(?P<field>(?:[^"]|\\")*)"$/ |
||
222 | */ |
||
223 | public function fillLocalizedField($field, $value) |
||
228 | |||
229 | /** |
||
230 | * @Given I click localized :link in the :rowText row |
||
231 | * @Then I (should )see the localized :link in the :rowText row |
||
232 | */ |
||
233 | public function assertLocalizedClickInTableRow($link, $rowText){ |
||
243 | |||
244 | public function getTableRow(Element $element, $search) { |
||
256 | |||
257 | /** |
||
258 | * Click on text in specified region |
||
259 | * |
||
260 | * @When /^I click on the localized text "([^"]*)" in the "(?P<region>[^"]*)"(?:| region)$/ |
||
261 | */ |
||
262 | public function iClickOnTheLocalizedTextInRegion($text, $region){ |
||
266 | |||
267 | /** |
||
268 | * Choose certain option from given selector |
||
269 | * |
||
270 | * @When I select localized :option from chosen :selector |
||
271 | */ |
||
272 | public function lselectLocalizedOptionWithJavascript($selector, $option) { |
||
276 | |||
277 | /** |
||
278 | * @When I select the localized radio button :label with the id :id |
||
279 | * @When I select the localized radio button :label |
||
280 | * |
||
281 | */ |
||
282 | public function assertSelectLocalizedRadioById($label, $id = '') { |
||
286 | } |
||
287 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..