Permalink
Browse files

API CHANGE Removed $priority arguments from _t(), use module prioriti…

…es instead.

ENHANCEMENT Refactored i18nTextCollector collection logic alongside $priority removal, from regex to (slightly more maintainable) PHP tokenizer. Using var_export() for generating PHP, which auto-escapes strings more robustly.
ENHANCEMENT Refactored i18nTextCollector into pluggable writers (in preparation of new YML output format)
  • Loading branch information...
1 parent d44f6b3 commit fca2c205b71e6bf304d1e085df7485a8dd2c8453 @chillu chillu committed Apr 13, 2012
Showing with 276 additions and 299 deletions.
  1. +15 −47 docs/en/topics/i18n.md
  2. +8 −5 i18n/i18n.php
  3. +179 −169 i18n/i18nTextCollector.php
  4. +74 −78 tests/i18n/i18nTextCollectorTest.php
@@ -118,7 +118,7 @@ The field tries to translate the date formats and locales into a format compatib
$field->setConfig('jslocale', 'de'); // jQuery UI only has a generic German localization
$field->setConfig('dateformat', 'dd. MMMM YYYY'); // will be transformed to 'dd. MM yy' for jQuery
-## Adapting modules for i18n
+## Translating text
Adapting a module to make it localizable is easy with SilverStripe. You just need to avoid hardcoding strings that are
language-dependent and use a translator function call instead.
@@ -130,59 +130,24 @@ language-dependent and use a translator function call instead.
echo _t("Namespace.Entity","This is a string");
-All strings passed through the _t() function will be collected in a separate language table (see "Collecting entities"
+All strings passed through the `_t()` function will be collected in a separate language table (see "Collecting entities"
below), which is the starting point for translations.
### The _t() function
-Here is the function prototype of this translator function
+The `_t()` function is the main gateway to localized text, and takes four parameters, all but the first being optional.
- :::php
- public function _t(string $entity [, string $string [, int $priority [, string $context]]]) {
-
-
-**$entity:** The first parameter is the identifier, and is composed by a namespace and an entity name, with a dot separating them.
-The main class name (i.e. the same one that the php name file) should usually be used as the namespace. This means that
-if we are coding in the file LeftAndMain.php, the namespace should be 'LeftAndMain', and therefore the complete first
-parameter would be 'LeftAndMain.ENTITY'. There is an exception to this rule. If you are using the same exactly string in two different files, for example in
-A.php and B.php, and the string in B.php will always be the same string that in A.php, then you can 'declare' this
-string in A.php with `_t('A.ENTITY','String that is used in A and B');`{php} and then in B.php simply write:
-`_t('A.ENTITY');`{php} In this way if somewhere in the future you need to modify this string, you just need to edit it
-in one file (A.php). Translators will also have to translate this string just once. Entity names are by convention written in uppercase. They have to be unique within their namespace, and its purpose is
-to serve as an identificator to this string, together with the namespace. Having an unique identificator for each string
-allows some features like change tracking. Therefore, a meaningful name is always welcomed, although not required. And
-also, that's why you shouldn't change an existing entity name in the code, unless you have a good reason to do it.
-
-**$string:** The second parameter is the string itself. It's not mandatory if you have set this same string in another place before
-(using the same class and entity). So you could write `_t('ClassName.HELLO',"Hello")` and later `_t('ClassName.HELLO')`.
-In fact, if you write the string in this second case, a warning will be issued when text-collecting to alert that you
-are redeclaring an entity.
-
-**$priority:** Priority parameter is an optional parameter and it can be used to set a translation priority. If a string is widely
-used, it should have a high priority (PR_HIGH), in this way translators will be able to prioritise the translation of
-this strings. If a string is extremely rarely shown, use PR_LOW. You can use PR_MEDIUM as well. Leaving this field blank
-will be interpretated as a "normal" priority (some less than PR_MEDIUM). Using priorities allows translators to benefit from the 80/20 rule when translating, since typically there is a reduced
-set of strings that are widely displayed, and a lot of more specific strings. Therefore, in a module with a considerable
-amount of strings, where partial translations can be expected, priorities will help to have translated the most
-displayed strings. If a string is in a class is inheritable, it's not recommended to establish a priority (we don't know about child
-behavior a priori).
-
-### Context
-
-Last parameter is context, it's also optional. Sometimes short phrases or words can have several translations depending
-upon where they are used, and Context serves as a way to tell translators more information about the string in these
-cases where translating can be difficult, due to lack of context or ambiguity.
-
-This context param can also be used with other situations where translation may need to know more than the original
-string, for example with sprintf '%' params inside the string, since you can tell translators about the meaning of this
-parameters.
+ * **$entity:** Unique identifier, composed by a namespace and an entity name, with a dot separating them. Both are arbitrary names, although by convention we use the name of the containing class or template. Use this identifier to reference the same translation elsewhere in your code.
+ * **$string:** (optional) The original language string to be translated. Only needs to be declared once, and gets picked up the [text collector](#collecting-text).
+ * **$string:** (optional) Natural language (particularly short phrases and individual words)
+are very context dependent. This parameter allows the developer to convey this information
+to the translator. Can also be used to explain `sprintf()` placeholders.
:::php
//Example 4: Using context to hint information about a parameter
sprintf(
_t('CMSMain.RESTORED',
"Restored '%s' successfully",
- PR_MEDIUM,
'Param %s is a title'
),
$title
@@ -255,18 +220,21 @@ If you want to run the text collector for just one module you can use the 'modul
**Note**: You'll need to install PHPUnit to run the text collector (see [testing-guide](/topics/testing)).
</div>
-## Language tables in PHP
+## Language definitions
+
+Each module can have one language table per locale, stored by convention in the `lang/` subfolder.
+The translation is powered by `[Zend_Translate](http://framework.zend.com/manual/en/zend.translate.html)`,
+which supports different translation adapters, dealing with different storage formats.
-Each module can have one language table per locale. These tables are just PHP files with array notations. By convention,
-the files are stored in the /lang subfolder, and are named after their locale value, e.g. "en_US.php".
+In SilverStripe 2.x, there tables are just PHP files with array notations,
+stored based on their locale name (e.g. "en_US.php").
Example: framework/lang/en_US.php (extract)
:::php
// ...
$lang['en_US']['ImageUploader']['ATTACH'] = array(
'Attach %s',
- PR_MEDIUM,
'Attach image/file'
);
$lang['en_US']['FileIFrameField']['NOTEADDFILES'] = 'You can add files once you have saved for the first time.';
View
@@ -1452,14 +1452,17 @@ public static function get_time_format() {
* the class name where this string is used and Entity identifies the string inside the namespace.
* @param string $string The original string itself. In a usual call this is a mandatory parameter, but if you are reusing a string which
* has already been "declared" (using another call to this function, with the same class and entity), you can omit it.
- * @param string $priority Optional parameter to set a translation priority. If a string is widely used, should have a high priority (PR_HIGH),
- * in this way translators will be able to prioritise this strings. If a string is rarely shown, you should use PR_LOW.
- * You can use PR_MEDIUM as well. Leaving this field blank will be interpretated as a "normal" priority (less than PR_MEDIUM).
* @param string $context If the string can be difficult to translate by any reason, you can help translators with some more info using this param
- *
* @return string The translated string, according to the currently set locale {@link i18n::set_locale()}
*/
- static function _t($entity, $string = "", $priority = 40, $context = "") {
+ static function _t($entity, $string = "", $context = "") {
+ if(is_numeric($context) && in_array($context, array(PR_LOW, PR_MEDIUM, PR_HIGH))) {
+ $context = func_get_arg(4);
+ Deprecation::notice(
+ '3.0',
+ 'The $priority argument to _t() is deprecated, please use module inclusion priorities instead'
+ );
+ }
// get current locale (either default or user preference)
$locale = i18n::get_locale();
$lang = i18n::get_lang_from_locale($locale);
Oops, something went wrong. Retry.

0 comments on commit fca2c20

Please sign in to comment.