5

I want to be able to use $this->__('String to translate') in an external script. How do I do this? Magento version 1.5.1.0.

Niklas Modess
  • 2,521
  • 1
  • 20
  • 34

2 Answers2

10

I think the right way to set locale is:

Mage::getSingleton('core/translate')->setLocale('sv_SE')->init('frontend', true);
cads
  • 171
  • 2
  • 9
  • If you copy and paste this code, this will retrieve the transactions without using the cache, the right way is: `Mage::getSingleton('core/translate')->setLocale('sv_SE')->init('frontend');` You're warned! – Christophe Eblé Dec 13 '13 at 12:42
6

This should work:

require 'app/Mage.php'; // here you should use the correct path to Mage.php file

Mage::app();
Mage::getSingleton('core/translate')->init('sv_SE', true); // UPDATE
$to_translate = "String to translate";
$translated = Mage::helper('core')->__($to_translate);
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Fabrizio D'Ammassa
  • 4,729
  • 25
  • 32
  • Thank you, seems to be working (no errors) but it doesn't translate if I put a translation in the `translate.csv` file. How do I tell it which language to use? I want it to use `sv_SE`. – Niklas Modess Aug 25 '11 at 11:12
  • I have added a new line of code to force the Magento translator work in sv_SE language. Not sure if it works, but the best way to accomplish your task would be to create different store views for each language: http://www.magentocommerce.com/design_guide/articles/magento-design-terminologies4 – Fabrizio D'Ammassa Aug 25 '11 at 11:23
  • 1
    The init function expects an area as a parameter, not a locale. Please see the correct answer below: Mage::getSingleton('core/translate')->setLocale('sv_SE')->init('frontend', true); – petrica.martinescu Jul 21 '15 at 13:15
  • How can we done this activity in magento2 – Sanjeev Kumar Sharma Mar 03 '22 at 11:22