diff --git a/main/admin/skill_translate.php b/main/admin/skill_translate.php index 342876cb52..821042161b 100644 --- a/main/admin/skill_translate.php +++ b/main/admin/skill_translate.php @@ -4,6 +4,7 @@ use \Chamilo\CoreBundle\Entity\ExtraField; use Chamilo\CoreBundle\Entity\Skill; use Chamilo\CoreBundle\Entity\Language; +use Chamilo\CoreBundle\Component\Utils\ChamiloApi; $cidReset = true; @@ -24,13 +25,15 @@ if (isset($_GET['skill'])) { $skill = $em->find('ChamiloCoreBundle:Skill', intval($_GET['skill'])); if ($action === 'name') { - $variableLanguage = '$Skill'.api_underscore_to_camel_case( - str_replace(' ', '_', $skill->getName(false)) + $variableLanguage = ChamiloApi::getLanguageVar( + $skill->getName(false), + 'Skill' ); $originalName = $skill->getName(false); } elseif ($action === 'code') { - $variableLanguage = '$SkillCode'.api_underscore_to_camel_case( - str_replace(' ', '_', $skill->getShortCode(false)) + $variableLanguage = ChamiloApi::getLanguageVar( + $skill->getShortCode(false), + 'SkillCode' ); $originalName = $skill->getShortCode(false); } @@ -82,7 +85,7 @@ if ($languageId) { } $form->setDefaults([ - 'variable_language' => $variableLanguage, + 'variable_language' => '$'.$variableLanguage, 'original_name' => $originalName, 'sub_language' => $languageId, 'new_language' => $action === 'code' ? $skill->getShortCode() : $skill->getName() diff --git a/main/inc/lib/skill.lib.php b/main/inc/lib/skill.lib.php index 0acf2da8f1..07c45d83b9 100755 --- a/main/inc/lib/skill.lib.php +++ b/main/inc/lib/skill.lib.php @@ -3,6 +3,7 @@ use Chamilo\UserBundle\Entity\User; use Chamilo\UserBundle\Entity\Repository\UserRepository; +use Chamilo\CoreBundle\Component\Utils\ChamiloApi; /** * Class SkillProfile @@ -1967,11 +1968,9 @@ class Skill extends Model */ public static function translateName($name) { - $camelCase = 'Skill'.api_underscore_to_camel_case( - str_replace(' ', '_', $name) - ); + $variable = ChamiloApi::getLanguageVar($name, 'Skill'); - return isset($GLOBALS[$camelCase]) ? $GLOBALS[$camelCase] : $name; + return isset($GLOBALS[$variable]) ? $GLOBALS[$variable] : $name; } public static function translateCode($code) @@ -1980,10 +1979,8 @@ class Skill extends Model return ''; } - $camelCase = 'SkillCode'.api_underscore_to_camel_case( - str_replace(' ', '_', $code) - ); + $variable = ChamiloApi::getLanguageVar($code, 'SkillCode'); - return isset($GLOBALS[$camelCase]) ? $GLOBALS[$camelCase] : $code; + return isset($GLOBALS[$variable]) ? $GLOBALS[$variable] : $code; } } diff --git a/src/Chamilo/CoreBundle/Component/Utils/ChamiloApi.php b/src/Chamilo/CoreBundle/Component/Utils/ChamiloApi.php index f51942ec22..f3f6f1f3dd 100644 --- a/src/Chamilo/CoreBundle/Component/Utils/ChamiloApi.php +++ b/src/Chamilo/CoreBundle/Component/Utils/ChamiloApi.php @@ -214,4 +214,21 @@ class ChamiloApi return $requestedWith === 'XMLHttpRequest'; } + + /** + * Get a variable name for language file from a text + * @param string $text + * @param string $prefix + * @return string + */ + public static function getLanguageVar($text, $prefix = '') + { + $text = api_replace_dangerous_char($text); + $text = str_replace(['-', ' '], '_', $text); + $text = preg_replace('/\_{1,}/', '_', $text); + //$text = str_replace('_', '', $text); + $text = api_underscore_to_camel_case($text); + + return $prefix.$text; + } } diff --git a/src/Chamilo/CoreBundle/Entity/Skill.php b/src/Chamilo/CoreBundle/Entity/Skill.php index 329efe47c6..9c34a0124d 100644 --- a/src/Chamilo/CoreBundle/Entity/Skill.php +++ b/src/Chamilo/CoreBundle/Entity/Skill.php @@ -3,6 +3,7 @@ namespace Chamilo\CoreBundle\Entity; +use Chamilo\CoreBundle\Component\Utils\ChamiloApi; use Chamilo\SkillBundle\Entity\Profile; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\Mapping as ORM; @@ -124,11 +125,9 @@ class Skill public function getName($translated = true) { if ($translated) { - $camelCase = 'Skill'.api_underscore_to_camel_case( - str_replace(' ', '_', $this->name) - ); + $variable = ChamiloApi::getLanguageVar($this->name, 'Skill'); - return isset($GLOBALS[$camelCase]) ? $GLOBALS[$camelCase] : $this->name; + return isset($GLOBALS[$variable]) ? $GLOBALS[$variable] : $this->name; } return $this->name; @@ -156,11 +155,9 @@ class Skill public function getShortCode($translated = true) { if ($translated && !empty($this->shortCode)) { - $camelCase = 'SkillCode'.api_underscore_to_camel_case( - str_replace(' ', '_', $this->shortCode) - ); + $variable = ChamiloApi::getLanguageVar($this->shortCode, 'SkillCode'); - return isset($GLOBALS[$camelCase]) ? $GLOBALS[$camelCase] : $this->shortCode; + return isset($GLOBALS[$variable]) ? $GLOBALS[$variable] : $this->shortCode; } return $this->shortCode;