How to translate website automatically

0

I have a site in Portuguese written in PHP and HTML. I would like to offer this site to other countries, but without having to translate all content manually. So I would like a tool that translates the site automatically when the person accesses the link ( link ) or ( link ), similar to the option to translate from Chrome. The only most viable solution I found was Google Website Translator:

https://translate.google.com/manager/website/

So the site has a side bar where the person chooses the language that wants to see the page. However, this medium does not auto-translate the page, the person has to click to change to the desired language.

Sorry if my question is a little broad. Does anyone know of any other solution? Thank you.

    
asked by anonymous 11.08.2017 / 17:28

1 answer

2

I particularly like I like this zito system , it's simple and you can do several things with it!

You can define the language by $_GET , ie it is by url

You can have as many languages as you need, and it's easy to add translations!

Here's an example:

//Definir linguagens a ser usadas:

define('LANG_ENGLISH', 'en');
define('LANG_FRENCH', 'fr');
define('LANG_SPANISH', 'es');

// Definir linguagem principal
$language = isset($_GET['l']) ? $_GET['l'] : LANG_ENGLISH;

// Mini-função que faz o seu trabalho:

function t($string, $args = array(), $langcode = NULL) {
  global $language, $translation;

  // Set language code.
  $langcode = isset($langcode) ? $langcode : $language;

  // Search for a translated string.
  if ( isset($translation[$langcode][$string]) ) {
    $string = $translation[$langcode][$string];
  }

  // Replace arguments if present.
  if ( empty($args) ) {
    return $string;
  } else {
    foreach ( $args as $key => $value ) {
      switch ( $key[0] ) {
        case '!':
        case '@':
        case '%':
        default: $args[$key] = $value; break;
      }
    }

    return strtr($string, $args);
  }
}

To add translations:

(in another separate file)

//index.php
// incluir a mini-função acima se tiver noutro ficheiro
include('inc.language.php');
// adicionar traduções a uma certa linguagem exemplo:
$translation[LANG_SPANISH] = array(
  'Email' => 'email',
  'Name' => 'nombre',
  'Organization' => 'Organización',
  'Phone Number' => 'Número de teléfono',
  'Hello %name' => 'Hola %name',
);

In its use it would look something like:

//www.site.com/index.php?l=es
<text id="org"><?= t('Organization') ?></text>
// output: Organización
    
11.08.2017 / 20:08