1

I have a Drupal 7 webpage already developed with all texts in Spanish, but now I want to add English language to the page. As all my texts are written in spanish, I would like to know what is the best/fast way to translate it to english. I tried with t() function, something like this t('Hola mundo') expecting to be manually translated by me with Drupal translation module (admin/config/regional/translate/translate) to 'Hello world'.

The Drupal 7 documentation says this: $string A string containing the English string to translate.

So my question is: is there any way to translate with t() from the default language (spanish)?

If it is not possible, how can I translate all the texts to english from spanish?

Thanks

  • Is the web page content (e.g. an Article node), or is it a page you generate through PHP code? API functions like t() can only be used in code. – Free Radical Mar 25 '15 at 2:25
  • It is mostly generated with code, that's why I though t() was a good solution. – KeoP Mar 25 '15 at 9:37
1

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.

  • As I commented in the Patrick answer, I am not expecting to be translated automatically by Drupal, I know I have to translate it manually in admin/config/regional/translate/translate (maybe I didn't explain myself right, so sorry for my bad english). I have set Spanish as the default Drupal language hoping to make it work, but I wasn't able to translate from Spanish to English with t() function. – KeoP Mar 25 '15 at 14:42
  • Thanks for your answer anyway. I'm still looking for some way to translate from Spanish to English (maybe another Drupal module?) – KeoP Mar 25 '15 at 14:45
  • There is no problem using t() to translate from Spanish to English. You just have to do it in a way that is not very intuitive. – Free Radical Mar 25 '15 at 15:20
  • Hi, that is exactly what I need. How can I use t() to show translated strings from Spanish (default language in my site) to English? As the docs says, the input parameter has to be in English, but I'd like to find a way to use the parameter as a Spanish string, that would be perfect for me. Thanks. – KeoP Mar 25 '15 at 19:44
  • The default parameter doesn't have to be English - it just works simpler that way. – Free Radical Mar 25 '15 at 21:31
0

Drupal does not translate anything for you. Drupal along with other modules simply provide functionality for you to easily manage your translations. Where you get these translations is entirely out of Drupal's hands. There are some modules that integrate 3rd party translation services such as Google translate, but most of these services are not very accurate.

Wrapping text in t() just allows Drupal to know that this is a translatable piece of text. Anything wrapped in t() will be accessible to modules so that you can supply the translation manually.

If you don't already have the internationalization modules installed, you should start there - but these will only help you manage your translations rather that translate for you. A good place to get started would be here and here.

  • Thanks for your answer but I didn't say I want Drupal to translate for me anything. As I said, I want to translate with Drupal from translation modules (admin/config/regional/translate/translate), not by Drupal. – KeoP Mar 24 '15 at 21:29
  • And my question is: Is there any way to translate with t() from the default language (spanish)? If it is not possible, how can I translate all the texts to english from spanish? – KeoP Mar 24 '15 at 21:30
  • t() doesn't care what language you put in it - it will make the string available at (admin/config/regional/translate/translate). If you are unable to see it there, you probably need to visit the page that the string is displayed on first - that will trigger it to be registered. – Patrick Ryan Mar 24 '15 at 22:19
  • Are you sure t() doesn't care about the language you put in it? This is my main problem here. I see all I put inside t() in admin/config/regional/translate/translate but I can't translate it to english, it only let me translate into spanish – KeoP Mar 25 '15 at 9:37
0

You need a function that gets a string in default language (spanish) and returns it in the user's language (i.e. english). Right? Module i18n provides this function: i18n_string_translate($name, $string, $options)

More about it here:

https://www.drupal.org/node/1114010

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.