2008-10-16 22:42:41 +02:00
|
|
|
<?php
|
2015-08-07 21:07:36 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\i18n;
|
|
|
|
|
|
|
|
use SilverStripe\Core\ClassInfo;
|
|
|
|
use SilverStripe\Core\Object;
|
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Core\Manifest\ClassLoader;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\Debug;
|
|
|
|
use SilverStripe\Control\Director;
|
|
|
|
use ReflectionClass;
|
2015-08-07 21:07:36 +02:00
|
|
|
|
2008-10-16 22:42:41 +02:00
|
|
|
/**
|
2008-11-01 14:26:08 +01:00
|
|
|
* SilverStripe-variant of the "gettext" tool:
|
|
|
|
* Parses the string content of all PHP-files and SilverStripe templates
|
|
|
|
* for ocurrences of the _t() translation method. Also uses the {@link i18nEntityProvider}
|
2014-08-15 08:53:05 +02:00
|
|
|
* interface to get dynamically defined entities by executing the
|
2008-11-01 14:26:08 +01:00
|
|
|
* {@link provideI18nEntities()} method on all implementors of this interface.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-11-01 14:26:08 +01:00
|
|
|
* Collects all found entities (and their natural language text for the default locale)
|
|
|
|
* into language-files for each module in an array notation. Creates or overwrites these files,
|
2013-08-15 21:47:09 +02:00
|
|
|
* e.g. framework/lang/en.yml.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-11-01 14:26:08 +01:00
|
|
|
* The collector needs to be run whenever you make new translatable
|
|
|
|
* entities available. Please don't alter the arrays in language tables manually.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-11-18 20:00:13 +01:00
|
|
|
* Usage through URL: http://localhost/dev/tasks/i18nTextCollectorTask
|
2009-11-10 22:50:27 +01:00
|
|
|
* Usage through URL (module-specific): http://localhost/dev/tasks/i18nTextCollectorTask/?module=mymodule
|
|
|
|
* Usage on CLI: sake dev/tasks/i18nTextCollectorTask
|
|
|
|
* Usage on CLI (module-specific): sake dev/tasks/i18nTextCollectorTask module=mymodule
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-16 22:42:41 +02:00
|
|
|
* @author Bernat Foj Capell <bernat@silverstripe.com>
|
2008-10-17 17:21:33 +02:00
|
|
|
* @author Ingo Schommer <FIRSTNAME@silverstripe.com>
|
2008-11-01 14:26:08 +01:00
|
|
|
* @uses i18nEntityProvider
|
|
|
|
* @uses i18n
|
2008-10-16 22:42:41 +02:00
|
|
|
*/
|
2009-11-10 22:50:27 +01:00
|
|
|
class i18nTextCollector extends Object {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
/**
|
|
|
|
* Default (master) locale
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2008-10-16 22:42:41 +02:00
|
|
|
protected $defaultLocale;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-17 17:21:33 +02:00
|
|
|
/**
|
2015-07-05 06:53:21 +02:00
|
|
|
* The directory base on which the collector should act.
|
2008-10-17 17:21:33 +02:00
|
|
|
* Usually the webroot set through {@link Director::baseFolder()}.
|
2015-07-20 11:33:55 +02:00
|
|
|
*
|
2008-10-17 17:21:33 +02:00
|
|
|
* @todo Fully support changing of basePath through {@link SSViewer} and {@link ManifestBuilder}
|
2015-07-20 11:33:55 +02:00
|
|
|
*
|
2015-07-05 06:53:21 +02:00
|
|
|
* @var string
|
2008-10-17 17:21:33 +02:00
|
|
|
*/
|
|
|
|
public $basePath;
|
2012-04-14 00:16:30 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
/**
|
|
|
|
* Save path
|
|
|
|
*
|
2015-07-20 11:33:55 +02:00
|
|
|
* @var string
|
2015-07-05 06:53:21 +02:00
|
|
|
*/
|
2012-04-14 00:16:30 +02:00
|
|
|
public $baseSavePath;
|
|
|
|
|
2008-10-17 17:21:33 +02:00
|
|
|
/**
|
2012-04-14 00:16:30 +02:00
|
|
|
* @var i18nTextCollector_Writer
|
2008-10-17 17:21:33 +02:00
|
|
|
*/
|
2012-04-14 00:16:30 +02:00
|
|
|
protected $writer;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
/**
|
|
|
|
* List of file extensions to parse
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fileExtensions = array('php', 'ss');
|
2015-07-20 11:33:55 +02:00
|
|
|
|
2008-10-16 22:42:41 +02:00
|
|
|
/**
|
|
|
|
* @param $locale
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function __construct($locale = null) {
|
2015-07-05 06:53:21 +02:00
|
|
|
$this->defaultLocale = $locale
|
|
|
|
? $locale
|
2016-08-19 00:51:35 +02:00
|
|
|
: i18n::get_lang_from_locale(i18n::config()->get('default_locale'));
|
2008-10-17 17:21:33 +02:00
|
|
|
$this->basePath = Director::baseFolder();
|
|
|
|
$this->baseSavePath = Director::baseFolder();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-16 22:42:41 +02:00
|
|
|
parent::__construct();
|
|
|
|
}
|
2012-04-14 00:16:30 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
/**
|
|
|
|
* Assign a writer
|
2015-07-20 11:33:55 +02:00
|
|
|
*
|
2015-07-05 06:53:21 +02:00
|
|
|
* @param i18nTextCollector_Writer $writer
|
|
|
|
*/
|
2012-04-14 00:16:30 +02:00
|
|
|
public function setWriter($writer) {
|
|
|
|
$this->writer = $writer;
|
|
|
|
}
|
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
/**
|
|
|
|
* Gets the currently assigned writer, or the default if none is specified.
|
2015-07-20 11:33:55 +02:00
|
|
|
*
|
2015-07-05 06:53:21 +02:00
|
|
|
* @return i18nTextCollector_Writer
|
|
|
|
*/
|
2012-04-14 00:16:30 +02:00
|
|
|
public function getWriter() {
|
2015-07-05 06:53:21 +02:00
|
|
|
if(!$this->writer) {
|
2016-08-19 00:51:35 +02:00
|
|
|
$this->setWriter(Injector::inst()->get('SilverStripe\\i18n\\i18nTextCollector_Writer'));
|
2015-07-05 06:53:21 +02:00
|
|
|
}
|
2012-04-14 00:16:30 +02:00
|
|
|
return $this->writer;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-16 22:42:41 +02:00
|
|
|
/**
|
2015-07-05 06:53:21 +02:00
|
|
|
* This is the main method to build the master string tables with the
|
|
|
|
* original strings. It will search for existent modules that use the
|
|
|
|
* i18n feature, parse the _t() calls and write the resultant files
|
|
|
|
* in the lang folder of each module.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-10-16 22:42:41 +02:00
|
|
|
* @uses DataObject->collectI18nStatics()
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-01-05 07:19:48 +01:00
|
|
|
* @param array $restrictToModules
|
2015-07-05 06:53:21 +02:00
|
|
|
* @param bool $mergeWithExisting Merge new master strings with existing
|
|
|
|
* ones already defined in language files, rather than replacing them.
|
|
|
|
* This can be useful for long-term maintenance of translations across
|
|
|
|
* releases, because it allows "translation backports" to older releases
|
|
|
|
* without removing strings these older releases still rely on.
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2013-06-02 20:17:28 +02:00
|
|
|
public function run($restrictToModules = null, $mergeWithExisting = false) {
|
|
|
|
$entitiesByModule = $this->collect($restrictToModules, $mergeWithExisting);
|
2015-07-05 06:53:21 +02:00
|
|
|
if(empty($entitiesByModule)) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-20 11:33:55 +02:00
|
|
|
|
2013-06-02 20:17:28 +02:00
|
|
|
// Write each module language file
|
2015-07-05 06:53:21 +02:00
|
|
|
foreach($entitiesByModule as $module => $entities) {
|
|
|
|
// Skip empty translations
|
|
|
|
if(empty($entities)) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-07-20 11:33:55 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
// Clean sorting prior to writing
|
|
|
|
ksort($entities);
|
|
|
|
$path = $this->baseSavePath . '/' . $module;
|
|
|
|
$this->getWriter()->write($entities, $this->defaultLocale, $path);
|
2013-06-02 20:17:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
/**
|
|
|
|
* Gets the list of modules in this installer
|
2015-07-20 11:33:55 +02:00
|
|
|
*
|
2015-07-05 06:53:21 +02:00
|
|
|
* @param string $directory Path to look in
|
|
|
|
* @return array List of modules as paths relative to base
|
|
|
|
*/
|
|
|
|
protected function getModules($directory) {
|
|
|
|
// Include self as head module
|
|
|
|
$modules = array();
|
2015-07-20 11:33:55 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
// Get all standard modules
|
|
|
|
foreach(glob($directory."/*", GLOB_ONLYDIR) as $path) {
|
|
|
|
// Check for _config
|
|
|
|
if(!is_file("$path/_config.php") && !is_dir("$path/_config")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$modules[] = basename($path);
|
|
|
|
}
|
2015-07-20 11:33:55 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
// Get all themes
|
|
|
|
foreach(glob($directory."/themes/*", GLOB_ONLYDIR) as $path) {
|
|
|
|
// Check for templates
|
|
|
|
if(is_dir("$path/templates")) {
|
|
|
|
$modules[] = 'themes/'.basename($path);
|
|
|
|
}
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
return $modules;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
/**
|
|
|
|
* Extract all strings from modules and return these grouped by module name
|
2015-07-20 11:33:55 +02:00
|
|
|
*
|
2015-07-05 06:53:21 +02:00
|
|
|
* @param array $restrictToModules
|
|
|
|
* @param bool $mergeWithExisting
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function collect($restrictToModules = array(), $mergeWithExisting = false) {
|
|
|
|
$entitiesByModule = $this->getEntitiesByModule();
|
2015-07-20 11:33:55 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
// Resolve conflicts between duplicate keys across modules
|
|
|
|
$entitiesByModule = $this->resolveDuplicateConflicts($entitiesByModule);
|
|
|
|
|
|
|
|
// Optionally merge with existing master strings
|
|
|
|
if($mergeWithExisting) {
|
|
|
|
$entitiesByModule = $this->mergeWithExisting($entitiesByModule);
|
|
|
|
}
|
2015-07-20 11:33:55 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
// Restrict modules we update to just the specified ones (if any passed)
|
|
|
|
if(!empty($restrictToModules)) {
|
|
|
|
foreach (array_diff(array_keys($entitiesByModule), $restrictToModules) as $module) {
|
|
|
|
unset($entitiesByModule[$module]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $entitiesByModule;
|
|
|
|
}
|
2015-07-20 11:33:55 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
/**
|
|
|
|
* Resolve conflicts between duplicate keys across modules
|
2015-07-20 11:33:55 +02:00
|
|
|
*
|
2015-07-05 06:53:21 +02:00
|
|
|
* @param array $entitiesByModule List of all modules with keys
|
|
|
|
* @return array Filtered listo of modules with duplicate keys unassigned
|
|
|
|
*/
|
|
|
|
protected function resolveDuplicateConflicts($entitiesByModule) {
|
|
|
|
// Find all keys that exist across multiple modules
|
|
|
|
$conflicts = $this->getConflicts($entitiesByModule);
|
|
|
|
foreach($conflicts as $conflict) {
|
|
|
|
// Determine if we can narrow down the ownership
|
|
|
|
$bestModule = $this->getBestModuleForKey($entitiesByModule, $conflict);
|
|
|
|
if(!$bestModule) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-07-20 11:33:55 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
// Remove foreign duplicates
|
|
|
|
foreach($entitiesByModule as $module => $entities) {
|
|
|
|
if($module !== $bestModule) {
|
|
|
|
unset($entitiesByModule[$module][$conflict]);
|
2010-11-18 20:00:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-05 06:53:21 +02:00
|
|
|
return $entitiesByModule;
|
|
|
|
}
|
2015-07-20 11:33:55 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
/**
|
|
|
|
* Find all keys in the entity list that are duplicated across modules
|
2015-07-20 11:33:55 +02:00
|
|
|
*
|
2015-07-05 06:53:21 +02:00
|
|
|
* @param array $entitiesByModule
|
|
|
|
* @return array List of keys
|
|
|
|
*/
|
|
|
|
protected function getConflicts($entitiesByModule) {
|
|
|
|
$modules = array_keys($entitiesByModule);
|
|
|
|
$allConflicts = array();
|
|
|
|
// bubble-compare each group of modules
|
|
|
|
for($i = 0; $i < count($modules) - 1; $i++) {
|
|
|
|
$left = array_keys($entitiesByModule[$modules[$i]]);
|
|
|
|
for($j = $i+1; $j < count($modules); $j++) {
|
|
|
|
$right = array_keys($entitiesByModule[$modules[$j]]);
|
|
|
|
$conflicts = array_intersect($left, $right);
|
|
|
|
$allConflicts = array_merge($allConflicts, $conflicts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array_unique($allConflicts);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-06-15 06:03:16 +02:00
|
|
|
/**
|
|
|
|
* Map of translation keys => module names
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $classModuleCache = [];
|
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
/**
|
|
|
|
* Determine the best module to be given ownership over this key
|
2015-07-20 11:33:55 +02:00
|
|
|
*
|
2015-07-05 06:53:21 +02:00
|
|
|
* @param array $entitiesByModule
|
|
|
|
* @param string $key
|
|
|
|
* @return string Best module, if found
|
|
|
|
*/
|
|
|
|
protected function getBestModuleForKey($entitiesByModule, $key) {
|
|
|
|
// Check classes
|
|
|
|
$class = current(explode('.', $key));
|
2016-06-15 06:03:16 +02:00
|
|
|
if(array_key_exists($class, $this->classModuleCache)) {
|
|
|
|
return $this->classModuleCache[$class];
|
|
|
|
}
|
|
|
|
$owner = $this->findModuleForClass($class);
|
2015-07-05 06:53:21 +02:00
|
|
|
if($owner) {
|
2016-06-15 06:03:16 +02:00
|
|
|
$this->classModuleCache[$class] = $owner;
|
2015-07-05 06:53:21 +02:00
|
|
|
return $owner;
|
2010-11-18 20:00:13 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
// @todo - How to determine ownership of templates? Templates can
|
|
|
|
// exist in multiple locations with the same name.
|
2008-10-17 17:21:33 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
// Display notice if not found
|
|
|
|
Debug::message(
|
2016-06-15 06:03:16 +02:00
|
|
|
"Duplicate key {$key} detected in no / multiple modules with no obvious owner",
|
2015-07-05 06:53:21 +02:00
|
|
|
false
|
|
|
|
);
|
2015-07-20 11:33:55 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
// Fall back to framework then cms modules
|
|
|
|
foreach(array('framework', 'cms') as $module) {
|
|
|
|
if(isset($entitiesByModule[$module][$key])) {
|
2016-06-15 06:03:16 +02:00
|
|
|
$this->classModuleCache[$class] = $module;
|
2015-07-05 06:53:21 +02:00
|
|
|
return $module;
|
|
|
|
}
|
2010-11-18 20:00:13 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
// Do nothing
|
2016-06-15 06:03:16 +02:00
|
|
|
$this->classModuleCache[$class] = null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a partial class name, attempt to determine the best module to assign strings to.
|
|
|
|
*
|
|
|
|
* @param string $class Either a FQN class name, or a non-qualified class name.
|
|
|
|
* @return string Name of module
|
|
|
|
*/
|
|
|
|
protected function findModuleForClass($class) {
|
|
|
|
if(ClassInfo::exists($class)) {
|
|
|
|
return i18n::get_owner_module($class);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we can't find a class, see if it needs to be fully qualified
|
|
|
|
if(strpos($class, '\\') !== false) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find FQN that ends with $class
|
|
|
|
$classes = preg_grep(
|
|
|
|
'/'.preg_quote("\\{$class}", '\/').'$/i',
|
2016-09-09 08:43:05 +02:00
|
|
|
ClassLoader::instance()->getManifest()->getClassNames()
|
2016-06-15 06:03:16 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// Find all modules for candidate classes
|
|
|
|
$modules = array_unique(array_map(function($class) {
|
|
|
|
return i18n::get_owner_module($class);
|
|
|
|
}, $classes));
|
|
|
|
|
|
|
|
if(count($modules) === 1) {
|
|
|
|
return reset($modules);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Couldn't find it! Exists in none, or multiple modules.
|
2015-07-05 06:53:21 +02:00
|
|
|
return null;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
/**
|
|
|
|
* Merge all entities with existing strings
|
2015-07-20 11:33:55 +02:00
|
|
|
*
|
2015-07-05 06:53:21 +02:00
|
|
|
* @param array $entitiesByModule
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function mergeWithExisting($entitiesByModule) {
|
|
|
|
// TODO Support all defined source formats through i18n::get_translators().
|
|
|
|
// Currently not possible because adapter instances can't be fully reset through the Zend API,
|
|
|
|
// meaning master strings accumulate across modules
|
|
|
|
foreach($entitiesByModule as $module => $entities) {
|
2016-08-19 00:51:35 +02:00
|
|
|
$adapter = Injector::inst()->create('SilverStripe\\i18n\\i18nRailsYamlAdapter');
|
2015-07-05 06:53:21 +02:00
|
|
|
$fileName = $adapter->getFilenameForLocale($this->defaultLocale);
|
|
|
|
$masterFile = "{$this->basePath}/{$module}/lang/{$fileName}";
|
|
|
|
if(!file_exists($masterFile)) {
|
|
|
|
continue;
|
|
|
|
}
|
2008-10-17 17:21:33 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
$adapter->addTranslation(array(
|
|
|
|
'content' => $masterFile,
|
|
|
|
'locale' => $this->defaultLocale
|
|
|
|
));
|
|
|
|
$entitiesByModule[$module] = array_merge(
|
|
|
|
array_map(
|
|
|
|
// Transform each master string from scalar value to array of strings
|
|
|
|
function($v) {return array($v);},
|
|
|
|
$adapter->getMessages($this->defaultLocale)
|
|
|
|
),
|
|
|
|
$entities
|
2015-07-20 11:33:55 +02:00
|
|
|
);
|
2015-07-05 06:53:21 +02:00
|
|
|
}
|
|
|
|
return $entitiesByModule;
|
|
|
|
}
|
2015-07-20 11:33:55 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
/**
|
|
|
|
* Collect all entities grouped by module
|
2015-07-20 11:33:55 +02:00
|
|
|
*
|
2015-07-05 06:53:21 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getEntitiesByModule() {
|
|
|
|
// A master string tables array (one mst per module)
|
|
|
|
$entitiesByModule = array();
|
|
|
|
$modules = $this->getModules($this->basePath);
|
2008-10-17 17:21:33 +02:00
|
|
|
foreach($modules as $module) {
|
2014-08-15 08:53:05 +02:00
|
|
|
// we store the master string tables
|
2009-01-05 07:19:48 +01:00
|
|
|
$processedEntities = $this->processModule($module);
|
|
|
|
if(isset($entitiesByModule[$module])) {
|
|
|
|
$entitiesByModule[$module] = array_merge_recursive($entitiesByModule[$module], $processedEntities);
|
|
|
|
} else {
|
|
|
|
$entitiesByModule[$module] = $processedEntities;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-01-05 07:19:48 +01:00
|
|
|
// extract all entities for "foreign" modules (fourth argument)
|
2015-07-05 06:53:21 +02:00
|
|
|
// @see CMSMenu::provideI18nEntities for an example usage
|
2009-01-05 07:19:48 +01:00
|
|
|
foreach($entitiesByModule[$module] as $fullName => $spec) {
|
2015-07-05 06:53:21 +02:00
|
|
|
if(!empty($spec[2]) && $spec[2] !== $module) {
|
2012-04-27 10:11:38 +02:00
|
|
|
$othermodule = $spec[2];
|
2015-07-05 06:53:21 +02:00
|
|
|
if(!isset($entitiesByModule[$othermodule])) {
|
|
|
|
$entitiesByModule[$othermodule] = array();
|
|
|
|
}
|
2012-04-27 10:11:38 +02:00
|
|
|
unset($spec[2]);
|
2009-01-05 07:19:48 +01:00
|
|
|
$entitiesByModule[$othermodule][$fullName] = $spec;
|
|
|
|
unset($entitiesByModule[$module][$fullName]);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
2012-07-25 01:39:39 +02:00
|
|
|
}
|
2013-06-02 20:17:28 +02:00
|
|
|
return $entitiesByModule;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function write($module, $entities) {
|
|
|
|
$this->getWriter()->write($entities, $this->defaultLocale, $this->baseSavePath . '/' . $module);
|
2014-01-17 13:17:48 +01:00
|
|
|
return $this;
|
2008-10-16 22:42:41 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-16 22:42:41 +02:00
|
|
|
/**
|
2012-05-18 02:32:12 +02:00
|
|
|
* Builds a master string table from php and .ss template files for the module passed as the $module param
|
2012-06-20 23:59:16 +02:00
|
|
|
* @see collectFromCode() and collectFromTemplate()
|
2008-10-16 22:42:41 +02:00
|
|
|
*
|
2015-07-05 06:53:21 +02:00
|
|
|
* @param string $module A module's name or just 'themes/<themename>'
|
|
|
|
* @return array An array of entities found in the files that comprise the module
|
2008-10-16 22:42:41 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
protected function processModule($module) {
|
2012-04-14 00:16:30 +02:00
|
|
|
$entities = array();
|
2008-10-16 22:42:41 +02:00
|
|
|
|
2008-10-17 17:21:33 +02:00
|
|
|
// Search for calls in code files if these exists
|
2015-07-05 06:53:21 +02:00
|
|
|
$fileList = $this->getFileListForModule($module);
|
2008-10-17 17:21:33 +02:00
|
|
|
foreach($fileList as $filePath) {
|
2015-07-05 06:53:21 +02:00
|
|
|
$extension = pathinfo($filePath, PATHINFO_EXTENSION);
|
|
|
|
$content = file_get_contents($filePath);
|
|
|
|
// Filter based on extension
|
|
|
|
if($extension === 'php') {
|
|
|
|
$entities = array_merge(
|
|
|
|
$entities,
|
|
|
|
$this->collectFromCode($content, $module),
|
|
|
|
$this->collectFromEntityProviders($filePath, $module)
|
|
|
|
);
|
|
|
|
} elseif($extension === 'ss') {
|
2008-10-17 17:21:33 +02:00
|
|
|
// templates use their filename as a namespace
|
|
|
|
$namespace = basename($filePath);
|
2015-07-05 06:53:21 +02:00
|
|
|
$entities = array_merge(
|
|
|
|
$entities,
|
|
|
|
$this->collectFromTemplate($content, $module, $namespace)
|
|
|
|
);
|
2008-10-16 22:42:41 +02:00
|
|
|
}
|
2008-10-17 17:21:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// sort for easier lookup and comparison with translated files
|
2012-04-14 00:16:30 +02:00
|
|
|
ksort($entities);
|
2008-10-17 17:21:33 +02:00
|
|
|
|
2012-04-14 00:16:30 +02:00
|
|
|
return $entities;
|
2008-10-16 22:42:41 +02:00
|
|
|
}
|
2012-04-16 07:24:48 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the list of files for this module
|
2015-07-20 11:33:55 +02:00
|
|
|
*
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param string $module
|
2015-07-05 06:53:21 +02:00
|
|
|
* @return array List of files to parse
|
|
|
|
*/
|
|
|
|
protected function getFileListForModule($module) {
|
|
|
|
$modulePath = "{$this->basePath}/{$module}";
|
2015-07-20 11:33:55 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
// Search all .ss files in themes
|
|
|
|
if(stripos($module, 'themes/') === 0) {
|
|
|
|
return $this->getFilesRecursive($modulePath, null, 'ss');
|
|
|
|
}
|
2015-07-20 11:33:55 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
// If Framework or non-standard module structure, so we'll scan all subfolders
|
|
|
|
if($module === FRAMEWORK_DIR || !is_dir("{$modulePath}/code")) {
|
|
|
|
return $this->getFilesRecursive($modulePath);
|
|
|
|
}
|
2015-07-20 11:33:55 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
// Get code files
|
|
|
|
$files = $this->getFilesRecursive("{$modulePath}/code", null, 'php');
|
|
|
|
|
|
|
|
// Search for templates in this module
|
|
|
|
if(is_dir("{$modulePath}/templates")) {
|
|
|
|
$templateFiles = $this->getFilesRecursive("{$modulePath}/templates", null, 'ss');
|
|
|
|
} else {
|
|
|
|
$templateFiles = $this->getFilesRecursive($modulePath, null, 'ss');
|
|
|
|
}
|
2015-07-20 11:33:55 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
return array_merge($files, $templateFiles);
|
|
|
|
}
|
2012-04-16 07:24:48 +02:00
|
|
|
|
2012-05-18 02:32:12 +02:00
|
|
|
/**
|
|
|
|
* Extracts translatables from .php files.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-18 02:32:12 +02:00
|
|
|
* @param string $content The text content of a parsed template-file
|
2015-07-05 06:53:21 +02:00
|
|
|
* @param string $module Module's name or 'themes'. Could also be a namespace
|
|
|
|
* Generated by templates includes. E.g. 'UploadField.ss'
|
2012-05-18 02:32:12 +02:00
|
|
|
* @return array $entities An array of entities representing the extracted translation function calls in code
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2012-04-18 07:23:57 +02:00
|
|
|
public function collectFromCode($content, $module) {
|
|
|
|
$entities = array();
|
2012-04-16 07:24:48 +02:00
|
|
|
|
2012-04-18 07:23:57 +02:00
|
|
|
$tokens = token_get_all("<?php\n" . $content);
|
|
|
|
$inTransFn = false;
|
|
|
|
$inConcat = false;
|
|
|
|
$finalTokenDueToArray = false;
|
|
|
|
$currentEntity = array();
|
|
|
|
foreach($tokens as $token) {
|
|
|
|
if(is_array($token)) {
|
|
|
|
list($id, $text) = $token;
|
|
|
|
|
|
|
|
if($inTransFn && $id == T_ARRAY) {
|
|
|
|
//raw 'array' token found in _t function, stop processing the tokens for this _t now
|
|
|
|
$finalTokenDueToArray = true;
|
2012-04-16 07:24:48 +02:00
|
|
|
}
|
|
|
|
|
2012-04-18 07:23:57 +02:00
|
|
|
if($id == T_STRING && $text == '_t') {
|
|
|
|
// start definition
|
|
|
|
$inTransFn = true;
|
|
|
|
} elseif($inTransFn && $id == T_VARIABLE) {
|
|
|
|
// Dynamic definition from provideEntities - skip
|
|
|
|
$inTransFn = false;
|
|
|
|
$inConcat = false;
|
|
|
|
$currentEntity = array();
|
|
|
|
} elseif($inTransFn && $id == T_CONSTANT_ENCAPSED_STRING) {
|
|
|
|
// Fixed quoting escapes, and remove leading/trailing quotes
|
|
|
|
if(preg_match('/^\'/', $text)) {
|
|
|
|
$text = str_replace("\'", "'", $text);
|
|
|
|
$text = preg_replace('/^\'/', '', $text);
|
|
|
|
$text = preg_replace('/\'$/', '', $text);
|
|
|
|
} else {
|
|
|
|
$text = str_replace('\"', '"', $text);
|
|
|
|
$text = preg_replace('/^"/', '', $text);
|
|
|
|
$text = preg_replace('/"$/', '', $text);
|
|
|
|
}
|
2012-04-16 07:24:48 +02:00
|
|
|
|
2012-04-18 07:23:57 +02:00
|
|
|
if($inConcat) {
|
|
|
|
$currentEntity[count($currentEntity)-1] .= $text;
|
|
|
|
} else {
|
|
|
|
$currentEntity[] = $text;
|
|
|
|
}
|
2012-04-16 07:24:48 +02:00
|
|
|
}
|
2012-04-18 07:23:57 +02:00
|
|
|
} elseif($inTransFn && $token == '.') {
|
|
|
|
$inConcat = true;
|
|
|
|
} elseif($inTransFn && $token == ',') {
|
|
|
|
$inConcat = false;
|
2013-05-05 02:19:31 +02:00
|
|
|
} elseif($inTransFn && ($token == ')' || $finalTokenDueToArray || $token == '[')) {
|
2012-04-18 07:23:57 +02:00
|
|
|
// finalize definition
|
|
|
|
$inTransFn = false;
|
|
|
|
$inConcat = false;
|
|
|
|
$entity = array_shift($currentEntity);
|
|
|
|
$entities[$entity] = $currentEntity;
|
|
|
|
$currentEntity = array();
|
|
|
|
$finalTokenDueToArray = false;
|
2012-04-14 00:16:30 +02:00
|
|
|
}
|
2008-10-16 22:42:41 +02:00
|
|
|
}
|
2012-04-16 07:24:48 +02:00
|
|
|
|
2012-04-18 07:23:57 +02:00
|
|
|
foreach($entities as $entity => $spec) {
|
|
|
|
// call without master language definition
|
|
|
|
if(!$spec) {
|
|
|
|
unset($entities[$entity]);
|
|
|
|
continue;
|
2012-04-16 07:24:48 +02:00
|
|
|
}
|
|
|
|
|
2012-04-18 07:23:57 +02:00
|
|
|
unset($entities[$entity]);
|
|
|
|
$entities[$this->normalizeEntity($entity, $module)] = $spec;
|
2012-04-16 07:24:48 +02:00
|
|
|
}
|
2012-04-18 07:23:57 +02:00
|
|
|
ksort($entities);
|
2012-04-16 07:24:48 +02:00
|
|
|
|
2012-04-18 07:23:57 +02:00
|
|
|
return $entities;
|
2008-10-16 22:42:41 +02:00
|
|
|
}
|
|
|
|
|
2012-05-18 02:32:12 +02:00
|
|
|
/**
|
|
|
|
* Extracts translatables from .ss templates (Self referencing)
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-18 02:32:12 +02:00
|
|
|
* @param string $content The text content of a parsed template-file
|
|
|
|
* @param string $fileName The name of a template file when method is used in self-referencing mode
|
2016-08-19 00:51:35 +02:00
|
|
|
* @param string $module Module's name or 'themes'
|
|
|
|
* @param array $parsedFiles
|
2012-05-18 02:32:12 +02:00
|
|
|
* @return array $entities An array of entities representing the extracted template function calls
|
2014-08-15 08:53:05 +02:00
|
|
|
*/
|
2012-12-20 12:46:35 +01:00
|
|
|
public function collectFromTemplate($content, $fileName, $module, &$parsedFiles = array()) {
|
2012-04-16 07:24:48 +02:00
|
|
|
// use parser to extract <%t style translatable entities
|
2015-07-05 06:53:21 +02:00
|
|
|
$entities = i18nTextCollector_Parser::GetTranslatables($content);
|
2012-04-16 07:24:48 +02:00
|
|
|
|
|
|
|
// use the old method of getting _t() style translatable entities
|
2012-04-14 00:16:30 +02:00
|
|
|
// Collect in actual template
|
2012-07-18 14:58:53 +02:00
|
|
|
if(preg_match_all('/(_t\([^\)]*?\))/ms', $content, $matches)) {
|
|
|
|
foreach($matches[1] as $match) {
|
|
|
|
$entities = array_merge($entities, $this->collectFromCode($match, $module));
|
2012-04-14 00:16:30 +02:00
|
|
|
}
|
|
|
|
}
|
2012-02-27 22:14:02 +01:00
|
|
|
|
2012-04-14 00:16:30 +02:00
|
|
|
foreach($entities as $entity => $spec) {
|
|
|
|
unset($entities[$entity]);
|
|
|
|
$entities[$this->normalizeEntity($entity, $module)] = $spec;
|
2008-10-17 17:21:33 +02:00
|
|
|
}
|
2012-04-14 00:16:30 +02:00
|
|
|
ksort($entities);
|
2012-05-03 17:45:25 +02:00
|
|
|
|
2012-04-14 00:16:30 +02:00
|
|
|
return $entities;
|
2008-10-17 17:21:33 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-02 00:16:45 +01:00
|
|
|
/**
|
2015-07-05 06:53:21 +02:00
|
|
|
* Allows classes which implement i18nEntityProvider to provide
|
|
|
|
* additional translation strings.
|
2015-07-20 11:33:55 +02:00
|
|
|
*
|
2015-07-05 06:53:21 +02:00
|
|
|
* Not all classes can be instanciated without mandatory arguments,
|
|
|
|
* so entity collection doesn't work for all SilverStripe classes currently
|
2015-07-20 11:33:55 +02:00
|
|
|
*
|
2008-11-02 00:16:45 +01:00
|
|
|
* @uses i18nEntityProvider
|
2015-07-05 06:53:21 +02:00
|
|
|
* @param string $filePath
|
|
|
|
* @param string $module
|
|
|
|
* @return array
|
2008-11-02 00:16:45 +01:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function collectFromEntityProviders($filePath, $module = null) {
|
2012-04-14 00:16:30 +02:00
|
|
|
$entities = array();
|
2008-11-01 19:56:39 +01:00
|
|
|
$classes = ClassInfo::classes_for_file($filePath);
|
2015-07-05 06:53:21 +02:00
|
|
|
foreach($classes as $class) {
|
|
|
|
// Skip non-implementing classes
|
2016-08-19 00:51:35 +02:00
|
|
|
if(!class_exists($class) || !is_a($class, 'SilverStripe\\i18n\\i18nEntityProvider', true)) {
|
2015-07-05 06:53:21 +02:00
|
|
|
continue;
|
2008-11-01 19:56:39 +01:00
|
|
|
}
|
2015-07-05 06:53:21 +02:00
|
|
|
|
|
|
|
// Skip abstract classes
|
|
|
|
$reflectionClass = new ReflectionClass($class);
|
|
|
|
if($reflectionClass->isAbstract()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$obj = singleton($class);
|
|
|
|
$entities = array_merge($entities, (array)$obj->provideI18nEntities());
|
2008-11-01 19:56:39 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-14 00:16:30 +02:00
|
|
|
ksort($entities);
|
|
|
|
return $entities;
|
2008-11-01 19:56:39 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-17 17:21:33 +02:00
|
|
|
/**
|
2012-05-18 02:32:12 +02:00
|
|
|
* Normalizes enitities with namespaces.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-05-18 02:32:12 +02:00
|
|
|
* @param string $fullName
|
2014-08-15 08:53:05 +02:00
|
|
|
* @param string $_namespace
|
2012-05-18 02:32:12 +02:00
|
|
|
* @return string|boolean FALSE
|
2008-10-17 17:21:33 +02:00
|
|
|
*/
|
2012-04-14 00:16:30 +02:00
|
|
|
protected function normalizeEntity($fullName, $_namespace = null) {
|
2008-10-17 17:21:33 +02:00
|
|
|
// split fullname into entity parts
|
|
|
|
$entityParts = explode('.', $fullName);
|
|
|
|
if(count($entityParts) > 1) {
|
|
|
|
// templates don't have a custom namespace
|
2008-10-16 22:42:41 +02:00
|
|
|
$entity = array_pop($entityParts);
|
2008-10-17 17:21:33 +02:00
|
|
|
// namespace might contain dots, so we explode
|
2014-08-15 08:53:05 +02:00
|
|
|
$namespace = implode('.',$entityParts);
|
2008-10-17 17:21:33 +02:00
|
|
|
} else {
|
|
|
|
$entity = array_pop($entityParts);
|
|
|
|
$namespace = $_namespace;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-29 22:07:17 +01:00
|
|
|
// If a dollar sign is used in the entity name,
|
|
|
|
// we can't resolve without running the method,
|
|
|
|
// and skip the processing. This is mostly used for
|
|
|
|
// dynamically translating static properties, e.g. looping
|
|
|
|
// through $db, which are detected by {@link collectFromEntityProviders}.
|
2012-04-15 21:49:51 +02:00
|
|
|
if($entity && strpos('$', $entity) !== FALSE) return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-04-14 00:16:30 +02:00
|
|
|
return "{$namespace}.{$entity}";
|
2008-10-17 17:21:33 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-11-01 19:56:39 +01:00
|
|
|
/**
|
2012-05-18 02:32:12 +02:00
|
|
|
* Helper function that searches for potential files (templates and code) to be parsed
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-11-01 19:56:39 +01:00
|
|
|
* @param string $folder base directory to scan (will scan recursively)
|
2012-05-18 02:32:12 +02:00
|
|
|
* @param array $fileList Array to which potential files will be appended
|
2015-07-05 06:53:21 +02:00
|
|
|
* @param string $type Optional, "php" or "ss" only
|
|
|
|
* @param string $folderExclude Regular expression matching folder names to exclude
|
2012-05-18 02:32:12 +02:00
|
|
|
* @return array $fileList An array of files
|
2008-11-01 19:56:39 +01:00
|
|
|
*/
|
2015-07-05 06:53:21 +02:00
|
|
|
protected function getFilesRecursive($folder, $fileList = array(), $type = null, $folderExclude = '/\/(tests)$/') {
|
|
|
|
if(!$fileList) {
|
|
|
|
$fileList = array();
|
|
|
|
}
|
|
|
|
// Skip ignored folders
|
|
|
|
if(is_file("{$folder}/_manifest_exclude") || preg_match($folderExclude, $folder)) {
|
|
|
|
return $fileList;
|
|
|
|
}
|
2008-11-01 19:56:39 +01:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
foreach(glob($folder.'/*') as $path) {
|
|
|
|
// Recurse if directory
|
|
|
|
if(is_dir($path)) {
|
2012-10-05 16:29:40 +02:00
|
|
|
$fileList = array_merge(
|
2014-08-15 08:53:05 +02:00
|
|
|
$fileList,
|
2015-07-05 06:53:21 +02:00
|
|
|
$this->getFilesRecursive($path, $fileList, $type, $folderExclude)
|
2012-10-05 16:29:40 +02:00
|
|
|
);
|
2015-07-05 06:53:21 +02:00
|
|
|
continue;
|
|
|
|
}
|
2015-07-20 11:33:55 +02:00
|
|
|
|
2015-07-05 06:53:21 +02:00
|
|
|
// Check if this extension is included
|
|
|
|
$extension = pathinfo($path, PATHINFO_EXTENSION);
|
|
|
|
if(in_array($extension, $this->fileExtensions)
|
|
|
|
&& (!$type || $type === $extension)
|
|
|
|
) {
|
|
|
|
$fileList[$path] = $path;
|
2012-05-03 17:45:25 +02:00
|
|
|
}
|
2008-11-01 19:56:39 +01:00
|
|
|
}
|
|
|
|
return $fileList;
|
2008-10-17 17:21:33 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-17 17:21:33 +02:00
|
|
|
public function getDefaultLocale() {
|
|
|
|
return $this->defaultLocale;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-10-17 17:21:33 +02:00
|
|
|
public function setDefaultLocale($locale) {
|
|
|
|
$this->defaultLocale = $locale;
|
2008-10-16 22:42:41 +02:00
|
|
|
}
|
|
|
|
}
|