PHP Classes
PHP Classes

How to Create a Multilingual Website Tutorial in PHP in 2020 - PHPolyglot package blog

Recommend this page to a friend!
  All package blogs All package blogs   PHPolyglot PHPolyglot   Blog PHPolyglot package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Create a Multi...  
  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Updated on: 2020-01-15

Posted on:

Package: PHPolyglot

In a globalized world where more and more people are using the Internet from many different countries it has became increasingly important to provide sites that can be adapted to display text, images or even audio messages in multiple languages.

Read this short tutorial to learn how you can use the PHPolyglot package to help implementing modern multilingual Web sites in PHP.


 



In this article you can read about:

Why It Is Important to Create a PHP Multilingual Website?

What Can You Do to Implement a PHP Multilingual Website?

How Can the PHPolyglot Package Help Creating a PHP Multilingual Website?

How You Can Try the PHPolyglot Package Now?

Why It Is Important to Create a PHP Multilingual Website?

Web sites are useful to communicate to their users something of their interest. Since users of different countries may use different languages, it would be better if the sites provided support to appear with texts, images and even audio messages in the language of choice of the user, or at least in a language that he may prefer besides the default language that the site uses.

If you provide a Web site in multiple languages, you can also attract more people that are searching on Google for the content or services that you provide, as they will be searching using Google by typing keywords in their own favorite language.

What Can You Do to Implement a PHP Multilingual Website?

PHP comes with built-in support to provide multiple translations of the texts used in PHP Websites using the PHP gettext extension.

However, if you do not have this extension enabled in your PHP Website server, you may need to resort to a solution based on pure PHP code like any good PHP locale class that exists on the PHP Classes site, or even the PHPolyglot that I am presenting on this article.

How Can the PHPolyglot Package Help Creating a PHP Multilingual Website?

Combining APIs for different purposes like language translation, dictionary lookup, spelling correction and speech synthesis (TTS) in an easy to use and extend way, PHPolyglot offers you an opportunity to turn your texts into multilingual easily and on-the-fly.

The package uses several internal interfaces that could wrap external APIs and provides them in a generic way, no matter what third-party API you use: Yandex, Google, Bing, IBM Watson or whatever. This approach also allows contributors to jump-in easily and participate in API integration.

The package allows you to translate simple text strings or even translate array of strings in bulk mode. It gives you a way to get word variants, alternative translations or other insights provided by dictionary methods. It also can check grammar for you and even convert your texts into audio.

Of course, APIs that it uses have their strengths and and their weaknesses. APIs can have limitations, such as the number of supported languages or the number of supported voice to speak conversions. Therefore the more APIs are integrated, the more powerful PHPolyglot will become. 

In the first edition PHPolyglot has Yandex APIs for translation, dictionary lookup and grammar correction and IBM Watson API for speech synthesis.

Here is a simple example of usage of the PHPolyglot package. It shows not only how to get translations for application texts in different languages, but also get meanings for certain expressions.

use GinoPane\PHPolyglot\PHPolyglot;

try {
    
$phpolyglot = new PHPolyglot();

    
$textToLookup 'Hello!';

    
$languageFrom 'en';
    
$languageTo 'ru';

    
$response $phpolyglot->lookup($textToLookup$languageFrom$languageTo)->getEntries();

    if (empty(
$response)) {
        throw new 
Exception('Nothing returned! Maybe API has changed?');
    }

    echo 
"Word to translate: $textToLookup \n";

    echo 
"Translations: \n";

    foreach (
$response as $entry) {
        echo 
$entry->getTextTo();

        if (
$meanings $entry->getMeanings()) {
            echo 
" (" implode(", "$meanings) . ")";
        }

        echo 
"\n";
    }
} catch (
Exception $exception) {
    
$errorMessage $exception->getMessage();

    echo 
sprintf("Error happened: %s"$errorMessage);
}

How You Can Try the PHPolyglot Package Now?

As you may have read the PHPolyglot makes it easier to create multilingual Web sites, but it goes farther by providing other useful functionality for this kind of sites like getting the meaning of certain expressions or even get the audio for a text automatically translated to a supported language.

If you would like to try this package, you can just access the PHPolyglot package page and view the package code there, get instructions to install it using Composer or just download the package code into your computer so you can study it using your favorite code editor.




You need to be a registered user or login to post a comment

Login Immediately with your account on:

FacebookGmail
HotmailStackOverflow
GitHubYahoo


Comments:

No comments were submitted yet.



  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  
  All package blogs All package blogs   PHPolyglot PHPolyglot   Blog PHPolyglot package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Create a Multi...  

For more information send a message to info at phpclasses dot org.