Laravel translation made __('simple').

Overview

Translation.io client for Laravel 5.5+/6/7/8

Software License Build Status Test Coverage Maintainability Package Version Downloads

Add this package to localize your Laravel application.

Use the official Laravel syntax (with PHP or JSON files), or use the GetText syntax.

Write only the source text, and keep it synchronized with your translators on Translation.io.

Translation.io interface

Technical Demo (2.5min)

Need help? [email protected]

Table of contents

Translation syntaxes

Laravel Localization (PHP key/values)

The default Laravel method to localize.

// Regular
__('inbox.title');

// Regular with sublevel key
__('inbox.menu.title');

// Pluralization
trans_choice('inbox.message', $number);

// Interpolation
__('inbox.hello', ['name' => $user->name]);

With the PHP file resources/lang/en/inbox.php:

return [
    'title' => 'Title to be translated',
    'hello' => 'Hello :name',
    'messages' => 'One message|Many messages',
    'menu' => [
        'title' => 'Title of menu'
    ]
];

Notes:

  • trans can also be used instead of __.
  • You can organize your PHP files with subfolders like en/subfolder/inbox.php using keys like subfolder/inbox.title.

Laravel Localization (JSON source text)

A new feature of Laravel 5.4 is the possibility to use __ with the source text (and not only with keys like in the previous section).

These translations are stored into JSON files located in the resources/lang/ directory.

// Regular
__("Text to be translated");

// Pluralization
trans_choice(__('One message|Many messages'), $number);

// Interpolation
__('Hello :name', ['name' => $user->name]);

With the JSON file resources/lang/en.json:

{
    "Text to be translated": "",
    "One message|Many messages": "",
    "Hello :name": ""
}

Notes:

  • To spend less time dealing with multiple JSON files, we advise to only edit the original language (usually en.json) to add new strings, and leave the translations empty. During a sync, This package will automatically create and fill the JSON files of the target languages.

  • If you want to organize your JSON files by feature, you can register new paths in AppServiceProvider like this:

public function boot()
{
    $loader = $this->app['translation.loader'];
    $loader->addJsonPath(base_path('resources/lang/my_feature'));
}

GetText

This package adds the GetText support to Laravel. We strongly suggest that you use GetText to localize your application since it allows an easier and more complete syntax.

Also, you won't need to create and manage any PHP or JSON file since your code will be automatically scanned for any string to translate.

// Regular
t("Text to be translated");

// Pluralization
n("Singular text", "Plural text", $number);

// Regular with context
p("context", "Text to be translated");

// Pluralization with context
np("context", "Singular text", "Plural text", $number);

// Simple Interpolations (works with n, p and np too)
t('Hello %s', $user->name);

// Complex Interpolations (works with n, p and np too)
t(':city1 is bigger than :city2', [ ':city1' => 'NYC', ':city2' => 'BXL' ]);

Installation

  1. Add the package via Composer:
$ composer require tio/laravel
  1. Create a new translation project from the UI.
  2. Copy the initializer into your Laravel app (config/translation.php) or execute php artisan vendor:publish.

The initializer looks like this:

return [
    'key' => env('TRANSLATIONIO_KEY'),
    'source_locale' => 'en',
    'target_locales' => ['fr', 'nl', 'de', 'es']
];
  1. Add the API key (TRANSLATIONIO_KEY) in your .env file.
  2. Initialize your project and push existing translations to Translation.io with:
$ php artisan translation:init

If you need to add or remove languages in the future, please read this section about that.

Usage

Sync

To send new translatable keys/strings and get new translations from Translation.io, simply run:

$ php artisan translation:sync

Sync and Show Purgeable

If you need to find out what are the unused keys/strings from Translation.io, using the current branch as reference:

$ php artisan translation:sync_and_show_purgeable

As the name says, this operation will also perform a sync at the same time.

Sync and Purge

If you need to remove unused keys/strings from Translation.io, using the current branch as reference:

$ php artisan translation:sync_and_purge

As the name says, this operation will also perform a sync at the same time.

Warning: all keys that are not present in the current local branch will be permanently deleted from Translation.io.

Manage Languages

Add or Remove Language

