Skip to content
This repository has been archived by the owner on Oct 29, 2020. It is now read-only.

Localization Guidelines

sergii-tkachenko edited this page Sep 16, 2014 · 16 revisions

Our awesome international partners speak many languages. So should DoSomething templates and JavaScripts.

PHP Templates

All strings in PHP-based templates must be wrapped into t() function:

<?php print t('Edit Profile'); ?>

Usage Examples

PR #2878 replaces all hardcoded strings of Pareneue theme with t() wrapper.

  1. Hardcoded string

    - Edit Profile
    + <?php print t('Edit Profile'); ?>
  2. 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.

Placeholders

There are three styles of placeholders:

  1. @variable: escaped to HTML using check_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));
  2. !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,
   ));

JavaScript

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 during ds grunt task.

Usage Examples

PR #2928 replaces all hardcoded strings in Pareneue theme JavaScripts with Drupal.t() wrapper.

  1. Hardcoded string

    - $uploadBtn.text("Change Pic");
    + $uploadBtn.text(Drupal.t("Change Pic"));
  2. 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

  1. Underscore Templates

    - <div class="__flag -staff-pick">Staff Pick</div>
    + <div class="__flag -staff-pick"><%= Drupal.t("Staff Pick") %></div>

FAQ

I added a string with t(), but can't see it

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!

Adding/switching Drupal language using drush

For JavaScript's Drupal.t() you may also need to clean JavaScript cache and reload the page once more:

  • drush cc css-js
Clone this wiki locally