33
2
mirror of https://github.com/joomla-extensions/jedchecker.git synced 2024-11-30 16:53:59 +00:00

Merge PR #122 into devlope

This commit is contained in:
Llewellyn van der Merwe 2021-08-31 11:50:23 +02:00
commit 4b14943af4
Signed by: Llewellyn
GPG Key ID: EFC0C720A240551C
2 changed files with 93 additions and 7 deletions

View File

@ -131,13 +131,14 @@ COM_JEDCHECKER_LANG_TRANSLATION_ERROR="Invalid translation string"
COM_JEDCHECKER_LANG_INVALID_UTF8="Invalid UTF8 string"
COM_JEDCHECKER_LANG_TRANSLATION_QUOTES="All translation strings should be enclosed in double quotation marks."
COM_JEDCHECKER_LANG_TRANSLATION_MISSED_LEFT_QUOTE="You have missed the left quote."
COM_JEDCHECKER_LANG_TRANSLATION_MISSED_RIGTH_QUOTE="You have missed the right quote"
COM_JEDCHECKER_LANG_TRANSLATION_MISSED_RIGHT_QUOTE="You have missed the right quote"
COM_JEDCHECKER_LANG_TRANSLATION_EMPTY="Empty translation string"
COM_JEDCHECKER_LANG_QQ_DEPRECATED="Usage of \"_QQ_\" is deprecated since Joomla! 3.9. Use escaped double quotes (\\\") instead"
COM_JEDCHECKER_LANG_QQDEPRECATED="Usage of "_QQ_" is deprecated since Joomla! 3.9. Use escaped double quotes (\\") instead"
COM_JEDCHECKER_LANG_UNESCAPED_QUOTE="Unescaped double quotation mark found"
COM_JEDCHECKER_LANG_VARIABLE_REF="A variable reference found in the translation string."
COM_JEDCHECKER_LANG_INCORRECT_ARGNUM="Probably you use incorrect placeholder format (e.g. '%1s' instead of '%1$s'), see <a href='https://www.php.net/manual/en/function.sprintf.php'>printf's argnum format specification</a> for details"
COM_JEDCHECKER_LANG_SPACES_AROUND="Spaces around the translation string are detected"
COM_JEDCHECKER_LANG_UNKNOWN_KEY_IN_CODE="Unknown language key '%s' found in the code."
COM_JEDCHECKER_TOOLBAR_CHECK="Check"
COM_JEDCHECKER_TOOLBAR_CLEAR="Clear"
COM_JEDCHECKER_CLICK_TO_VIEW_DETAILS="Click to View Details"

View File

@ -44,6 +44,13 @@ class JedcheckerRulesLanguage extends JEDcheckerRule
*/
protected $description = 'COM_JEDCHECKER_LANG_DESC';
/**
* Key-value map for language translations
*
* @var array
*/
protected $langKeys = array();
/**
* Initiates the search and check
*
@ -61,12 +68,36 @@ class JedcheckerRulesLanguage extends JEDcheckerRule
(where "tag" is a language code, e.g. en-GB, and "extension" is the extension element name, e.g. com_content)
Joomla!4 allows to skip tag prefix inside of the tag directory
(i.e. to name files as extension.ini and extension.sys.ini) */
if (preg_match('#(?:^|/)([a-z]{2,3}-[A-Z]{2})[./]\w+(?:\.sys)?\.ini$#', $file, $match))
if (preg_match('#(?:^|/)([a-z]{2,3}-[A-Z]{2})(?:[./]\w+)?(?:\.sys)?\.ini$#', $file, $match))
{
$tag = $match[1];
// Try to validate the file
$this->find($file, $match[1]);
$this->find($file, $tag);
if ($tag === 'en-GB')
{
$this->populateLangKeys($file);
}
}
}
// Load default Joomla's translations
$files = version_compare(JVERSION, '4.0', '>=') ? array('joomla.ini', 'lib_joomla.ini') : array('en-GB.ini', 'en-GB.lib_joomla.ini');
foreach ($files as $file)
{
$this->populateLangKeys(JPATH_ROOT . '/language/en-GB/' . $file);
$this->populateLangKeys(JPATH_ADMINISTRATOR . '/language/en-GB/' . $file);
}
// Check JText usage
$files = JFolder::files($this->basedir, '\.php$', true, true);
foreach ($files as $file)
{
$this->findJText($file);
}
}
/**
@ -89,7 +120,7 @@ class JedcheckerRulesLanguage extends JEDcheckerRule
// Check EOL format is \n (not \r or \n\r)
if (strpos($content, "\r") !== false)
{
$this->report->addWarning($file, JText::_('COM_JEDCHECKER_LANG_INCORRECT_EOL'));
$this->report->addInfo($file, JText::_('COM_JEDCHECKER_LANG_INCORRECT_EOL', false, false));
}
$lines = file($file);
@ -123,7 +154,7 @@ class JedcheckerRulesLanguage extends JEDcheckerRule
$this->report->addWarning($file, JText::_('COM_JEDCHECKER_LANG_BOM_FOUND'), $startLineno);
}
// Romeve BOM for further checks
// Remove BOM for further checks
$line = substr($line, 3);
}
@ -272,7 +303,7 @@ class JedcheckerRulesLanguage extends JEDcheckerRule
// Check for legacy "_QQ_" code (deprecated since Joomla! 3.9 in favour of escaped double quote \"; removed in Joomla! 4)
if (strpos($value, '"_QQ_"') !== false)
{
$this->report->addCompat($file, JText::_('COM_JEDCHECKER_LANG_QQ_DEPRECATED'), $startLineno, $line);
$this->report->addCompat($file, JText::_('COM_JEDCHECKER_LANG_QQDEPRECATED'), $startLineno, $line);
}
// Convert "_QQ_" to escaped quotes for further analysis
@ -328,4 +359,58 @@ class JedcheckerRulesLanguage extends JEDcheckerRule
// All checks passed. Return true
return true;
}
/**
* Appends keys from INI file to the list
*
* @param string $file Language INI-file name
*
* @return void
*/
protected function populateLangKeys($file)
{
if (is_file($file))
{
$data = parse_ini_file($file);
if (is_array($data))
{
$this->langKeys = array_replace($this->langKeys, $data);
}
}
}
/**
* Reads PHP files and checks JText arguments
*
* @param string $file - The path to the file
*
* @return boolean True on success, otherwise False.
*/
protected function findJText($file)
{
$content = file_get_contents($file);
// Search for Text/JText calls
if (!preg_match_all('/\bJ?Text::(?:_|s?printf|alt|plural|script)\s*\(\s*([\'])([^\'"]+)\1\s*[\),]/', $content, $matches, PREG_OFFSET_CAPTURE))
{
return true;
}
$lines = explode("\n", $content);
// Check all keys exist in INI files
foreach ($matches[2] as $match)
{
$key = strtoupper($match[0]);
if (!isset($this->langKeys[$key]))
{
$lineno = substr_count($content, "\n", 0, $match[1]);
$this->report->addInfo($file, JText::sprintf('COM_JEDCHECKER_LANG_UNKNOWN_KEY_IN_CODE', htmlspecialchars($key)), $lineno + 1, $lines[$lineno]);
}
}
return true;
}
}