0
0
Fork 0
mirror of https://github.com/salesagility/SuiteCRM.git synced 2025-03-16 06:13:34 +00:00
salesagility_SuiteCRM/include/LangText.php

222 lines
6.3 KiB
PHP
Raw Normal View History

2018-03-19 11:55:19 +00:00
<?php
2018-03-19 15:20:32 +00:00
2018-03-19 11:55:19 +00:00
/**
*
* SugarCRM Community Edition is a customer relationship management program developed by
* SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
*
* SuiteCRM is an extension to SugarCRM Community Edition developed by SalesAgility Ltd.
* Copyright (C) 2011 - 2018 SalesAgility Ltd.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by the
* Free Software Foundation with the addition of the following permission added
* to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
* IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
* OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License along with
* this program; if not, see http://www.gnu.org/licenses or write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
* You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
* SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "Powered by
* SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
* reasonably feasible for technical reasons, the Appropriate Legal Notices must
* display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM".
*/
namespace SuiteCRM;
if (!defined('sugarEntry') || !sugarEntry) {
die('Not A Valid Entry Point');
}
/**
2018-04-13 12:47:01 +00:00
* LangText
2018-03-19 11:55:19 +00:00
*
* @author gyula
*/
class LangText
{
2018-03-19 15:20:32 +00:00
2018-03-19 11:55:19 +00:00
/**
* string
*/
const LOG_LEVEL = 'fatal';
2018-03-19 15:20:32 +00:00
2018-03-19 11:55:19 +00:00
/**
* integer
*/
const USING_MOD_STRINGS = 1;
2018-03-19 15:20:32 +00:00
2018-03-19 11:55:19 +00:00
/**
* integer
*/
const USING_APP_STRINGS = 2;
2018-03-19 15:20:32 +00:00
2018-03-19 11:55:19 +00:00
/**
* integer
*/
2018-03-19 15:20:32 +00:00
const USING_ALL_STRINGS = 3;
2018-03-19 11:55:19 +00:00
/**
*
* @var string
*/
protected $key;
2018-03-19 15:20:32 +00:00
2018-03-19 11:55:19 +00:00
/**
*
* @var array
*/
protected $args;
2018-03-19 15:20:32 +00:00
2018-03-19 11:55:19 +00:00
/**
*
2018-03-19 15:20:32 +00:00
* @var integer
2018-03-19 11:55:19 +00:00
*/
protected $use;
2018-03-19 15:20:32 +00:00
2018-03-19 11:55:19 +00:00
/**
*
* @var boolean
*/
protected $log;
2018-03-19 15:20:32 +00:00
2018-03-19 11:55:19 +00:00
/**
*
* @var boolean
*/
protected $throw;
2018-09-24 10:17:02 +00:00
protected $module;
protected $lang;
2018-03-19 15:20:32 +00:00
2018-03-19 11:55:19 +00:00
/**
2018-03-19 15:20:32 +00:00
*
2018-03-19 11:55:19 +00:00
* @param string|null $key
* @param array|null $args
* @param integer $use
* @param boolean $log
* @param boolean $throw
*/
2018-09-24 10:17:02 +00:00
public function __construct($key = null, $args = null, $use = self::USING_ALL_STRINGS, $log = true, $throw = true, $module = null, $lang = null)
2018-03-19 11:55:19 +00:00
{
$this->key = $key;
$this->args = $args;
$this->use = $use;
$this->log = $log;
$this->throw = $throw;
2018-09-24 10:17:02 +00:00
$this->module = $module;
$this->lang = $lang;
2018-03-19 11:55:19 +00:00
}
2018-03-19 15:20:32 +00:00
2018-03-19 11:55:19 +00:00
/**
2018-03-19 15:20:32 +00:00
*
2018-03-19 11:55:19 +00:00
* @global array $app_strings
* @global array $mod_strings
* @param string|null $key
* @param array|null $args
* @param integer|null $use
* @return string
2018-03-22 13:24:38 +00:00
* @throws ErrorMessageException
2018-03-19 11:55:19 +00:00
*/
2018-09-24 10:17:02 +00:00
public function getText($key = null, $args = null, $use = null, $module = null, $lang = null)
2018-03-19 15:20:32 +00:00
{ // TODO: rename the methode to LangText::translate()
2018-03-19 11:55:19 +00:00
// TODO: app_strings and mod_strings could be in separated methods
2018-09-24 08:42:46 +00:00
global $app_strings, $mod_strings, $app_list_strings;
2018-09-24 10:17:02 +00:00
$module = $module ? $module : $this->module;
if (!$mod_strings && $module) {
// retrieve translation for specified module
$lang = $lang ? $lang : ($this->lang ? $this->lang : $GLOBALS['current_language']);
include_once __DIR__ . '/SugarObjects/LanguageManager.php';
\LanguageManager::loadModuleLanguage($module, $lang);
}
2018-03-19 15:20:32 +00:00
2018-03-19 11:55:19 +00:00
if (!is_null($key)) {
$this->key = $key;
}
2018-03-19 15:20:32 +00:00
2018-03-19 11:55:19 +00:00
if (!is_null($args)) {
$this->args = $args;
}
2018-03-19 15:20:32 +00:00
2018-03-19 11:55:19 +00:00
if (!is_null($use)) {
$this->use = $use;
}
2018-03-19 15:20:32 +00:00
2018-03-19 11:55:19 +00:00
if ($this->use === self::USING_MOD_STRINGS) {
2018-03-19 15:20:32 +00:00
$text = isset($mod_strings[$this->key]) && $mod_strings[$this->key] ? $mod_strings[$this->key] : null;
2018-03-19 11:55:19 +00:00
} elseif ($this->use === self::USING_APP_STRINGS) {
2018-03-19 15:20:32 +00:00
$text = isset($app_strings[$this->key]) && $app_strings[$this->key] ? $app_strings[$this->key] : null;
} elseif ($this->use === self::USING_ALL_STRINGS) {
$text = isset($mod_strings[$this->key]) && $mod_strings[$this->key] ? $mod_strings[$this->key] : (
2018-09-24 08:42:46 +00:00
isset($app_strings[$this->key]) ? $app_strings[$this->key] : (
isset($app_list_strings[$this->key]) ? $app_list_strings[$this->key] : null
)
2018-03-20 10:28:04 +00:00
);
2018-03-19 11:55:19 +00:00
} else {
2018-03-20 10:28:04 +00:00
ErrorMessage::drop('Unknown use case for translation: ' . $this->use);
2018-03-19 11:55:19 +00:00
}
2018-03-19 15:20:32 +00:00
2018-03-19 11:55:19 +00:00
if (!$text) {
if ($this->log) {
2018-03-20 10:28:04 +00:00
ErrorMessage::handler('A language key does not found: [' . $this->key . ']', self::LOG_LEVEL, $this->throw);
2018-03-19 11:55:19 +00:00
} else {
$text = $this->key;
}
}
2018-03-19 15:20:32 +00:00
foreach ((array) $this->args as $name => $value) {
2018-03-19 11:55:19 +00:00
$text = str_replace('{' . $name . '}', $value, $text);
}
2018-03-19 15:20:32 +00:00
2018-03-19 11:55:19 +00:00
return $text;
}
2018-03-19 15:20:32 +00:00
2018-03-19 11:55:19 +00:00
/**
2018-03-19 15:20:32 +00:00
*
2018-03-19 11:55:19 +00:00
* @return string
*/
public function __toString()
{
$text = $this->getText();
return $text;
}
2018-03-19 15:20:32 +00:00
2018-03-19 11:55:19 +00:00
/**
2018-03-19 15:20:32 +00:00
*
2018-03-19 11:55:19 +00:00
* @param string $key
* @param array|null $args
* @param boolean|null $log
2018-03-19 15:20:32 +00:00
* @param integer $use
2018-03-19 11:55:19 +00:00
* @param boolean $throw
* @return string
2018-03-22 13:24:38 +00:00
* @throws ErrorMessageException
2018-03-19 11:55:19 +00:00
*/
2018-03-19 15:20:32 +00:00
public static function get($key, $args = null, $use = self::USING_ALL_STRINGS, $log = true, $throw = true)
2018-03-19 11:55:19 +00:00
{
$text = new LangText($key, $args, $use, $log, $throw);
$translated = $text->getText();
return $translated;
}
}