Magento translations in external script
Asked Answered
M

2

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.

Myrmecophagous answered 25/8, 2011 at 10:31 Comment(0)
U
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);
Untoward answered 25/8, 2011 at 10:49 Comment(4)
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.Myrmecophagous
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: magentocommerce.com/design_guide/articles/…Schooling
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);Cocke
How can we done this activity in magento2Tubular
N
10

I think the right way to set locale is:

Mage::getSingleton('core/translate')->setLocale('sv_SE')->init('frontend', true);
Nabala answered 6/12, 2011 at 12:3 Comment(1)
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!Britten
U
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);
Untoward answered 25/8, 2011 at 10:49 Comment(4)
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.Myrmecophagous
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: magentocommerce.com/design_guide/articles/…Schooling
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);Cocke
How can we done this activity in magento2Tubular

© 2022 - 2024 — McMap. All rights reserved.