Symfony

Last updated 6 months ago

Github repository: https://github.com/weglot/translate-bundle

WeglotSlack Latest Stable Version Maintainability License

Requirements

  • PHP version 5.5 and later

  • Weglot API Key, starting at free level

Installation

You can install the library via Composer. Run the following command:

composer require weglot/translate-bundle

When you require the bundle with symfony/flex (available for symfony/symfony:^4.0) it should ask you if you wanna execute a recipe, tell yes. Like that it will make bundle registration in config/bundles.php & default config creation in config/packages/weglot_translate.yaml.

To use the library, use Composer's autoload:

require_once __DIR__. '/vendor/autoload.php';

Getting started

Bundle Register

Symfony 4

Add Weglot bundle in the config/bundles.php:

return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
// ... Other bundles ...
Weglot\TranslateBundle\WeglotTranslateBundle::class => ['all' => true],
];

Symfony 3 & 2

Add Weglot bundle to app/AppKernel.php file:

$bundles = array(
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
// ... Other bundles ...
new Weglot\TranslateBundle\WeglotTranslateBundle(),
);

Quick configuration

For Symfony 4, create configuration file under config/packages/weglot_translate.yaml and add following content. For Symfony 3 & 2, add following content to your app/config/config.yaml.

weglot_translate:
api_key: '%env(WG_API_KEY)%'
original_language : 'en'
cache: true
destination_languages:
- 'fr'
- 'de'

This is the default configuration file, if you want more default, check next part.

Configuration

As already said, depending on Symfony version, we've different configuration file paths:

  • Symfony 4: config/packages/weglot_translate.yaml

  • Symfony 3 & 2: app/config/config.yaml

There is a full configuration file:

weglot_translate:
api_key: '%env(WG_API_KEY)%'
original_language : 'en'
cache: false
destination_languages:
- 'fr'
- 'de'
exclude_blocks:
- '.material-icons'

This is an example of configuration, enter your own API key, your original language and destination languages that you want.

  • api_key : is your personal API key. You can get an API Key by signing up on Weglot.

  • original_language : original language is the language of your website before translation.

  • destination_languages : are the languages that you want your website to be translated into.

  • cache : if you wanna use cache or not. It's not a required field and set as false by default. Look at Caching part for more details.

There is also a non-required parameters:

  • exclude_blocks : You can list here all blocks you don't want to be translated. In this example, we won't translate all DOM tags with "material-icons" class.

Twig extensions

Hreflang links are a way to describe your website and to tell webcrawlers (such as search engines) if this page is available in other languages. More details on Google post about hreflang: support.google.com/webmasters/answer/189077

You can add them through the Twig function: weglot_hreflang_render

Just put the function at the end of your <head> tag:

<html>
<head>
<!-- your header -->
{{ weglot_hreflang_render() }}
</head>

Language button

You can add a language button if you're using Twig with function: weglot_translate_render

Two layouts exists:

<!-- first layout -->
{{ weglot_translate_render(1) }}
<!-- second layout -->
{{ weglot_translate_render(2) }}

Language code

Simple filter to convert ISO 639-1 code to full language name. It can takes one boolean parameter that allow you to choose having english name or original language name.

Here is some examples:

<!-- Will return english name for given code, here: "Bulgarian" -->
{{ 'bg' | language }}
<!-- Will return original name for given code, here: "български" -->
{{ 'bg' | language(false) }}

Caching

We implemented usage of cache pool service for both Symfony 4 and Symfony 3 (symfony/cache bundle was released with Symfony 3, so there is no compatibility for Symfony 2).

If you wanna use cache, just add cache: true to this bundle configuration. It will use a file-based cache through Symfony cache.system service.

To clear the cache, you just have to use the usual pool clear command:

$ php bin/console cache:pool:clear weglot_translate.cache

Examples

You'll find a short README with details about example on each repository

About

translate-bundle is guided and supported by the Weglot Developer Team.

translate-bundle is maintained and funded by Weglot SAS. The names and logos for translate-bundle are trademarks of Weglot SAS.

License

The MIT License (MIT)