class i18nTextCollector (View source)

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} interface to get dynamically defined entities by executing the {@link 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

Properties

string $basePath

The directory base on which the collector should act.

string $baseSavePath

Save path

Methods

static Injectable
create(array ...$args)

An implementation of the factory method, allows you to create an instance of a class

static Injectable
singleton(string $class = null)

Creates a class instance by the "singleton" design pattern.

__construct($locale = null)

No description

$this
setWriter(Writer $writer)

Assign a writer

getWriter()

Gets the currently assigned writer, or the default if none is specified.

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 = array(), bool $mergeWithExisting = false)

Extract all strings from modules and return these grouped by module name

$this
write(Module $module, array $entities)

Write entities to a module

array
collectFromCode(string $content, string $fileName, Module $module)

Extracts translatables from .php files.

array
collectFromTemplate(string $content, string $fileName, Module $module, array $parsedFiles = array())

Extracts translatables from .ss templates (Self referencing)

array
collectFromEntityProviders(string $filePath, Module $module = null)

Allows classes which implement i18nEntityProvider to provide additional translation strings.

getDefaultLocale()

No description

setDefaultLocale($locale)

No description

bool
getWarnOnEmptyDefault()

No description

$this
setWarnOnEmptyDefault(bool $warnOnEmptyDefault)

No description

Details

static Injectable create(array ...$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'); $list = SiteTree::get();

Parameters

array ...$args

Return Value

Injectable

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).

Parameters

string $class

Optional classname to create, if the called class should not be used

Return Value

Injectable

The singleton instance

__construct($locale = null)

Parameters

$locale

$this setWriter(Writer $writer)

Assign a writer

Parameters

Writer $writer

Return Value

$this

Writer getWriter()

Gets the currently assigned writer, or the default if none is specified.

Return Value

Writer

Reader getReader()

Get reader

Return Value

Reader

$this setReader(Reader $reader)

Set reader

Parameters

Reader $reader

Return Value

$this

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.

Parameters

array $restrictToModules
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.

array collect(array $restrictToModules = array(), bool $mergeWithExisting = false)

Extract all strings from modules and return these grouped by module name

Parameters

array $restrictToModules
bool $mergeWithExisting

Return Value

array

$this write(Module $module, array $entities)

Write entities to a module

Parameters

Module $module
array $entities

Return Value

$this

array collectFromCode(string $content, string $fileName, Module $module)

Extracts translatables from .php files.

Note: Translations without default values are omitted.

Parameters

string $content

The text content of a parsed template-file

string $fileName

Filename Optional filename

Module $module

Module being collected

Return Value

array

Map of localised keys to default values provided for this code

array collectFromTemplate(string $content, string $fileName, Module $module, array $parsedFiles = array())

Extracts translatables from .ss templates (Self referencing)

Parameters

string $content

The text content of a parsed template-file

string $fileName

The name of a template file when method is used in self-referencing mode

Module $module

Module being collected

array $parsedFiles

Return Value

array

$entities An array of entities representing the extracted template function calls

array collectFromEntityProviders(string $filePath, Module $module = null)

Allows classes which implement i18nEntityProvider to provide additional translation strings.

Not all classes can be instanciated without mandatory arguments, so entity collection doesn't work for all SilverStripe classes currently

Parameters

string $filePath
Module $module

Return Value

array

getDefaultLocale()

setDefaultLocale($locale)

Parameters

$locale

bool getWarnOnEmptyDefault()

Return Value

bool

$this setWarnOnEmptyDefault(bool $warnOnEmptyDefault)

Parameters

bool $warnOnEmptyDefault

Return Value

$this