2017 © Pedro Peláez
 

library laravel-database-translation

A Laravel package to load translation strings from database with fallback to file system

image

mike-zange/laravel-database-translation

A Laravel package to load translation strings from database with fallback to file system

  • Friday, June 29, 2018
  • by MikeZange
  • Repository
  • 1 Watchers
  • 2 Stars
  • 25 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 9 Versions
  • 0 % Grown

The README.md

StyleCI Scrutinizer Code Quality Build Status, (*1)

Laravel Database Translations

For Laravel <= 5.4 use version 0.2.x, (*2)

For Laravel >= 5.5 use version 1.x, (*3)

Description

This package extends the functionality of spatie/laravel-translatable and will allow you to manage your translation strings in the database while using the file based translations as a fallback., (*4)

The fallback priority is as follows:, (*5)

Database current locale -> Database fallback locale -> File system current locale -> File system fallback locale, (*6)

This is intentional as this package is meant to be a replacement for the file based language strings, (*7)

Usage

You can use the Laravel trans('group.key') or __('group.key') helpers to display your translations., (*8)

The blade @lang('key') directive also behaves the same way., (*9)

Namespacing works the same as before too, (*10)

In a Service Provider app('translator')->addNamespace('namespace', 'path/to/namespaced/folder/lang');, (*11)

and trans('namespace::group.key'), __('namepace::group.key') or @lang('namepace::group.key') to display, (*12)

By default the translations are cached for 1 day with the key {$locale}.{$namespace}.{$group}, caching can be disabled in the config file., (*13)

The cache for a translation group is refreshed when one of the lines is updated via the translation repository., (*14)

Importing translation files

To load your current translation files in to the database run php artisan trans-db:load {locale} where {locale} is a language code, (*15)

For example php artisan trans-db:load en will load all the files for 'en' and php artisan trans-db:load es will load all the files for 'es', (*16)

Note: this is a catch all function and will include any files in language namespaces you have added to your service providers as well as any in resources/lang, (*17)

Adding more translation keys

The included utility command php artisan trans-db:load is non-destructive so add your new key to the appropriate translation file and run the command again, your new key will appear in the database whilst preserving your modified translations, (*18)

Working with translations

Add: use MikeZange\LaravelDatabaseTranslation\Repositories\TranslationRepository; and inject it into your __construct() or method., (*19)

  1. To retrieve all of the translations from the database: $translationRepository->all($related = [], $perPage), (*20)

  2. Get a group of keys by namespace and group: $translationRepository->getItems($namespace, $group), (*21)

  3. Get a specific key $translationRepository->getItem($namespace, $group, $key), (*22)

There are 2 included methods for updating translations via the translations repository., (*23)

  1. $translationRepository->updateTranslationById($id, $locale, $value, $overwrite), (*24)

  2. $translationRepository->updateTranslation(Translation $line, $locale, $value, $overwrite), (*25)

The second method requires an instance of the translation model., (*26)

The optional 4th parameter controls whether the new translation will overwrite the old one if it exists, default is true, (*27)

Requirements

  • PHP >=7.0
  • Laravel ~5.5
  • spatie/laravel-translatable ^2.1

Installation

  1. As always: back up your database - I am not responsible for any data loss, (*28)

  2. Install the package via Composer:, (*29)

    composer require mike-zange/laravel-database-translation, (*30)

  3. Comment the following Service Provider in config/app.php:, (*31)

    Illuminate\Translation\TranslationServiceProvider::class,, (*32)

    The new service provider is auto-loaded in laravel 5.5, (*33)

  4. Publish the configuration file database.translations.php, (*34)

    php artisan vendor:publish --provider="MikeZange\LaravelDatabaseTranslation\TranslationServiceProvider", (*35)

  5. Make sure you have a locale set either via app()->setLocale() or in config/app.php, (*36)

  6. Edit the config at config/database.translations.php to your requirements, the defaults should be okay for most uses, (*37)

  7. Run php artisan migrate to create the database table, (*38)

To Do

  • Allow configuration of caching time

The Versions

29/06 2018

dev-master

9999999-dev

A Laravel package to load translation strings from database with fallback to file system

  Sources   Download

MIT

The Requires

 

by Mike Zange

database laravel language translation

30/01 2018

dev-develop

dev-develop

A Laravel package to load translation strings from database with fallback to file system

  Sources   Download

MIT

The Requires

 

by Mike Zange

database laravel language translation

30/01 2018

1.0.1

1.0.1.0

A Laravel package to load translation strings from database with fallback to file system

  Sources   Download

MIT

The Requires

 

by Mike Zange

database laravel language translation

30/01 2018

dev-analysis-zGm629

dev-analysis-zGm629

A Laravel package to load translation strings from database with fallback to file system

  Sources   Download

MIT

The Requires

 

by Mike Zange

database laravel language translation

30/01 2018

1.0.0

1.0.0.0

A Laravel package to load translation strings from database with fallback to file system

  Sources   Download

MIT

The Requires

 

by Mike Zange

database laravel language translation

30/01 2018

0.2.2

0.2.2.0

A Laravel package to load translation strings from database with fallback to file system

  Sources   Download

MIT

The Requires

 

by Mike Zange

database laravel language translation

10/05 2017

0.2.1

0.2.1.0

A Laravel package to load translation strings from database with fallback to file system

  Sources   Download

MIT

The Requires

 

by Mike Zange

database laravel language translation

22/03 2017

0.2.0

0.2.0.0

A Laravel package to load translation strings from database with fallback to file system

  Sources   Download

MIT

The Requires

 

by Mike Zange

database laravel language translation

22/03 2017

0.1.0

0.1.0.0

A Laravel package to load translation strings from database with fallback to file system

  Sources   Download

MIT

The Requires

 

by Mike Zange

database laravel language translation