Mode to translate website automatically

Asked

Viewed 1,293 times

1

I have a Portuguese site 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 (http://site.com/english) or (http://site.com/? lang=en), similar to the Chrome translate option. The only viable solution I found was Google Website Translator:

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

So the site gets a side bar where the person chooses the language they want to see the page. However, this means does not make the machine translation of the page, the person has to click to switch to the desired language.

Sorry if my question is a bit wide. Does anyone know any other solution? Thank you.

1 answer

2

Me Particularmente like this systemZito, It’s simple and you can do a lot of things with it!

you can define the language by $_GET, ie is by url

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

Here is 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 file separately)

//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 be something like:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.