PHP Classes
elePHPant
Icontem

Laravel Localization Package to Vue: Get localization texts in array and JSON formats

Recommend this page to a friend!
  Info   View files Example   View files View files (11)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-08-23 (1 year ago) RSS 2.0 feedNot yet rated by the usersTotal: 32 All time: 9,718 This week: 330Up
Version License PHP version Categories
laravel-localization 1.0MIT/X Consortium ...7Localization, Utilities and Tools, PHP 7
Description Author

This package can get localization texts in array and JSON formats.

It comes with a service provider that can retrieve application texts according to given configuration and return the texts in multiple formats

The package supports formats like PHP arrays or JSON strings suitable for use in JavaScript applications like those built using the Vue JavaScript library.

Name: Stefan Ninic is available for providing paid consulting. Contact Stefan Ninic .
Classes: 5 packages by
Country: Bosnia and Herzegovina Bosnia and Herzegovina
Age: ???
All time rank: 40567 in Bosnia and Herzegovina Bosnia and Herzegovina
Week rank: 64 Up2 in Bosnia and Herzegovina Bosnia and Herzegovina Up
Innovation award
Innovation award
Nominee: 3x

 

Details

Latest Stable Version Total Downloads Latest Unstable Version License Monthly Downloads Daily Downloads

Laravel Localization To Vue/JSON

This package collects all localizations from resources/lang directory and it's sub-directories and converts them to plain array which can later be converted to JSON object and used with libraries like Vue, Angular, etc.

Installing

Just require this package with composer.

composer require kg-bot/laravel-localization-to-vue

Laravel 5.5+

Laravel 5.5 uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.

If you don't use auto-discovery, add the ServiceProvider to the providers array in config/app.php

KgBot\LaravelLocalization\LaravelLocalizationServiceProvider::class

and if you want alias add this inside aliases array in config/app.php

"ExportLocalization" => "KgBot\\LaravelLocalization\\Facades\\ExportLocalizations"

Settings and configuration

You can export config by running

php artisan vendor:publish --provider=KgBot\LaravelLocalization\LaravelLocalizationServiceProvider --tag=config

Usage

This package can be used in multiple ways, I'll give examples for some of them, but there's really no limitation.

First example would be to add view composed variable and use it in blade views.

// inside ServiceProvider

// With alias
use ExportLocalization;

// Without alias
use KgBot\LaravelLocalization\Facades\ExportLocalizations as LaravelLocalization;


View::composer( 'view.file', function ( $view ) {

    return $view->with( [
        'messages' => ExportLocalization::export()->toArray(),
    ] );
} );

Second way would be to request it over HTTP just like any other file

<script>
let messages = axios.get('http://localhost/js/lang.js') // This is default route which can be changed in config
</script>

You can also export messages to ECMAScript 6 standard JavaScript module with artisan command


## Some examples why would you use this package and messages over Laravel standard localization

```
// Inside blade view
<script>
    window.default_locale = "{{ config('app.locale') }}";
    window.fallback_locale = "{{ config('app.fallback_locale') }}";
    window.messages = @json($messages);
</script>

// And optionaly you can then use it in any JavaScript file or Vue.js component

// app.js
import Vue from 'vue';
import Lang from 'lang.js';

const default_locale = window.default_language;
const fallback_locale = window.fallback_locale;
const messages = window.messages;

Vue.prototype.trans = new Lang( { messages, locale: default_locale, fallback: fallback_locale } );

// Example.vue
<b-input v-model="query"
                 type="text"
                 :placeholder="trans.get('search.placeholder')"></b-input>
``` 

## Routing

This package exposes one route `http://localhost/js/lang.js` by default but you can change the prefix to anything you whish in config file.  

You can also have a nice route name for blade templates or any other route calls, it's `route('assets.lang')` by default but it's customizable by config/environment file.

## Proposals, comments, feedback

Everything of this is highly welcome and appreciated

## To-Do

+ Create exclude configuration so not files/directories are collected

Anything else you can think of please leave me comments, mail me, create issue, whatever you prefer.

## License

This project is licensed under the MIT License - see the LICENSE.md file for details
  Files folder image Files  
File Role Description
Files folder imagesrc (2 files, 5 directories)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageClasses (1 file)
Files folder imageconfig (1 file)
Files folder imageConsole (1 directory)
Files folder imageEvents (1 file)
Files folder imageFacades (1 file)
  Plain text file LaravelLocalizationServiceProvider.php Class Class source
  Accessible without login Plain text file routes.php Example Example script

  Files folder image Files  /  src  /  Classes  
File Role Description
  Plain text file ExportLocalizations.php Class Class source

  Files folder image Files  /  src  /  config  
File Role Description
  Accessible without login Plain text file laravel-localization.php Aux. Auxiliary script

  Files folder image Files  /  src  /  Console  
File Role Description
Files folder imageCommands (2 files)

  Files folder image Files  /  src  /  Console  /  Commands  
File Role Description
  Plain text file ExportMessages.php Class Class source
  Plain text file ExportMessagesToFlat.php Class Class source

  Files folder image Files  /  src  /  Events  
File Role Description
  Plain text file LaravelLocalizationExported.php Class Class source

  Files folder image Files  /  src  /  Facades  
File Role Description
  Plain text file ExportLocalizations.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:32
This week:0
All time:9,718
This week:330Up