FileMessage.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 namespace wcmf\lib\i18n\impl;
12 
15 
16 /**
17  * FileMessage retrieves localized messages from files, that are
18  * stored in the localization directory. Inside this directory there must
19  * be a messages_$lang.php files for each language defining the translation
20  * for each message in an associative array called $messages.
21  *
22  * For example the messages_de file could have the following content:
23  * @code
24  * $messages = [
25  * 'up' => 'hoch',
26  * 'down' => 'runter',
27  * ...
28  * ];
29  * @endcode
30  *
31  * @author ingo herwig <ingo@wemove.com>
32  */
33 class FileMessage implements Message {
34 
35  private $localeDir;
36  private $language;
37 
38  /**
39  * Cache for loaded translations
40  */
41  private $translations = [];
42 
43  /**
44  * Constructor
45  * @param $localeDir The directory
46  * @param $language
47  */
48  public function __construct($localeDir, $language='') {
49  $this->localeDir = FileUtil::realpath(WCMF_BASE.$localeDir).'/';
50  if (strlen($language) > 0) {
51  $this->language = $language;
52  }
53  else if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
54  $this->language = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
55  }
56  }
57 
58  /**
59  * @see Message::getText()
60  */
61  public function getText($message, $parameters=null, $lang='') {
62  // get the translations
63  $lang = strlen($lang) == 0 ? $this->language : $lang;
64  $translations = $this->getTranslations($lang);
65  $localizedMessage = $message;
66  if (isset($translations[$message]) && strlen($translations[$message]) > 0) {
67  $localizedMessage = $translations[$message];
68  }
69 
70  // replace parameters
71  preg_match_all("/%([0-9]+)%/", $localizedMessage, $matches);
72  $matches = $matches[1];
73  for ($i=0, $count=sizeof($matches); $i<$count; $i++) {
74  $matches[$i] = '/\%'.$matches[$i].'\%/';
75  }
76  sort($matches);
77  if (sizeof($matches) > 0 && is_array($parameters)) {
78  $localizedMessage = preg_replace($matches, $parameters, $localizedMessage);
79  }
80  return $localizedMessage;
81  }
82 
83  /**
84  * @see Message::getAll()
85  */
86  public function getAll($lang='') {
87  // get the translations
88  $translations = $this->getTranslations($lang);
89  return $translations;
90  }
91 
92  /**
93  * Get all translations for a language.
94  * @param $lang The language (optional, default: '')
95  * @return The translations as associative array
96  */
97  private function getTranslations($lang) {
98  if (!isset($this->translations[$lang])) {
99  $messageFile = $this->localeDir."/messages_".$lang.".php";
100  if (file_exists($messageFile)) {
101  require_once($messageFile);
102  // store for later reference
103  $this->translations[$lang] = ${"messages_$lang"};
104  }
105  else {
106  $this->translations[$lang] = [];
107  }
108  }
109  return $this->translations[$lang];
110  }
111 }
112 ?>
__construct($localeDir, $language='')
Constructor.
Definition: FileMessage.php:48
getText($message, $parameters=null, $lang='')
Definition: FileMessage.php:61
FileUtil provides basic support for file functionality like HTTP file upload.
Definition: FileUtil.php:22
static realpath($path)
Realpath function that also works for non existing paths code from http://www.php....
Definition: FileUtil.php:244
Message is used to get localized messages to be used in the user interface.
Definition: Message.php:23
FileMessage retrieves localized messages from files, that are stored in the localization directory.
Definition: FileMessage.php:33