You can add or remove a language by updating 'target_locales' => [] in your config/translation.php file, and executing php artisan translation:sync.

If you want to add a new language with existing translations (ex. if you already have a translated PHP file in your lang directory), you will need to create a new project on Translation.io and run php artisan translation:init for them to appear.

Edit Language

To edit existing languages while keeping their translations (e.g. changing from en to en-US).

  1. Create a new project on Translation.io with the correct languages.
  2. Adapt config/translation.php (new API key and languages)
  3. Adapt directory language names in resources/lang (optional: adapt GetText .po headers)
  4. Execute php artisan translation:init and check that everything went fine.
  5. Invite your collaborators in the new project.
  6. Remove the old project.

Since you created a new project, the translation history and tags will unfortunately be lost.

Custom Languages

A custom language is always derived from an existing language. It's useful if you want to adapt some translations to another instance of your application, or to a specific customer.

The structure of a custom language is: existing language code + - + custom text, where custom text can only contain alphanumeric characters and -.

Examples: en-microsoft or fr-BE-custom.

Custom languages can be added and used like any other language.

Change the current locale

Globally

The easiest way to change the current locale is with the set.locale Middleware.

// in routes/web.php

// Solution 1: Apply the locale selection to root.
//             => https://yourdomain.com?locale=fr
Route::get('/', function () {
    return view('welcome');
})->middleware('set.locale');

// Solution 2: Apply the locale selection to many routes.
//             => https://yourdomain.com/...?locale=fr
Route::middleware('set.locale')->group(function () {
    Route::get('/', function () {
        return view('welcome');
    });
});

// Solution 3: prefix your routes with the locale and apply it.
//             => https://yourdomain.com/fr
//             => https://yourdomain.com/fr/...
Route::prefix('{locale?}')->middleware('set.locale')->group(function() {
    Route::get('/', function () {
        return view('welcome');
    });
});

First time the user will connect, it will automatically set the locale extracted from the browser HTTP_ACCEPT_LANGUAGE value, and keep it in the session between requests.

The set.locale Middleware code is here, feel free to adapt it with your own locale management.

Locally

Change the current locale with:

use Tio\Laravel\Facade as Translation;

Translation::setLocale('fr');

Frontend Localization

Using this Package

This package is also able to cover frontend localization (React, Vue, ...).

There are several ways to pass the translation strings from the backend to the frontend: JavaScript serialization, data- HTML attributes, JSON files etc.

The easiest strategy when dealing with React/Vue would be to pass the corresponding translations as props when mounting the components.

Notes:

  • You can structure the i18n props with multiple levels of depth and pass the subtree as props to each of your sub-components.
  • It also works great with server-side rendering of your components.

Using our official React & JavaScript package

As Translation.io is directly integrated in the great Lingui internationalization framework, you can also consider frontend localization as a completely different localization project.

Please read more about this on:

Advanced Configuration Options

The config/translation.php file can take several optional configuration options.

Some options are described below but for an exhaustive list, please refer to translation.php.

Ignored PHP keys

If you would like to ignore specific PHP keys, or even entire PHP files or subdirectories from the source language, you can use the ignored_key_prefixes option.

For example:

return [
    ...
    'ignored_key_prefixes' => [
        'validation',        // ignore the whole validation.php file.
        'validation.custom', // ignore the "custom" subtree in validation.php file.
        'subfolder/more',    // ignore the whole subfolder/more.php file.
    ],
    ...
];

Testing

To run the specs with oldest dependencies:

$ composer update --no-interaction --prefer-stable --prefer-lowest
$ ./vendor/bin/phpunit

To run the specs with latest dependencies:

$ composer update --no-interaction --prefer-stable
$ ./vendor/bin/phpunit

Contributing

Please read the CONTRIBUTING file.

List of clients for Translation.io

These implementations were usually started by contributors for their own projects. Some of them are officially supported by Translation.io and some are not yet supported. However, they are quite well documented.

Thanks a lot to these contributors for their hard work!

Ruby on Rails (Ruby)

Officially Supported on https://translation.io/rails

Credits: @aurels, @michaelhoste

Laravel (PHP)

Officially Supported on https://translation.io/laravel

