i18nTextCollector
class i18nTextCollector (View source)
SilverStripe-variant of the "gettext" tool: Parses the string content of all PHP-files and SilverStripe templates for occurrences of the _t() translation method. Also uses the i18nEntityProvider interface to get dynamically defined entities by executing the provideI18nEntities() method on all implementors of this interface.
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, e.g. framework/lang/en.yml.
The collector needs to be run whenever you make new translatable entities available. Please don't alter the arrays in language tables manually.
Usage through URL: http://localhost/dev/tasks/i18nTextCollectorTask 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
Traits
A class that can be instantiated or replaced via DI
Constants
private THEME_PREFIX |
|
Properties
protected | string | $defaultLocale | Default (master) locale |
|
protected | bool | $warnOnEmptyDefault | Trigger if warnings should be shown if default is omitted |
|
public | string | $basePath | The directory base on which the collector should act. |
|
public | string | $baseSavePath | Save path |
|
protected | Writer | $writer | ||
protected | Reader | $reader | Translation reader |
|
protected | array | $fileExtensions | List of file extensions to parse |
|
protected | array | $classModuleCache | Map of translation keys => module names |
Methods
An implementation of the factory method, allows you to create an instance of a class
Creates a class instance by the "singleton" design pattern.
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.
Extract all strings from modules and return these grouped by module name
Resolve conflicts between duplicate keys across modules
Find all keys in the entity list that are duplicated across modules
Determine the best module to be given ownership over this key
Given a partial class name, attempt to determine the best module to assign strings to.
Builds a master string table from php and .ss template files for the module passed as the $module param
Extracts translatables from .php files.
Extracts translatables from .ss templates (Self referencing)
Allows classes which implement i18nEntityProvider to provide additional translation strings.
Normalizes entities with namespaces.
Helper function that searches for potential files (templates and code) to be parsed
Details
static Injectable
create(mixed ...$args)
An implementation of the factory method, allows you to create an instance of a class
This method will defer class substitution to the Injector API, which can be customised via the Config API to declare substitution classes.
This can be called in one of two ways - either calling via the class directly, or calling on Object and passing the class name as the first parameter. The following are equivalent: $list = DataList::create(SiteTree::class); $list = SiteTree::get();
static Injectable
singleton(string $class = null)
Creates a class instance by the "singleton" design pattern.
It will always return the same instance for this class, which can be used for performance reasons and as a simple way to access instance methods which don't rely on instance data (e.g. the custom SilverStripe static handling).
__construct($locale = null)
No description
$this
setWriter(Writer $writer)
Assign a writer
Writer
getWriter()
Gets the currently assigned writer, or the default if none is specified.
Reader
getReader()
Get reader
$this
setReader(Reader $reader)
Set reader
run(array $restrictToModules = null, bool $mergeWithExisting = false)
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.
array
collect(array $restrictToModules = [], bool $mergeWithExisting = false)
Extract all strings from modules and return these grouped by module name
protected array
resolveDuplicateConflicts(array $entitiesByModule)
Resolve conflicts between duplicate keys across modules
protected array
getConflicts(array $entitiesByModule)
Find all keys in the entity list that are duplicated across modules
protected string
getBestModuleForKey(array $entitiesByModule, string $key)
Determine the best module to be given ownership over this key
protected string
findModuleForClass(string $class)
Given a partial class name, attempt to determine the best module to assign strings to.
protected array
mergeWithExisting(array $entitiesByModule)
Merge all entities with existing strings
protected array
getEntitiesByModule()
Collect all entities grouped by module
$this
write(Module $module, array $entities)
Write entities to a module
protected array
processModule(Module $module)
Builds a master string table from php and .ss template files for the module passed as the $module param
protected array
getFileListForModule(Module $module)
Retrieves the list of files for this module
array
collectFromCode(string $content, string $fileName, Module $module)
Extracts translatables from .php files.
Note: Translations without default values are omitted.
array
collectFromTemplate(string $content, string $fileName, Module $module, array $parsedFiles = [])
Extracts translatables from .ss templates (Self referencing)
array
collectFromEntityProviders(string $filePath, Module $module = null)
Allows classes which implement i18nEntityProvider to provide additional translation strings.
Not all classes can be instantiated without mandatory arguments, so entity collection doesn't work for all SilverStripe classes currently
array
collectFromORM(string $filePath)
Extracts translations for ORM fields
protected string|bool
normalizeEntity(string $fullName, string $_namespace = null)
Normalizes entities with namespaces.
protected array
getFilesRecursive(string $folder, array $fileList = [], string $type = null, string $folderExclude = '/\\/(tests)$/')
Helper function that searches for potential files (templates and code) to be parsed
getDefaultLocale()
No description
setDefaultLocale($locale)
No description
bool
getWarnOnEmptyDefault()
No description
$this
setWarnOnEmptyDefault(bool $warnOnEmptyDefault)
No description