| 1 | <?php |
||
| 2 | |||
| 3 | namespace LeKoala\Multilingual; |
||
| 4 | |||
| 5 | use SilverStripe\i18n\i18n; |
||
| 6 | use SilverStripe\Dev\BuildTask; |
||
| 7 | use SilverStripe\Control\Director; |
||
| 8 | use SilverStripe\Control\Middleware\HTTPCacheControlMiddleware; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * A better task for collecting text |
||
| 12 | * |
||
| 13 | * Use our TextCollector under the hood that actually works... |
||
| 14 | */ |
||
| 15 | class ConfigurableI18nTextCollectorTask extends BuildTask |
||
| 16 | { |
||
| 17 | use BuildTaskTools; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private static $segment = 'ConfigurableI18nTextCollectorTask'; |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $title = "Configurable i18n Textcollector Task"; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $description = " |
||
| 33 | Traverses through files in order to collect the 'entity master tables' |
||
| 34 | stored in each module. Provides the ability to choose modules and clear/merge translations. |
||
| 35 | "; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * This is the main method to build the master string tables with the original strings. |
||
| 39 | * It will search for existent modules that use the i18n feature, parse the _t() calls |
||
| 40 | * and write the resultant files in the lang folder of each module. |
||
| 41 | * |
||
| 42 | * @uses DataObject->collectI18nStatics() |
||
| 43 | * @param \SilverStripe\Control\HTTPRequest $request |
||
| 44 | * @return void |
||
| 45 | */ |
||
| 46 | public function run($request) |
||
| 47 | { |
||
| 48 | HTTPCacheControlMiddleware::singleton()->disableCache(true); |
||
| 49 | |||
| 50 | $this->request = $request; |
||
| 51 | $this->increaseTimeLimitTo(); |
||
| 52 | |||
| 53 | $modules = $this->getModulesAndThemes(); |
||
| 54 | |||
| 55 | $this->addOption("locale", "Locale to use", LangHelper::get_lang()); |
||
| 56 | $this->addOption("merge", "Merge with previous translations", true); |
||
| 57 | $this->addOption("auto_translate", "Translate strings using helper", false); |
||
| 58 | $this->addOption("auto_translate_lang", "Base language for translation", LangHelper::get_lang()); |
||
| 59 | $this->addOption("auto_translate_mode", "Translate new strings or all", ''); |
||
| 60 | $this->addOption("clear_unused", "Remove keys that are not used anymore", false); |
||
| 61 | $this->addOption("debug", "Show debug messages and prevent write", false); |
||
| 62 | $this->addOption("module", "Module", 'default', $modules); |
||
| 63 | |||
| 64 | $options = $this->askOptions(); |
||
| 65 | |||
| 66 | $locale = $options['locale']; |
||
| 67 | $merge = $options['merge']; |
||
| 68 | $module = $options['module']; |
||
| 69 | $clearUnused = $options['clear_unused']; |
||
| 70 | $debug = $options['debug']; |
||
| 71 | $auto_translate = $options['auto_translate']; |
||
| 72 | $auto_translate_lang = $options['auto_translate_lang']; |
||
| 73 | $auto_translate_mode = $options['auto_translate_mode']; |
||
| 74 | |||
| 75 | $themes = Director::baseFolder() . '/themes'; |
||
| 76 | $folders = glob($themes . '/*'); |
||
| 77 | $toCollect = ['app']; |
||
| 78 | foreach ($folders as $f) { |
||
| 79 | $toCollect[] = 'themes:' . basename($f); |
||
| 80 | } |
||
| 81 | if ($module && $module != 'default') { |
||
| 82 | $toCollect = [$module]; |
||
| 83 | } |
||
| 84 | if ($locale) { |
||
| 85 | foreach ($toCollect as $module) { |
||
| 86 | $this->message("Proceeding with locale $locale for module $module"); |
||
| 87 | $collector = MultilingualTextCollector::create($locale); |
||
| 88 | $collector->setMergeWithExisting($merge); |
||
| 89 | $collector->setClearUnused($clearUnused); |
||
| 90 | $collector->setDebug($debug); |
||
| 91 | $collector->setAutoTranslate($auto_translate, $auto_translate_lang, $auto_translate_mode); |
||
| 92 | $result = $collector->run([$module], $merge); |
||
| 93 | if ($result) { |
||
| 94 | foreach ($result as $module => $entities) { |
||
| 95 | $this->message("Collected " . count($entities) . " messages for module $module"); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } |
||
| 101 | } |
||
| 102 |