Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/developersunesis/lang2js

A package that provides an easy way to export and sync Laravel localization files for JavaScript use
https://github.com/developersunesis/lang2js

Last synced: about 1 month ago
JSON representation

A package that provides an easy way to export and sync Laravel localization files for JavaScript use

Awesome Lists containing this project

README

        

Welcome to lang2js 👋



Version

Documentation


License: MIT


Twitter: developrsunesis

> A package that provides an easy way to export and sync Laravel localization files for JavaScript use
## Problem
I have a Laravel project/website, while thinking of how to sync my localization and translation files from the app
for JavaScript usage; my first approach was to have the localization content stored in the `localStorage` of the browser
when a user first visits the app. While this was a goto solution, I realized this wasn't efficient enough, because it meant
that the first page the user visits might not have its strings translated until the localization is available
already in the `localStorage`. Another solution, was to inject the localization content directly into a `DOMElement`:
``, clearly there was a trade-off as this drastically increased the page load time but solves the
problem of the translations not being available.

#### Final Solution
My final solution which is currently in use was to have a package periodically sync the localization files for JavaScript use.
The package reads the following files:
```
resources
├── lang
│ ├── en
│ │ ├── auth.php
│ │ └── dashboard.php
│ ├── fr
│ │ ├── auth.php
│ │ └── dashboard.php
```
and converts it to minified js files
```
public
├── js
│ ├── locales
│ │ ├── en.min.js
│ │ ├── fr.min.js
│ │ └── lang2js.min.js
```
So each locale that needs to be used is imported into my blade component
```html
...





let helloText = __("index.TEST_2", 'en') // this function is provided by `lang2js.min.js`
document.getElementById("hellotext").innerHTML = helloText

...
```

## Install

```sh
composer require developersunesis/lang2js
```

## Usage
You can simply run a command
```sh
php artisan lang2js:export exportDir=:exportDir
```
The command above reads the translation files from Laravel default lang folder.


But if you have a custom location you want the translation files to be read from, you can use the following
```shell
php artisan lang2js:export exportDir=:exportPath localesDir=:localesPath
```
The two commands above uses the base path of the app and the path you specified as their absolute path.

Example:
```shell
php artisan lang2js:export exportDir=/public/js/locales localesDir=/resources/lang

# Uses full path
# exportDir == {YOUR_CURRENT_APP_LOCATION}/public/js/locales
# localesDir == {YOUR_CURRENT_APP_LOCATION}/public/resources/lang
```
To disable to command from using your base app file, you can add an option to the command as below
```shell
php artisan lang2js:export exportDir=C:/manners/Documents/public/js/locales localesDir=C:/manners/Documents/resources/lang --useBasePath=false
```
There are various use cases, one of which is to create a schedule for the package to resync the JavaScript translations
periodically, this is very useful if you make use of laravel localizations that can be dynamically changed
```php
$command = "php artisan lang2js:export exportDir=/public/js/locales"
$schedule->command($command)
->weekdays()
->daily();

# or through a facade function call
$schedule->call(function () {
$lang2js = new Lang2js();
$lang2js->setExportsDir("resources/exports");
$lang2js->export();
})->weekly()->daily();

# or through a facade function call
$schedule->call(function () {
L2J::setExportsDir("/public/js/locales")->export();
})->weekly()->daily();
```

## Author

👤 **Uche Emmanuel**

* Website: https://developersunesis.com
* Twitter: [@developrsunesis](https://twitter.com/developrsunesis)
* Github: [@developersunesis](https://github.com/developersunesis)
* LinkedIn: [@developersunesis](https://linkedin.com/in/developersunesis)

## 🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check [issues page](https://github.com/developersunesis/lang2js/issues).

## Show your support

Give a ⭐️ if this project helped you!

***
_This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_