6

On Drupal 7 with the i18n-modules, in what language should your default strings be written in? In modules it's obvious that all strings should be in English (for reuseability) and use t().

But a custom theme will only be used for the specific site. If the site is not in English it seems lika an awkward practice to first write everything in English and then translate to the specific language.

3

If you're 100% sure that you do not need to (re-)use this code in a multi-language environment, now or in the future, you're safe using foreign language strings. Personally, I consider it best practice to always use t() because you never know what's going to happen in the future.

Be aware that strings in code that are run through t(), are assumed to be in English, so you have to choose between t('English text') or 'Svensk text'. Never use t('Svensk text').

  • You're never 100% sure, ever. – Bart Dec 10 '11 at 16:13
1

There is no difference between using t() in modules, or themes; in both the cases, the function expects the string passed as argument to be in English.

Between using t(), even when you know the page is always show in a specific language, and using a string that is in the language you know to use, using t() is preferable.

Why? Because in the first case, the string would be hardcoded in the code of the theme, which means you cannot change it without to change the code. If the page that you are now showing in a language needs to be shown in a different language, you should change the code of your theme to change the strings. That is exactly what t() avoids.
Suppose that the same code is used for more than one site, and in one site one of the string needs to be different. Without using t(), you would need two different themes, while using t() you would use the same code for both the sites.

It's enough that in the settings.php file you add lines similar to the following:

 $conf['locale_custom_strings_en'][''] = array(
   'forum'      => 'Discussion board',
   '@count min' => '@count minutes',
 );

The example is for changing the strings used for English, but the same can be done for every language; it's enough you change the "_en" part in "locale_custom_strings_en" with the language ID for the language you are interested. The first string is in English, and the second string is the translation of the first string in that language.

Suppose that your theme contains print t('forum'). Adding that code in the settings.php file, what the user will see, when the currently set language for that page is English, is "Discussion board"; if your theme contains print 'forum', the user will always see "forum," and to change that you need to change the code of your theme.

I could also use the following code:

 $conf['locale_custom_strings_it'][''] = array(
   'forum'      => 'Tavola di discussione',
   '@count min' => '@count minuti primi',
 );

In this case, when Italian is the language set for a page, and the theme contains print t('forum'), what the users will see is "Tavola di discussione."

2

I personally wouldn't want to use English in the theme if there was no expectation of the site ever using that language. I think it would slow down my initial coding, slow down updating text, and if the site was to need English at a later date, I doubt the quality of the initial translations would be up to scratch (because I was never anticipating them being used live). Having said that, obviously if you are going to use i18n at any point you'll need to follow the rules and use English, similarly if you want to release the theme on d.o (now or in the future), or reuse it in a project featuring i18n. One suggestion: I think I might create a function, eg.

function ct($string, array $args = array(), array $options = array()) {
    return t($string, $args, $options);
}

And use that in place of t(). So to continue from marcvangend you could use ct('Svensk text'). This way you get the convenience of using the final language, but you can easily find examples of where you've done so and quickly modify them if you later need to.

  • Using ct(), or t() is the same thing, as the first string passed to t() must be in English. Also, if you replace t('English string') with ct('English string'), the script that looks for the strings to translate them will not find any string to translate because it looks for t('literal string'), not t($dynamic_variable). – kiamlaluno Sep 7 '12 at 3:27
  • @kiamlaluno If you read the arguments again, I never claim this will give you English translations. By using a different function name you allow rapid prototyping/build using non-English phrases, and if you later need to update the site to be translated into English, you can search and replace on that specific function name to find every example of the native language. If you have no initial need to translate, especially if you or some members of your team aren't proficient in English, then this is the best strategy I can think of. (I don't do i18n so this isn't tested, just a suggestion.) – Andy Sep 7 '12 at 8:15
  • If you are going to use ct('Svensk text'), as you suggest in your answer, then you can use directly 'Svensk text'. With the code you show, ct('Svensk text') is wrong as well as t('Svensk text'): The first argument of t() is string containing English words. – kiamlaluno Sep 7 '12 at 8:41
  • @kiamlaluno The problem with your suggestion is that if you then later want to have your site in English, you need to find every place where you've used a Swedish (for example) string literal. That's not very easy. Finding every example of ct() would be much easier. Additionally you can still use t() style placeholders to sanitise your output, making the replacement to t() (if ever necessary) less of a hassle. – Andy Sep 7 '12 at 8:47
  • In that case, you can use t('Svensk text') directly, if the messages are all in the same language. – kiamlaluno Sep 7 '12 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.