2008-11-01 13:26:08 +00:00
|
|
|
<?php
|
2016-08-19 10:51:35 +12:00
|
|
|
|
|
|
|
namespace SilverStripe\i18n;
|
|
|
|
|
2017-01-18 16:58:48 +13:00
|
|
|
use SilverStripe\i18n\TextCollection\i18nTextCollector;
|
|
|
|
|
2008-11-01 13:26:08 +00:00
|
|
|
/**
|
|
|
|
* Dynamically provide translatable entites for the {@link i18n} logic.
|
|
|
|
* This is particularly handy for natural language strings in static variables
|
|
|
|
* of a class definition, as the _t() method can only be used in a runtime/instance
|
|
|
|
* context. The provideI18nEntities() method enables you to define your own entities
|
|
|
|
* with your custom naming, mostly involving either the variable name or the array
|
|
|
|
* key. With this in place, you can use a getter method to trigger translation
|
|
|
|
* of your values.
|
|
|
|
* For any statics containing natural language, never use the static directly -
|
|
|
|
* always wrap it in a getter.
|
2015-10-06 18:31:58 +13:00
|
|
|
*
|
2015-07-05 16:53:21 +12:00
|
|
|
* Classes must be able to be constructed without mandatory arguments, otherwise
|
|
|
|
* this interface will have no effect.
|
2015-10-06 18:31:58 +13:00
|
|
|
*
|
2008-11-01 23:16:45 +00:00
|
|
|
* @uses i18nTextCollector->collectFromEntityProviders()
|
2008-11-01 13:26:08 +00:00
|
|
|
*/
|
2016-11-29 12:31:16 +13:00
|
|
|
interface i18nEntityProvider
|
|
|
|
{
|
2015-10-06 18:31:58 +13:00
|
|
|
|
2016-11-29 12:31:16 +13:00
|
|
|
/**
|
2017-01-18 16:58:48 +13:00
|
|
|
* Returns the list of provided translations for this object.
|
2016-11-29 12:31:16 +13:00
|
|
|
*
|
2017-01-18 16:58:48 +13:00
|
|
|
* Note: Pluralised forms are always returned in array format.
|
2016-11-29 12:31:16 +13:00
|
|
|
*
|
2017-01-18 16:58:48 +13:00
|
|
|
* Example usage:
|
|
|
|
* <code>
|
|
|
|
* class MyTestClass implements i18nEntityProvider
|
|
|
|
* {
|
|
|
|
* public function provideI18nEntities()
|
|
|
|
* {
|
|
|
|
* $entities = [];
|
2017-08-23 09:42:10 +12:00
|
|
|
* foreach($this->config()->get('my_static_array) as $key => $value) {
|
2017-01-18 16:58:48 +13:00
|
|
|
* $entities["MyTestClass.my_static_array_{$key}"] = $value;
|
|
|
|
* }
|
|
|
|
* $entities["MyTestClass.PLURALS"] = [
|
|
|
|
* 'one' => 'A test class',
|
|
|
|
* 'other' => '{count} test classes',
|
|
|
|
* ]
|
|
|
|
* return $entities;
|
|
|
|
* }
|
2016-11-29 12:31:16 +13:00
|
|
|
* }
|
|
|
|
* </code>
|
|
|
|
*
|
|
|
|
* Example usage in {@link DataObject->provideI18nEntities()}.
|
|
|
|
*
|
2017-01-18 16:58:48 +13:00
|
|
|
* You can ask textcollector to add the provided entity to a different module.
|
|
|
|
* Simply wrap the returned value for any item in an array with the format:
|
|
|
|
* [ 'default' => $defaultValue, 'module' => $module ]
|
2016-11-29 12:31:16 +13:00
|
|
|
*
|
2017-01-18 16:58:48 +13:00
|
|
|
* <code>
|
|
|
|
* class MyTestClass implements i18nEntityProvider
|
|
|
|
* {
|
|
|
|
* public function provideI18nEntities()
|
|
|
|
* {
|
|
|
|
* $entities = [
|
|
|
|
* 'MyOtherModuleClass.MYENTITY' => [
|
|
|
|
* 'default' => $value,
|
|
|
|
* 'module' => 'myothermodule',
|
|
|
|
* ]
|
|
|
|
* ];
|
|
|
|
* }
|
|
|
|
* return $entities;
|
2016-11-29 12:31:16 +13:00
|
|
|
* }
|
|
|
|
* </code>
|
|
|
|
*
|
2017-01-18 16:58:48 +13:00
|
|
|
* @return array Map of keys to default values, which are strings in the default case,
|
|
|
|
* and array-form for pluralisations.
|
2016-11-29 12:31:16 +13:00
|
|
|
*/
|
|
|
|
public function provideI18nEntities();
|
2008-11-01 13:26:08 +00:00
|
|
|
}
|