Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed problems if translation had parameters #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
98 changes: 60 additions & 38 deletions src/Philo/Translate/Console/DiggCommand.php
Expand Up @@ -75,17 +75,13 @@ public function fire()
continue;
}

foreach($this->manager->getLanguages() as $language)
{
$lang_query = array_get($translate, 'lang_query');
$parameters = array_get($translate, 'parameters');
$group = array_get($translate, 'group');
$line = array_get($translate, 'line');

if(Lang::get($lang_query, $parameters) == $lang_query)
{
if(is_null($translation = $this->ask("Translate '$lang_query'" . ( ! empty($parameters) ? " [" . implode(',', $parameters) . "]" : null) . " in " . strtoupper($language) . ": "))) continue;
$this->manager->setLanguage($language)->addLine($group, $line, $translation);
foreach($this->manager->getLanguages() as $language) {
$this->manager->setLanguage($language);


if(\Lang::get($translate['lang_query']) == $translate['lang_query']) {
if(is_null($translation = $this->ask(($translate['parameters'] ? "NOTICE language guery contains parameters!\n": '')."Translate '{$translate['lang_query']}' in " . strtoupper($language) . ": "))) continue;
$this->manager->setLanguage($language)->addLine($translate['group'], $translate['line'], $translation, true);
}
}
}
Expand All @@ -102,51 +98,77 @@ public function getTranlations($file)
{
$data = File::get($file);

// Try to pick up all Lang functions from file
preg_match_all('/(trans|Lang::get)\(([^\)]*)\)/imU', $data, $matches, PREG_PATTERN_ORDER);

// Try to pick up all Lang funktions from file
preg_match_all('/Lang::get\s*\([^;]*\)\s*;/iU', $data, $matches, PREG_PATTERN_ORDER);

// return empty array if none found
if(empty($matches[2])) {
if(empty($matches[0])) {
return array();
}

// return unique translations
$files = array_unique($matches[2]);

// clean up
foreach ($files as &$item) {

// Set parameters [], if we have parameters on our translatios, fill it up next
$parameters = array();
$files = array_unique($matches[0]);

// separate parameters path from parameters
preg_match('/,\s*\t*(\[.*\])/i', $item, $parts);
if( ! empty($parts)) {
// array containing all lang queries
$lang_queries = [];

$item = str_replace($parts[0], '', $item);
$_parameters = $parts[1];
// clean up
foreach ($files as $item) {

$token = token_get_all('<?php '.str_replace(' ', '', $item).'?>');
foreach ($token as $key => $value) {
if(is_array($value)) {
// We noticed begining of Lang, validate and pick language query
if(self::getTokenValue($token, $key, T_STRING) === 'Lang') {
if(self::getTokenValue($token, $key+1, T_DOUBLE_COLON)!==false and self::getTokenValue($token, $key+2, T_STRING) === 'get' and self::getTokenValue($token, $key+4)!==false) {
$_i = substr(self::getTokenValue($token, $key+4), 1, -1);

$lang_queries[] = [
'lang_query' => $_i,
'valid' => true,
'group' => substr($_i, 0, strpos($_i, '.')),
'line' => substr($_i, strpos($_i, '.')+1),
'parameters' => (self::getTokenValue($token, ($key+5)) == ','),
];

}
}
}

preg_match_all('/("|\')([^\'"]*)("|\')\s*\t*\n*\r*=\s*>/iU', $_parameters, $keys);
}

if(empty($keys[2])) {
$parameters = array_fill_keys($keys[2], 'val');
}

return $lang_queries;
}

$_i = trim(str_replace(array('\'', '"'), '', $item));
/**
* Get the console command arguments.
*
* @return array
*/
protected function getTokenValue( array $tokens, $key, $token=null)
{

// Our token part is just a string, return string only if no token is defined
if(!is_array($tokens[$key])) {
if($token > 0) {
return false;
}

return $tokens[$key];
}

$item = array(
'lang_query' => $_i,
'valid' => (preg_match('/[^0-9a-zA-Z\._]/', $_i) == 0),
'group' => substr($_i, 0, strpos($_i, '.')),
'line' => substr($_i, strpos($_i, '.')+1),
'parameters' => $parameters,
);
if($token>0) {
return ($tokens[$key][0] == $token ? $tokens[$key][1] : false);
}

return $files;
return $tokens[$key][1];

}


/**
* Get the console command arguments.
*
Expand Down