Localization Guidelines - DoSomethingArchive/legacy-website GitHub Wiki
Our awesome international partners speak many languages. So should DoSomething templates and JavaScripts.
All strings in PHP-based templates must be wrapped into t()
function:
<?php print t('Edit Profile'); ?>
PR #2878 replaces all hardcoded strings of Pareneue theme with t()
wrapper.
-
- Edit Profile + <?php print t('Edit Profile'); ?>
-
Hardcoded string with variables
- Hey, <?php print $user_account['first_name']; ?>! + <?php print t("Hey, @name!", array("@name" => $user_account['first_name'])); ?>
In this case
@name
is just a placeholder for variable$user_account['first_name']
, so actual$user_account['first_name']
value doesn't get translated.
There are three styles of placeholders:
-
@variable
: escaped to HTML usingcheck_plain()
.Use this as the default choice for anything displayed on a page on the site.
$title = t("@name's blog", array('@name' => $account->name));
-
!variable
: inserted as is, with no sanitization or formatting.Only use this for text that has already been prepared for HTML display. This is useful for inserting variables into things like e-mail.
$message = t("You can change your settings at !url.", array( '!url' => l(t('My account'), "user/" . $account->uid)), );
3. `%variable`: escaped to HTML and formatted using [`drupal_placeholder()`](https://api.drupal.org/api/drupal/includes%21bootstrap.inc/function/drupal_placeholder/7), which makes it display as `<em>emphasized</em>` text.
```php
t('%name-from sent %name-to an e-mail.', array(
'%name-from' => $user->name,
'%name-to' => $account->name,
));
Drupal Documentation: Translating strings in JavaScript.
All strings in JavaSctipt that will be displayed to user must be wrapped into Drupal.t()
function:
var comments = Drupal.t("Comments");
Please use double quotes in JavaScript strings. Always.
JSHint recommends using double quotes for code consistency. Otherwise it will nag duringds grunt
task.
PR #2928 replaces all hardcoded strings in Pareneue theme JavaScripts with Drupal.t()
wrapper.
-
- $uploadBtn.text("Change Pic"); + $uploadBtn.text(Drupal.t("Change Pic"));
-
Hardcoded string with variables
- message: "Hey, " + string + "!" + message: Drupal.t("Hey, @name!", {"@name": string})
In JavaScripts placeholders works the same as in PHP. Reference: drupal.js:168
-
- <div class="__flag -staff-pick">Staff Pick</div> + <div class="__flag -staff-pick"><%= Drupal.t("Staff Pick") %></div>
Strings are added to the Drupal translation interface automatically on first t('Your string')
call, wherein Drupal is set to the language other than English!
For JavaScript's Drupal.t()
you may also need to clean JavaScript cache and reload the page once more:
drush cc css-js