Skip to content
This repository was archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
Init Package
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafakhaleddev committed Jul 29, 2018
0 parents commit 5387927
Show file tree
Hide file tree
Showing 8 changed files with 309 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Mustafa Khaled

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# AWT Translation

![AWT](https://mustafakhaled.com/images/awt.png)

Laravel Awesome Translation Helper using Google Translation
```php
// Generate translation file based in current app locale
awtTrans('Hello World !')

```

## Installation

Require this package, with [Composer](https://getcomposer.org/), in the root directory of your project.

```bash
$ composer require mkhdev/awt
```

Add the service provider to `config/app.php` in the `providers` array, or if you're using Laravel 5.5, this can be done via the automatic package discovery.

```php
mkhdev\AWT\AWTServiceProvider::class,
```


## Documentation

### Helper Function
You can use helper function to get the trans key or generate it if not found
```php
awtTrans('Hello World !')
```
In view you can use it like this
```php
{{awtTrans('Hello World !')}}
```
### Blade Directives
You can use our blade directive for fast translation
```php
@awt('Hello World !')
```

## License

[MIT](LICENSE) © [Mustafa Khaled](https://github.com/Dirape)
43 changes: 43 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "mkhdev/awt",
"description": "Awesome Translation For Laravel",
"authors": [
{
"name": "MustafaKhaled",
"email": "[email protected]"
}
],
"keywords": [
"laravel",
"translate",
"localization",
"translation",
"language",
"laravel localization",
"localization",
"laravel multi language",
"multi language",
"multilingual",
"multilingual support",
"laravel lang"
],
"license": "MIT",
"require": {
"stichoza/google-translate-php": "^3.2"
},
"autoload": {
"psr-4": {
"mkhdev\\AWT\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"mkhdev\\AWT\\AWTServiceProvider"
],
"aliases": {
}
}
},
"minimum-stability": "stable"
}
85 changes: 85 additions & 0 deletions src/AWTClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Created by PhpStorm.
* User: DevaGo
* Date: 7/29/2018
* Time: 12:36 PM
*/

namespace mkhdev\AWT;


use Illuminate\Support\Facades\Blade;

class AWTClass
{

/**
* Register the blade.
*
* @param array $directives
* @return void
*/
public static function register(array $directives)
{
collect($directives)->each(function ($item, $key) {
Blade::directive($key, $item);
});
}

/**
* Open AWT Lang file
*
* @param $AwtFile
* @return bool|resource
*/
public static function openAwtLangFile($AwtFile, $locale)
{
if (!file_exists($AwtFile)) {
$file = self::createAwtLangFile($AwtFile, $locale);

return $file;
}
$file = file($AwtFile);
return $file;
}


/**
* Create AWT Lang File
* @param $AwtFile
* @return bool|resource
*/
public static function createAwtLangFile($AwtFile, $locale)
{
if (!file_exists(resource_path('lang/' . $locale))) {
mkdir(resource_path('lang/' . $locale), 0777, true);
}
$file = copy(__DIR__ . '/stubs/blank.stub', $AwtFile);
return $file;
}


/**
* Push The Word To AWT Array File
*
* @param $word
* @param $translate
* @param $AwtFile
*/
public static function pushWord($word, $translate, $AwtFile)
{
$lines = array();
foreach (file($AwtFile) as $line) {
if (strpos($line, '#AWTLINEHELPER') !== false) {
array_push($lines, '"' . str_replace(' ', '', strtolower($word)) . '"=>"' . $translate . '",');
array_push($lines, "\n");

}

array_push($lines, $line);
}
file_put_contents($AwtFile, $lines);
}

}
35 changes: 35 additions & 0 deletions src/AWTHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

if (!function_exists('awtTrans')) {

/**
* AWT Trans Helper
*
* @param $word
* @param null $locale
* @return array|\Illuminate\Contracts\Translation\Translator|null|string
* @throws Exception
*/
function awtTrans($word, $locale = null)
{
if ($locale == null) {
$locale = App::getLocale();
}
$AwtFile = resource_path('lang/' . $locale . '/awt.php');
if (file_exists($AwtFile)) {
if (Lang::has('awt.' . str_replace(' ', '', strtolower($word)), $locale)) {
$word = str_replace(' ', '', strtolower($word));
return trans('awt.' . $word);
}
}
$langFile = \mkhdev\AWT\AWTClass::openAwtLangFile($AwtFile, $locale);
if ($langFile) {
$tr = new \Stichoza\GoogleTranslate\TranslateClient();
$wordT = $tr->setSource(null)->setTarget($locale)->translate($word);
\mkhdev\AWT\AWTClass::pushWord($word, $wordT, $AwtFile);
return $wordT;
}
$word = str_replace(' ', '', strtolower($word));
return trans('awt.' . $word);
}
}
46 changes: 46 additions & 0 deletions src/AWTServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace mkhdev\AWT;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

class AWTServiceProvider extends ServiceProvider
{

/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{

$this->registerDirectives();

}

/**
* Register the application services.
*
* @return void
*/
public function register()
{


}


/**
* Register AWT blade directives.
*
* @return void
*/
public function registerDirectives()
{
require __DIR__.'/AWTHelper.php';
$directives = require __DIR__.'/awtBlade.php';
AWTClass::register($directives);
}
}
12 changes: 12 additions & 0 deletions src/awtBlade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
use mkhdev\AWT\AWTClass;
return [
/*
|---------------------------------------------------------------------
| @awt(string)
|---------------------------------------------------------------------
*/
'awt' => function ($expression) {
return "<?php echo awtTrans($expression) ?>";
}
];
21 changes: 21 additions & 0 deletions src/stubs/blank.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Awesome Language Package (AWT)
|--------------------------------------------------------------------------
|
| The following language lines are used during processing for various
| messages that we need to display to the user. You are free to modify
| these language lines according to your application's requirements.
| but you have to know that`s those lines are dynamically created by out package
|
*/


#don`t Remove the next line at all
#AWTLINEHELPER

];

0 comments on commit 5387927

Please sign in to comment.