Skip to content
This repository has been archived by the owner on Apr 5, 2018. It is now read-only.

Commit

Permalink
Added support for nested plugin and template sources
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Olde Hampsink committed Dec 11, 2014
1 parent 6cc284b commit a69f7d8
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 64 deletions.
35 changes: 33 additions & 2 deletions elementtypes/TranslateElementType.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,35 @@ public function populateElementModel($row)
// Define the sources
public function getSources($context = null)
{

// Get plugin sources
$pluginSources = array();
$plugins = craft()->plugins->getPlugins();
foreach($plugins as $path => $plugin) {
$pluginSources['plugins:'.$path] = array(
'label' => $plugin->classHandle,
'criteria' => array(
'source' => craft()->path->getPluginsPath().$path
)
);
}

// Get template sources
$templateSources = array();
$templates = IOHelper::getFolderContents(craft()->path->getSiteTemplatesPath(), false);
foreach($templates as $template) {

// Get path/name of html/twig/js/json files and folders
preg_match('/(.*)\/(.*?)(\.(html|twig|js)|\/)$/', $template, $matches);
$path = $matches[2];

$templateSources['templates:'.$path] = array(
'label' => $path,
'criteria' => array(
'source' => $template
)
);
}

// Get default sources
$sources = array(
Expand All @@ -90,13 +119,15 @@ public function getSources($context = null)
'label' => Craft::t('Plugins'),
'criteria' => array(
'source' => craft()->path->getPluginsPath()
)
),
'nested' => $pluginSources
),
'templates' => array(
'label' => Craft::t('Templates'),
'criteria' => array(
'source' => craft()->path->getSiteTemplatesPath()
)
),
'nested' => $templateSources
)
);

Expand Down
157 changes: 95 additions & 62 deletions services/TranslateService.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,72 +89,38 @@ public function get($criteria)

// Loop through paths
foreach($criteria->source as $path) {

// Check if this is a folder or a file
$isFile = IOHelper::fileExists($path);

// If its not a file
if(!$isFile) {

// Set filter - no vendor folders, only php, html, twig or js
$filter = '^((?!vendor).)*(\.(php|html|twig|js)?)$';

// Get files
$files = IOHelper::getFolderContents($path, true, $filter);

// Loop through files and find translate occurences
foreach($files as $file) {

// Get file contents
$contents = IOHelper::getFileContents($file);

// Get extension
$extension = IOHelper::getExtension($file);

// Get matches per extension
foreach($this->_expressions[$extension] as $regex) {

// Match translation functions
if(preg_match_all($regex, $contents, $matches)) {

// Collect
foreach($matches[2] as $original) {

// Translate
$translation = Craft::t($original, array(), null, $criteria->locale);

// Show translation in textfield
$field = craft()->templates->render('_includes/forms/text', array(
'id' => ElementHelper::createSlug($original),
'name' => 'translation[' . $original . ']',
'value' => $translation,
'placeholder' => $translation
));

// Fill element with translation data
$element = TranslateModel::populateModel(array(
'id' => ElementHelper::createSlug($original),
'original' => $original,
'translation' => $translation,
'source' => $path,
'file' => $file,
'locale' => $criteria->locale,
'field' => $field
));
// Set filter - no vendor folders, only template files
$filter = '^((?!vendor).)*(\.(php|html|twig|js)?)$';

// Get files
$files = IOHelper::getFolderContents($path, true, $filter);

// If searching, only return matches
if($criteria->search && !stristr($element->original, $criteria->search) && !stristr($element->translation, $criteria->search)) {
continue;
}

// If wanting one status, ditch the rest
if($criteria->status && $criteria->status != $element->getStatus()) {
continue;
}

// Collect in array
$occurences[$original] = $element;
// Loop through files and find translate occurences
foreach($files as $file) {

// Parse file
$elements = $this->_parseFile($path, $file, $criteria);

// Collect in array
$occurences = array_merge($occurences, $elements);

}

}

}


} else {

// Parse file
$elements = $this->_parseFile($path, $path, $criteria);

// Collect in array
$occurences = array_merge($occurences, $elements);

}

}
Expand All @@ -163,4 +129,71 @@ public function get($criteria)

}

protected function _parseFile($path, $file, $criteria)
{

// Collect matches in file
$occurences = array();

// Get file contents
$contents = IOHelper::getFileContents($file);

// Get extension
$extension = IOHelper::getExtension($file);

// Get matches per extension
foreach($this->_expressions[$extension] as $regex) {

// Match translation functions
if(preg_match_all($regex, $contents, $matches)) {

// Collect
foreach($matches[2] as $original) {

// Translate
$translation = Craft::t($original, array(), null, $criteria->locale);

// Show translation in textfield
$field = craft()->templates->render('_includes/forms/text', array(
'id' => ElementHelper::createSlug($original),
'name' => 'translation[' . $original . ']',
'value' => $translation,
'placeholder' => $translation
));

// Fill element with translation data
$element = TranslateModel::populateModel(array(
'id' => ElementHelper::createSlug($original),
'original' => $original,
'translation' => $translation,
'source' => $path,
'file' => $file,
'locale' => $criteria->locale,
'field' => $field
));

// If searching, only return matches
if($criteria->search && !stristr($element->original, $criteria->search) && !stristr($element->translation, $criteria->search)) {
continue;
}

// If wanting one status, ditch the rest
if($criteria->status && $criteria->status != $element->getStatus()) {
continue;
}

// Collect in array
$occurences[$original] = $element;

}

}

}

// Return occurences
return $occurences;

}

}

0 comments on commit a69f7d8

Please sign in to comment.