Credits: @armandsar, @michaelhoste

React, React Native and JavaScript

Officially Supported on https://translation.io/lingui

Translation.io is directly integrated in the great Lingui internationalization project.

Others

If you want to create a new client for your favorite language or framework, please read our Create a Translation.io Library guide and use the special init and sync endpoints.

You can also use the more traditional API.

Feel free to contact us on [email protected] if you need some help or if you want to share your library.

License

The MIT License (MIT). Please see License File for more information.

Comments
  • Support for multiple JSON files

    Support for multiple JSON files

    We have a very large application that has multiple JSON translation files, registered with Lang::addJsonPath(). These files are split based on multiple modules within the app.

    It looks like it'd be relatively simple to update GettextPOGenerator::jsonFiles() to use all the registered JSON paths. Something like:

    $paths = [$this->application['path.lang']];
    
    $loader = Lang::getLoader();
    if ($loader instanceof FileLoader) {
      foreach ($loader->jsonPaths() as $jsonPath) {
        $paths[] = $jsonPath;
      }
    }
    
    foreach ($paths as $path) {
      foreach (glob($path . DIRECTORY_SEPARATOR . '*.json') as $filename) {
        $files[] = $filename;
      }
    }
    

    The trickier issue would be resolving which JSON file to write back to on sync. Is this something you're interested in supporting? I've been digging around in GettextTranslationSaver and it seems like it'd take a bit of a refactor to support this use-case.

    opened by inxilpro 18
  • Gettext support

    Gettext support

    Hey @armandsar, I added Gettext support to this package.

    A lot of Gettext syntaxes are now supported. You can see how in that file: tests/fixtures/gettext/example.php.

    As you can see, some are quite classic (_("") or _n("", "", number)) and some are added for easier interpolation (_i("hello %s", "world")).

    There are some tests for both client/server behaviour but also to test GetText functions.

    You'll see that there are also sync_and_purge and sync_and_show_purgeable methods in the package. They work like to the ones in Ruby: https://github.com/aurels/translation-gem#usage

    I also renamed php artisan translations:xxx to php artisan translation:xxx for better consistency, I hope you don't mind.

    Please tell me if you see something weird in the package, I certainly did some mistakes.

    Also, to change the language in your Laravel application, you need to do:

    $t = new TranslationIO(config('translationio'));
    $t->setLocale('fr');
    

    Do you know if there is a way to do something like TranslationIO::setLocale('fr') instead? I think I saw something like this somewhere but I'm not very good with Facades and PHP meta-programming.

    opened by MichaelHoste 16
  • Folder

    Folder "app" doest not exists. Gettext scan aborted.

    With a vanilla Laravel 8.10.0 project, I am getting the following error when calling translation:init

    Init started
    
       Exception
    
      Folder "app" doest not exists. Gettext scan aborted.
    
      at /var/www/project/vendor/tio/laravel/src/GettextPOGenerator.php:52
         48▕
         49▕         // Extract GetText strings from project
         50▕         foreach ($directories as $dir) {
         51▕             if (!is_dir($dir)) {
      ➜  52▕                 throw new \Exception('Folder "' . $dir . '" doest not exists. Gettext scan aborted.');
         53▕             }
         54▕
         55▕             foreach ($this->scanDir($dir) as $file) {
         56▕                 if (strstr($file, '.blade.php')) {
    
          +18 vendor frames
      19  /var/www/project/artisan:37
          Illuminate\Foundation\Console\Kernel::handle()
    

    The config which directories to scan is unchanged with 'gettext_parse_paths' => ['app', 'resources'],

    opened by smares 7
  • Allow subdirectories for translation PHP files

    Allow subdirectories for translation PHP files

    Accept the Laravel 5 subdirectories structure: {{ trans('foo/bar/foo.key') }}

    More info here: https://laravel.io/forum/02-23-2015-localization-load-files-from-subdirectories-at-resourceslanglocale

    (with more tests for sync and sourceEdits)


    I also debugged the SourceEditException that should have been SourceEditSyncException, and some behaviour that was wrong (sourceEdit must been executed before Sync to deal with more situations).

    I hope the changes are ok with you.


    Next, I'll find a way to use the Gettext syntax.

    I'm still not sure if I'll use eusonlito/laravel-Gettext or build something myself with oscarotero/Gettext. Any thoughts?

    opened by MichaelHoste 7
  • Allows source edits from Translation.io

    Allows source edits from Translation.io

    Hello @armandsar,

    Here is a pull request to allow source edits from Translation.io (read here for more info: https://translation.io/blog/new-feature-copywriting)

    The main idea is to change source text AND translations from a centralised place. It allows copywriting from the marketing/management team without annoying developers.

    Also, I added a snippet to pretty print translation files. So they are easier to read.

    If you have any comment on the produced code, please tell me. As you know, I'm not very confortable with PHP since that's not my language of choice.

    I think this package is already mature enough to be distributed to the Laravel community. I intend to create https://laravel.translation.io in the following month(s) and start to look for beta-testers.

    So you know, I think the package still need to deal with special translation management of Laravel >5 with sub-folders and keys like "this/is/repository.and.here.are.keys". But I doubt I lot of users use that feature so it can certainly wait.

    I added my name as author on the composer file, I hope you don't mind.

    Oh, and Happy Christmas ! 🎅 🎄

    opened by MichaelHoste 5
  • Update README file

    Update README file

    Hello @armandsar,

    I updated the README file for more consistency between https://github.com/aurels/translation-gem and https://github.com/armandsar/laravel-translationio/

    I hope you'll like it that way. Feel free to amend it!

    You'll notice that I removed --dev from the composer require command. If I'm not wrong, we need to require the package in production in order to be able to use GetText syntaxes like t__ and n__.

    FYI now that the package seems ready, I will create a specific landing page for Laravel (https://translation.io/laravel) and start a beta-test period to gather feedbacks (I'll reach the Laravel community). I'll keep you in touch.

    Thanks again for your support!

    I just increased your trial period on Translation.io until 2050. Feel free to create as many free Rails/Laravel projects as you want ;-)

    opened by MichaelHoste 4
  • Laravel 9 upgrade

    Laravel 9 upgrade

    In reading the Laravel 9 upgrade, they are moving the lang directory from resources/lang to the project directory. Are you going to implement this change?

    opened by rbruhn 3
  • php artisan translation:init suceeds, php artisan translation:sync tells me to init first

    php artisan translation:init suceeds, php artisan translation:sync tells me to init first

    After I init the translation (seemingly successfully, see output below), the backend still shows the "setup" page. If i try to php artisan translation:sync it tells me: {"error":"This project has not been initialized yet. Please use the \"init\" operation."} wven though I already initialized.

    My translation.php

    <?php
    
    return [
        'key' => env('TRANSLATIONIO_KEY'),
        'source_locale' => 'de-CH',
        'target_locales' => ['fr-FR'],
    
        /* Directories to scan for Gettext strings */
        'gettext_parse_paths' => ['app', 'resources'],
    
        /* Where the Gettext translations are stored */
        'gettext_locales_path' => 'resources/lang/gettext'
    ];
    

    The key is in my .env

    How i start the initialization: $ docker-compose exec app php artisan translation:init

    The output I get

    Init started
    Init ended with success
    ----------
    Use this URL to translate: https://translation.io/sandrotanner/my-project-name
    ----------
    
    

    Do your logs give any insight on what might go wrong? Thanks in advance :)

    BTW I use Laravel 8 and the official php docker image "php:8.0-fpm"

    opened by sandrotanner 3
  • File that is mentioned in 'ignored_key_prefixes' is removed after sync

    File that is mentioned in 'ignored_key_prefixes' is removed after sync

    Hi! I am using the v1.12 of your package, and PHP 7.3.23.

    I have a translation.php file as follows:

    	...
    
    	'source_locale' => 'de',
    
        'target_locales'       => ['en'],
    
        /*
        |--------------------------------------------------------------------------
        | # Ignored PHP key prefixes.
        |--------------------------------------------------------------------------
        |
        | Ex: * 'validation':        ignore the whole validation.php file.
        |     * 'validation.custom': ignore the "custom" subtree in validation.php file.
        |     * 'subfolder/more':    ignore the whole subfolder/more.php file.
        |
        */
        'ignored_key_prefixes' => ['legacy', 'validation'],
    
    	...
    

    So, I want to translate from german to english. The validation.php file is only available in the en, but not in the de folder (as I just need this file for internalt tests). So I have some structure like this:

    de/
    	file1.php
    	file2.php
    en/
    	file1.php
    	validation.php
    

    But although validation is mentioned in the ignored_key_prefixes, the en/validation.php file is removed after I call php artisan translation:sync. My structure after syncing is:

    de/
    	file1.php
    	file2.php
    en/
    	file1.php
    	file2.php
    

    If I understand correctly, the file en/validation.php should not be touched, because it is ignored, so I think I found a bug here?

    opened by stefanschindler 3
  • Strings inside blade attributes not recognized

    Strings inside blade attributes not recognized

    We've encountered an issue where having a blade component with a translated string as a attribute. Example code below:

    <x-form-row :label="t('Name')"></x-form-row>

    This unfortunately won't get picked up when running the sync command. There are ways around this obviously but I just wondered if anyone has an idea on if this could be solved?

    opened by antonhedling 1
  • Default locale independent to translation setup

    Default locale independent to translation setup

    This PR makes application locale set in config('app.locale') independent to translation setup. This way one can set any default language he/she wants when working locally or in a server while syncing with translation.io the way defined in config('translation').

    Examples:

    • Working locally with Greek locale (set in config('app.locale')) enabled, using gettext/json strings in english, while syncing with source language English and target languages Greek and Polish
    • Auto-deploying in 2 different application servers with default languages Polish and Greek respectively without any change in translation setup

    This shouldn't break previous behavior as long as the config('app.locale')) == config('translation.source_locale'), while giving the flexibility mentioned above.

    Having English as source_locale even someone is not using them at all (best practice for easy translations in any language) is also possible through this PR

    opened by nikosv 1
  • How do I add new segments to a pre-existing project?

    How do I add new segments to a pre-existing project?

    Hello,

    This isn't a bug report but simply a question. We've been using this package for a while now and everything works well. However I need to add a couple of hundred new translations (that I already have as .php files). However, when I run the sync command, I only get the source texts imported. All my translation files are overwritten with empty data from translation.io. I guess this is the way it's intended since it seems to use translation.io as a "source of truth".

    But still, I don't really have the time to add all these manually. Is there a way to sort of "re-init" or sync with the codebase as the source? Or is the only solution to create a new project from scratch?

    question 
    opened by antonhedling 1
  • Working with variable translation keys

    Working with variable translation keys

    Hello,

    I'm wondering if there is a way to register translation keys that come from other sources rather than being hardcoded. Example use case is having a set of data (e.g. API or local constants), which need to be translated, but they would need to be changed over time.

    I would envision a need for a separate scanning tool, that would require a script to be fully functional and therefore would be able to read variables/constants runtime.

    Use example:

    class User  {
        const ROLE_ADMIN = 'admin';
    }
    
    //dynamic-translations.php
    rtp('enums.user-roles', User::ROLE_ADMIN);
    
    //config/translation.php
    'scan_runtime' => [
        'runnable-file.php'
    ];
    
    //Result:
    msgctxt "enums.user-roles"
    msgid "admin"
    msgstr ""
    

    If one wants to change the value of ROLE_ADMIN to let's say, administrator, it would need to be done in just one place. In theory, is this something that you would be eager to have as a part of your package?

    opened by sybbear 4
  • remove array-align

    remove array-align

    this PR optionally uses https://github.com/themsaid/laravel-langman for dumping the translations to ensure compatibility of the formats

    also handle case when a JSON file is invalid

    opened by lsmith77 1
Releases(v1.21)
Better translation management for Laravel

Better localization management for Laravel Introduction Keeping a project's translations properly updated is cumbersome. Usually translators do not ha

Waavi 354 Dec 18, 2022
🎌 Laravel Localization Helper :: Easily add translation variables from Blade templates.

LocalizationHelper Package for convenient work with Laravel's localization features and fast language files generation. Take a look at contributing.md

Awes.io 36 Jul 18, 2022
Manage Laravel translation files

Laravel 5 Translation Manager For Laravel 4, please use the 0.1 branch! This is a package to manage Laravel translation files. It does not replace the

Barry vd. Heuvel 1.5k Jan 4, 2023
A GUI for managing JSON translation files in your laravel projects.

Laravel Language Manager Langman is a GUI for managing your JSON language files in a Laravel project. Installation Begin by installing the package thr

Mohamed Said 515 Nov 30, 2022
A Gui To Manage Laravel Translation Files

Lingo A file based translation manager, which unlike other Lang managers don't need a database connection to handle the translation. Installation comp

Muah 97 Dec 5, 2022
[virion] Language management library for automatic translation

libtranslator :: library for automatic translation ✔️ Multilingual support for plugin messages ✔️ Translation language is set according to the player

PocketMine-MP projects of PresentKim 5 Jul 29, 2022
Provides support for message translation and localization for dates and numbers.

The I18n library provides a I18n service locator that can be used for setting the current locale, building translation bundles and translating messages. Additionally, it provides the Time and Number classes which can be used to output dates, currencies and any numbers in the right format for the specified locale.

CakePHP 26 Oct 22, 2022
Internationalization tools, particularly message translation.

Aura.Intl The Aura.Intl package provides internationalization (I18N) tools, specifically package-oriented per-locale message translation. Installation

Aura for PHP 86 Dec 18, 2022
The Translation component provides tools to internationalize your application.

Translation Component The Translation component provides tools to internationalize your application. Getting Started $ composer require symfony/transl

Symfony 6.4k Jan 6, 2023
Filament Translations - Manage your translation with DB and cache

Filament Translations Manage your translation with DB and cache, you can scan your languages tags like trans(), __(), and get the string inside and tr

Fady Mondy 32 Nov 28, 2022
Official PHP library for the DeepL language translation API.

deepl-php Official PHP client library for the DeepL API. The DeepL API is a language translation API that allows other computer programs to send texts

DeepL 78 Dec 23, 2022
French-Traduction-Pterodactyl est la traduction française de pterodactyl French-Traduction-Pterodactyl is the French translation of pterodactyl

French-Traduction-Pterodactyl Star French-Traduction-Pterodactyl est la traduction française de pterodactyl French-Traduction-Pterodactyl is the Frenc

null 5 Sep 26, 2022
Translation (i18n) Manager as a virion

TL Translation (i18n) Manager as a virion Translation use hook-like $t = $tl->useTranslation($player->getLocale()); $player->sendMessage($t("message-k

RedMC Network 3 Oct 28, 2022
Composer package providing translation features for PHP apps

PHP translation This is a composer package providing translation support for PHP applications. It is similar to gettext, in usage, with these differen

Sérgio Carvalho 0 Aug 15, 2022
List of 77 languages for Laravel Framework 4, 5, 6, 7 and 8, Laravel Jetstream , Laravel Fortify, Laravel Cashier and Laravel Nova.

Laravel Lang In this repository, you can find the lang files for the Laravel Framework 4/5/6/7/8, Laravel Jetstream , Laravel Fortify, Laravel Cashier

Laravel Lang 6.9k Dec 29, 2022
FBT - a internationalization framework for PHP designed to be not just powerful and flexible, but also simple and intuitive

FBT is an internationalization framework for PHP designed to be not just powerful and flexible, but also simple and intuitive. It helps with the follo

Richard Dobroň 4 Dec 23, 2022
75 languages support for Laravel 5 application based on Laravel-Lang/lang.

Laravel-lang 75 languages support for Laravel 5 application based on Laravel-Lang/lang. Features Laravel 5+ && Lumen support. Translations Publisher.

安正超 1.3k Jan 4, 2023
Easy localization for Laravel

Laravel Localization Easy i18n localization for Laravel, an useful tool to combine with Laravel localization classes. The package offers the following

Marc Cámara 3k Jan 4, 2023
[Deprecated] A Laravel package for multilingual models

This package has been deprecated. But worry not. You can use Astrotomic/laravel-translatable. Laravel-Translatable If you want to store translations o

Dimitris Savvopoulos 2k Dec 25, 2022