Skip to content

Commit 40a72d0

Browse files
committed
Initial commit
1 parent 16c12bb commit 40a72d0

File tree

7 files changed

+195
-2
lines changed

7 files changed

+195
-2
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.idea
2+
vendor
3+
composer.lock
4+
_ide_helper.php
5+
*.sublime-*

README.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,51 @@
1-
# laravel-javascript-lang
2-
Laravel package that exposes translations and routes to JavaScript
1+
# Laravel JavaScript Lang
2+
3+
[![Packagist](https://img.shields.io/packagist/v/Pod-Point/laravel-javascript-lang.svg)](https://packagist.org/packages/pod-point/laravel-javascript-lang)
4+
5+
A Laravel package that exposes translations to JavaScript.
6+
7+
## Installation
8+
9+
Require the package in composer:
10+
11+
```javascript
12+
"require": {
13+
"pod-point/laravel-javascript-lang": "^1.0"
14+
},
15+
```
16+
17+
Add the service provider to your `config/app.php` providers array:
18+
19+
```php
20+
'providers' => [
21+
PodPoint\JsLang\Providers\ServiceProvider::class
22+
]
23+
```
24+
25+
Then finally, publish the config files:
26+
27+
```php
28+
php artisan vendor:publish --provider=PodPoint\JsLang\Providers\ServiceProvider
29+
```
30+
31+
## Usage
32+
33+
Now the varable `$jslang` is available in your view. We recommend attaching it to a data tag on your body element:
34+
35+
```html
36+
<body data-jslang='{{ $jslang }}'>
37+
```
38+
39+
There is a provided JavaScript module with a helper method you can use to retrieve the translations:
40+
41+
```js
42+
import jsLang from '../lib/jslang';
43+
44+
const string = jsLang.get('orders.form.error');
45+
```
46+
47+
Or you can get the data yourself:
48+
49+
```js
50+
JSON.parse(document.body.getAttribute('data-jslang'))
51+
```

assets/jsLang.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export default {
2+
3+
/**
4+
* Fetches a translation by key if it exists.
5+
*
6+
* @param form
7+
*/
8+
get: key => {
9+
const messages = JSON.parse(document.body.getAttribute('data-jslang'));
10+
11+
if (typeof messages[key] === 'undefined') {
12+
return key;
13+
}
14+
15+
return messages[key];
16+
},
17+
};

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "pod-point/laravel-javascript-lang",
3+
"description": "Laravel package that exposes translations and routes to JavaScript",
4+
"keywords": ["laravel", "javascript", "language", "translations", "routes"],
5+
"type": "library",
6+
"require": {
7+
"php": ">=5.5.9",
8+
"illuminate/support": "5.1.*|5.2.*|5.3.*|5.4.*|5.5.*"
9+
},
10+
"license": "MIT",
11+
"autoload": {
12+
"psr-4": {
13+
"PodPoint\\JsLang\\": "src/"
14+
}
15+
},
16+
"extra": {
17+
"laravel": {
18+
"providers": [
19+
"PodPoint\\JsLang\\Providers\\ServiceProvider"
20+
]
21+
}
22+
}
23+
}

config/javascript.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Javascript Language Strings
8+
|--------------------------------------------------------------------------
9+
|
10+
| An array of language strings that should be available to JS via the
11+
| jslang module.
12+
|
13+
*/
14+
15+
'lang' => [
16+
'validation',
17+
],
18+
19+
];

src/Providers/ServiceProvider.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace PodPoint\JsLang\Providers;
4+
5+
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
6+
use Illuminate\Contracts\View\Factory;
7+
use PodPoint\JsLang\ViewComposers\ViewComposer;
8+
9+
class ServiceProvider extends LaravelServiceProvider
10+
{
11+
/**
12+
* Bootstrap the application events.
13+
*
14+
* @param Factory $view
15+
* @return void
16+
*/
17+
public function boot(Factory $view)
18+
{
19+
$this->publishes([
20+
__DIR__ . '/../config/javascript.php' => config_path('javascript.php'),
21+
]);
22+
23+
$view->composer('*', ViewComposer::class);
24+
}
25+
}

src/ViewComposers/ViewComposer.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace PodPoint\JsLang\ViewComposers;
4+
5+
use Illuminate\Contracts\View\View;
6+
7+
class ViewComposer
8+
{
9+
/**
10+
* Binds the data to the view.
11+
*
12+
* @param View $view
13+
*/
14+
public function compose(View $view)
15+
{
16+
$keys = config('javascript.lang');
17+
$data = [];
18+
19+
foreach ($keys as $value) {
20+
$trans = trans($value);
21+
22+
if (is_array($trans)) {
23+
$data = array_merge($data, $this->transArray($trans, $value));
24+
} else {
25+
$data[$value] = trans($value);
26+
}
27+
}
28+
29+
$view->with([
30+
'jslang' => json_encode($data)
31+
]);
32+
}
33+
34+
/**
35+
* Translates an array of options with a prefix
36+
*
37+
* @param array $array
38+
* @param string $prefix
39+
* @return array
40+
*/
41+
private function transArray(array $array, $prefix)
42+
{
43+
$data = [];
44+
45+
foreach ($array as $key => $value) {
46+
if (is_array($value)) {
47+
$data = array_merge($data, $this->transArray($value, $prefix . '.' . $key));
48+
} else {
49+
$data[$prefix . '.' . $key] = trans($value);
50+
}
51+
}
52+
53+
return $data;
54+
}
55+
}

0 commit comments

Comments
 (0)