Shane Lessard
Shane Lessard

Reputation: 655

How do I make a node title translatable?

I'd like to translate each node title as a string (using i18n). I'm trying this function in my theme template:

function theme_process_page(&$variables) {
  $variables['title'] = t($variables['title']);
}

Yet when I refresh strings, none of my node titles are on the list. Is there something I'm missing?

And to clarify the function name is using my theme name, not the word "theme".

Upvotes: 1

Views: 338

Answers (2)

Florian Motteau
Florian Motteau

Reputation: 3724

Title is my usual solution for this (I use Entity Translation, it works fine with Title module).

This module replaces node titles by a regular translatable text field. You can choose wich content type titles must be replaced (on the "Manage Field" forms, you'll find a "replace" link in the title row). Pretty useful.

Good luck

Upvotes: 2

Niklas Brunberg
Niklas Brunberg

Reputation: 759

You should never use t() to translate user-supplied or variable strings. See the documentation on the function.

That said, there are some solutions, one is to use the built-in language support for entity fields. Following that you should be able to do something like this in a field hook (in a module, not in your template):

$langcode = $field_info['translatable'] ? $content_langcode : LANGUAGE_NONE;
$entity->{$field_name}[$langcode][0]['value'] = t("Salut!");

Upvotes: 0

Related Questions