blessing-skin-server/app/Services/Translations/JavaScript.php

59 lines
1.7 KiB
PHP
Raw Normal View History

2019-09-07 17:18:58 +08:00
<?php
namespace App\Services\Translations;
use Illuminate\Cache\Repository;
use Illuminate\Filesystem\Filesystem;
class JavaScript
{
/** @var Filesystem */
protected $filesystem;
/** @var Repository */
protected $cache;
protected $prefix = 'front-end-trans-';
2019-09-07 21:23:38 +08:00
public function __construct(Filesystem $filesystem, Repository $cache)
2019-09-07 17:18:58 +08:00
{
$this->filesystem = $filesystem;
$this->cache = $cache;
}
public function generate(string $locale): string
{
$source = resource_path("lang/$locale/front-end.yml");
$compiled = public_path("lang/$locale.js");
$sourceModified = $this->filesystem->lastModified($source);
$compiledModified = intval($this->cache->get($this->prefix.$locale, 0));
2019-12-14 11:10:37 +08:00
if ($sourceModified > $compiledModified || !$this->filesystem->exists($compiled)) {
2019-09-07 21:23:38 +08:00
$content = 'blessing.i18n = '.json_encode(trans('front-end'), JSON_UNESCAPED_UNICODE);
2019-09-07 17:18:58 +08:00
$this->filesystem->put($compiled, $content);
$this->cache->put($this->prefix.$locale, $sourceModified);
2020-03-26 11:20:12 +08:00
return url()->asset("lang/$locale.js?t=$sourceModified");
2019-09-07 17:18:58 +08:00
}
2020-03-26 11:20:12 +08:00
return url()->asset("lang/$locale.js?t=$compiledModified");
2019-09-07 17:18:58 +08:00
}
2019-09-08 18:57:19 +08:00
public function resetTime(string $locale): void
{
$this->cache->put($this->prefix.$locale, 0);
}
public function plugin(string $locale): string
{
$path = public_path("lang/${locale}_plugin.js");
if ($this->filesystem->exists($path)) {
2020-03-26 11:20:12 +08:00
$lastModified = $this->filesystem->lastModified($path);
return url()->asset("lang/${locale}_plugin.js?t=$lastModified");
}
return '';
}
2019-09-07 17:18:58 +08:00
}