Php translate string to english

been google'ing for a while how is the best way to translate with google translator in PHP, found very different ways converting URLS, or using Js but i want to do it only with php (or with a very simple solution JS/JQUery)

example:

//hopefully with $from_lan and $to_lan being like 'en','de', .. or similar
function translate($from_lan, $to_lan, $text){

// do

return $translated_text;

}

can you give me a clue? or maybe you already have this function..

my intention it's to use it only for the languages i have not already defined (or keys i haven't defined), that's why i wan it so simple, will be only temporal..

EDIT

thanks for your replies we are now trying this soulutions:

function auto_translate($from_lan, $to_lan, $text){
// do


$json = json_decode(file_get_contents('https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($text) . '&langpair=' . $from_lan . '|' . $to_lan));
$translated_text = $json->responseData->translatedText;


return $translated_text;

}

(there was a extra 'g' on variables for lang... anyway)

it returns: works now :)

i don't really understand much the function, so any idea why is not acepting the object? (now i do)

OR:

    function auto_translate($from_lan, $to_lan, $text){
    // do

//    $json = json_decode(file_get_contents('https://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=' . urlencode($text) . '&langpair=' . $from_lan . '|' . $to_lan));
//    $translated_text = $json['responseData']['translatedText'];
    error_reporting(1);
    require_once('GTranslate.php');
    try{
       $gt = new Gtranslate();
       $translated_text = $gt->english_to_german($text);

     } catch (GTranslateException $ge)
     {
           $translated_text= $ge->getMessage();
     }


    return $translated_text;
}

And this one looks great but it doesn't even gives me an error, the page won't load (error_report(1) :S)

thanks in advance!

Translator services

Php translate string to english

Services that can be used to translate strings

Install

The first thing you need to do is to install a HTTP client. Please read HTTPlug quickstart. When the client is installed you may install this package with composer by running:

composer require php-translation/translator

Intro

$translator = new Translator();
$translator->addTranslatorService(new GoogleTranslator('api_key'));

echo $translator->translate('apple', 'en', 'sv'); // "äpple"

When you plan to achieve a wider audience for your website – SEO (Search Engine Optimization), is an important task. Translating your website is an additional, however essential step, to rank higher in the search engine and drive more traffic into your website.

While there is no standard way to translate a custom-built multilingual PHP website, there are various parts of your website that requires special localization configuration/implementation. Including, a backend (dynamic content from DB) and website interface (UI). This blog will collectively shed light on different aspects and implementation methods.

Internalization Challenges

Internationalization usually is a one-time effort, but it is a challenging one and often underestimated by programmers. Various aspects need to be planned when structuring different components of the website/ app UI and the backend.

There are fundamental differences between languages that need to be taken into consideration when localizing your website. These include language nuances, date formats, grammar rules (such as pluralization).

For more tips on RTL localization, please visit localization for RTL languages.

Methods and Tools for Internationalization

For a multilingual website, you need to consider the following features:
–       A language switcher
–       Load translated content based on geolocation or browser language
–       URL routing

PHP Array Files
PHP associative arrays are the easiest way to translate a site. Translated strings are stored in a file for each language.

Php translate string to english

The strings are then retrieved and displayed inline according to the selected language.

Php translate string to english

This approach has been used for a long time and seems to be simple however, it is hard to maintain with a larger app scale, and in a variety of languages.
For example, the pluralization for English works differently from other languages. In Arabic, this implies a challenge and would require some conditional switching which might clutter up the code.
Some frameworks that use this approach are, Laravel, CodeIgniter, Kohana, and Zend.

GETTEXT Extension

The classic GETTEXT function has been around for a long time, where it used to be considered a rule of thumb for the internalization of applications and websites in many programming languages. Using the extension may be a little problematic in PHP, where you cannot install the extension into your hosting environment. In this case, PHP-GETTEXT can be used.

Many frameworks support internationalization through GETTEXT like Yii, Zend, CakePHP, Drupal, and WordPress. So, it is better to rely on a framework to build a multilingual website, rather than building a custom website and handling it yourself in the code.

.PO Files

For a custom PHP website, you will need to set up your .PO files to store translations in plain text or use a tool to extract the strings for you. When the translations are completed, the .PO files need to be compiled into .MO files and stored in a translation directory.

Database Table Translation

Implementing a simple but efficient database design is key for performance and scalability.

Approach 1: For every translatable entity in the database, a table is created, and corresponding translated columns are added for each language.

Php translate string to english

This approach might work for small websites and few languages, but for large websites and 15 languages, this solution is not the best in terms of performance and scalability.

Approach 2: A table is created for each language.

Php translate string to english

This will work best with small websites and a couple of languages.

Approach 3: As you can see below, this is a simple translation implementation on the database that can be generalized to all translatable entities in the database. This approach is simple and scalable as we add new languages to the website, it will not require any alteration to the database schema. Also, the queries developed to extract content based on the language are not complex.

Php translate string to english

Conclusion

When building a website, it is useful to consider localization best practices from the beginning, especially for business that targets to expand globally.

Most CMS’s and frameworks have built-in localization capabilities that make a developer’s life easier to build a multilingual website out of the box. Although Python, Node.js, and Ruby on Rails, are widely used, flat PHP websites are still being preferred by many developers.

How can I translate in PHP?

php class Translator { private $language = 'en'; private $lang = array(); public function __construct($language){ $this->language = $language; } private function findString($str) { if (array_key_exists($str, $this->lang[$this->language])) { echo $this->lang[$this->language][$str]; return; } echo $str; } private ...

How do you translate a string?

The translate() method returns a string where some specified characters are replaced with the character described in a dictionary, or in a mapping table. Use the maketrans() method to create a mapping table. If a character is not specified in the dictionary/table, the character will not be replaced.

What is PHP gettext?

October 28, 2020 · 16 min read. GNU gettext is a package that offers to programmers, translators and even users a well integrated set of tools that provide a framework within which other free packages may produce multi-lingual messages.

How can I convert Arabic to English in PHP?

What you could do as a makeshift solution is this : dynamically append a "dir=rtl" attribute to your html tag (or whatever tag containing text to be translated) and then somehow redirect to a google translated version of that page.