function.localize.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 
12 /**
13  * Extract the language dependent value of a node attribute.
14  *
15  * Example:
16  * @code
17  * {localize node=$project attribute="text" lang="en"}
18  * @endcode
19  *
20  * @param $params Array with keys:
21  * - node: The Node instance to extract the value from
22  * - attribute: The name of the attribute (will be appended with "_$lang")
23  * - lang: Language of the attribute
24  * @param $template Smarty_Internal_Template
25  * @return String
26  */
27 function smarty_function_localize(array $params, Smarty_Internal_Template $template) {
28  $language = $params['lang'];
29  $node = $params['node'];
30  $attribute = $params['attribute'];
31 
32  $defaultAttr = $attribute.'_'.$language;
33  $defaultValue = $node->getValue($defaultAttr);
34  if ($defaultValue) {
35  return $defaultValue;
36  }
37  else {
38  $fallbackAttr = $attribute.'_de';
39  return $node->getValue($fallbackAttr);
40  }
41 }
42 ?>