If you look at the API for t()
- https://api.drupal.org/api/drupal/includes!bootstrap.inc/function/t/7 - you'll notice that it allows you do specify $langcode
in the options array, however, that is a method to override the output language - not the input.
If you don't want to translate from English to Spanish, but the other way, there is a small quirk in Drupal localization framework. The name "English" in Drupal is reserved as the default index for languages, so you can't use that name for the target language. But please note that you don't have to use the index named "English" to hold English strings. You can use anything for that index. You can define new languages, and even invent them, so let us define a target language called "Ingles", and use that for English.
Now for each phrase you want to translate fro Spanish to English, you need to assign an index for lookup in the translation table. Let say you assign "¡Hola, mundo!" to be the index corresponding to the Spanish phrase "¡Hola, mundo!". On at least one page (it doesn't matter if it is Spanish, English or "Ingles, you must have t("¡Hola, mundo!");
to get Drupal to pick this up. Now go to Drupal's translation interface and search for "¡Hola, mundo!". When you've found it, you can enter the correct strings for the Spanish original ("¡Hola, mundo!") into the field for "Spanish" and its translation into English ("Hello, world!") into the field for "Ingles". Do that with every phrase you want to translate from Spanish to English.
When done, you should be able to use t("¡Hola, mundo!");
on a page with Spanish as its default language to make the user see "¡Hola, mundo!". To get the translation into English, you also use t("¡Hola, mundo!");
, but make sure the page's default language is "Ingles" (not English). This is how you use t()
-to translate from Spanish to "Ingles" (which is just an alias for "English" - we must use this alias since Drupal has reserved "English" to hold the default translation index).
However, you may find it a bit awkward to "invent" a new language called "Ingles". And there is a simpler way!
Instead of using "¡Hola, mundo!" as an index to "¡Hola, mundo!", you pick "Hello, world!" as your index. Otherwise, you proceed like before - to get this variable into Drupal's inserted into translation interface, you must on at least one page have t("Hello, world");
(it doesn't matter if that page is Spanish, English or "Ingles).
Now, instead of "¡Hola, mundo!" showing up as am index in Drupal's translation interface, you'll see the index "Hello, world!". But just like before must enter the string "¡Hola, mundo!" as the Spanish original phrase, and for translation into "Ingles", you enter "Hello, world!".
However, now there it is also possible to get rid of the invented language "Ingles". It just so happens that if the default language for a page is English (not "Ingles"), t();
will not look up the string in the table but just return the index. And since we're now using English phrases as index that index will be in English on pages where the default language is English. So we can stop using the the invented language "Ingles" and instead rely on getting the index back on English pages, while on Spanish pages, the index will be used for table lookup and we will get a Spanish string back.
t()
can only be used in code.