diff options
author | emkael <emkael@tlen.pl> | 2016-10-31 21:57:54 +0100 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2016-10-31 21:59:22 +0100 |
commit | f1c5b6c282e34a9e6b5c140371acd623c7b50d60 (patch) | |
tree | c4b27a44b9878c900c038bccae7cab102835a669 /app/frontend/mail/MailTemplateTranslator.php | |
parent | d216b3147bc3f37cf2337acab5767c6a4f74aa2e (diff) |
* mail templating module
Diffstat (limited to 'app/frontend/mail/MailTemplateTranslator.php')
-rw-r--r-- | app/frontend/mail/MailTemplateTranslator.php | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/app/frontend/mail/MailTemplateTranslator.php b/app/frontend/mail/MailTemplateTranslator.php new file mode 100644 index 0000000..0e9d7c9 --- /dev/null +++ b/app/frontend/mail/MailTemplateTranslator.php @@ -0,0 +1,80 @@ +<?php + +Prado::using('Lib.phptal.PHPTAL.TranslationService'); + +class MailTemplateTranslator implements PHPTAL_TranslationService { + + private $_module; + private $_catalogue = 'messages'; + private $_charset; + private $_formatter; + private $_source; + + public function __construct($catalogue=NULL) { + $this->_module = Prado::getApplication()->getGlobalization(FALSE); + $this->_catalogue = $catalogue; + } + + private $_language = NULL; + + function setLanguage(/* ... */) { + $this->_source = NULL; + $langs = array_filter(func_get_args()); + if ($langs) { + $availableLangs = $this->_module->AllowedCultures; + foreach ($langs as $lang) { + if (in_array($lang, $availableLangs)) { + $this->_language = $lang; + return; + } + } + throw new TInvalidDataValueException( + Prado::localize( + 'Languages [{lang}] not available', + ['lang' => implode(',', $langs)] + ) + ); + } else { + $this->_language = NULL; + } + } + + function setEncoding($encoding) { + $this->_charset = $encoding; + } + + function useDomain($domain) { + $this->_catalogue = $domain; + } + + private $_variables = []; + + function setVar($key, $value) { + $this->_variables[$key] = $value; + } + + function translate($key, $htmlescape=TRUE) { + $key = preg_replace('/\$(\{.*\})/', '\1', $key); + if ($this->_language) { + if (!$this->_source) { + $appConfig = Prado::getApplication()->getGlobalization()->getTranslationConfiguration(); + $this->_source = MessageSource::factory($appConfig['type'], $appConfig['source'], $appConfig['filename']); + $this->_source->setCulture($this->_language); + } + $this->_formatter = new MessageFormat( + $this->_source, + $this->_charset ?: Prado::getApplication()->getGlobalization()->getCharset() + ); + $variables = []; + foreach ($this->_variables as $varKey => $value) { + $variables['{' . $varKey . '}'] = $value; + } + return $this->_formatter->format($key, $variables, $this->_catalogue); + } else { + return Prado::localize($key, $this->_variables); + } + } + +} + +?> |