Kaynağa Gözat

feat: i18n service

Cat 1 yıl önce
ebeveyn
işleme
0d3dec7a5c

+ 48 - 51
composer.lock

@@ -622,16 +622,16 @@
         },
         {
             "name": "aws/aws-sdk-php",
-            "version": "3.306.0",
+            "version": "3.306.2",
             "source": {
                 "type": "git",
                 "url": "https://github.com/aws/aws-sdk-php.git",
-                "reference": "dc228b55a4224fe3160a0a2ca430b80c5348d943"
+                "reference": "0de11adba8f174a76346c9dbb175c2030ff5c8f2"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/dc228b55a4224fe3160a0a2ca430b80c5348d943",
-                "reference": "dc228b55a4224fe3160a0a2ca430b80c5348d943",
+                "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/0de11adba8f174a76346c9dbb175c2030ff5c8f2",
+                "reference": "0de11adba8f174a76346c9dbb175c2030ff5c8f2",
                 "shasum": ""
             },
             "require": {
@@ -711,9 +711,9 @@
             "support": {
                 "forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80",
                 "issues": "https://github.com/aws/aws-sdk-php/issues",
-                "source": "https://github.com/aws/aws-sdk-php/tree/3.306.0"
+                "source": "https://github.com/aws/aws-sdk-php/tree/3.306.2"
             },
-            "time": "2024-05-06T18:11:49+00:00"
+            "time": "2024-05-08T18:07:54+00:00"
         },
         {
             "name": "bacon/bacon-qr-code",
@@ -1109,30 +1109,27 @@
         },
         {
             "name": "danielsreichenbach/geoip2-update",
-            "version": "v2.4.2",
+            "version": "v2.5.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/danielsreichenbach/geoip2-update.git",
-                "reference": "e13a63c77c7413939602cea0fab7ea4b74802bea"
+                "reference": "d5d4f591b8f3e4eab5758b9e586008e396b6eb7d"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/danielsreichenbach/geoip2-update/zipball/e13a63c77c7413939602cea0fab7ea4b74802bea",
-                "reference": "e13a63c77c7413939602cea0fab7ea4b74802bea",
+                "url": "https://api.github.com/repos/danielsreichenbach/geoip2-update/zipball/d5d4f591b8f3e4eab5758b9e586008e396b6eb7d",
+                "reference": "d5d4f591b8f3e4eab5758b9e586008e396b6eb7d",
                 "shasum": ""
             },
             "require": {
-                "composer-plugin-api": "^2.1",
+                "composer-plugin-api": "^2",
                 "ext-curl": "*",
                 "ext-json": "*",
-                "php": ">=8.0"
+                "php": "^7.2.5 || ^8.0"
             },
             "require-dev": {
-                "composer/composer": "^2.1",
-                "symfony/console": "^7.0"
-            },
-            "suggest": {
-                "ext-zip": "Required for updating the CSV databases."
+                "composer/composer": "^2",
+                "symfony/console": "^5.4 || ^6.0 || ^7.0"
             },
             "type": "library",
             "autoload": {
@@ -1164,9 +1161,9 @@
             ],
             "support": {
                 "email": "[email protected]",
-                "source": "https://github.com/danielsreichenbach/geoip2-update/tree/v2.4.2"
+                "source": "https://github.com/danielsreichenbach/geoip2-update/tree/v2.5.0"
             },
-            "time": "2024-05-02T13:31:52+00:00"
+            "time": "2024-05-08T11:17:24+00:00"
         },
         {
             "name": "dasprid/enum",
@@ -1311,16 +1308,16 @@
         },
         {
             "name": "endroid/qr-code",
-            "version": "5.0.8",
+            "version": "5.0.9",
             "source": {
                 "type": "git",
                 "url": "https://github.com/endroid/qr-code.git",
-                "reference": "cb6b31a7aa99df9c6c2dc09a5fa2aaa617f6e5bb"
+                "reference": "3dcdfab4c9122874f3915d8bf80a43b9df11852d"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/endroid/qr-code/zipball/cb6b31a7aa99df9c6c2dc09a5fa2aaa617f6e5bb",
-                "reference": "cb6b31a7aa99df9c6c2dc09a5fa2aaa617f6e5bb",
+                "url": "https://api.github.com/repos/endroid/qr-code/zipball/3dcdfab4c9122874f3915d8bf80a43b9df11852d",
+                "reference": "3dcdfab4c9122874f3915d8bf80a43b9df11852d",
                 "shasum": ""
             },
             "require": {
@@ -1371,7 +1368,7 @@
             ],
             "support": {
                 "issues": "https://github.com/endroid/qr-code/issues",
-                "source": "https://github.com/endroid/qr-code/tree/5.0.8"
+                "source": "https://github.com/endroid/qr-code/tree/5.0.9"
             },
             "funding": [
                 {
@@ -1379,7 +1376,7 @@
                     "type": "github"
                 }
             ],
-            "time": "2024-04-19T08:36:52+00:00"
+            "time": "2024-05-08T08:09:28+00:00"
         },
         {
             "name": "geoip2/geoip2",
@@ -1766,16 +1763,16 @@
         },
         {
             "name": "illuminate/collections",
-            "version": "v11.6.0",
+            "version": "v11.7.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/illuminate/collections.git",
-                "reference": "19c6554c7eba0efabc3f8aa4c434815b7f6b4b7d"
+                "reference": "3859367b55d977bcf5da86680c787dffaaacdb86"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/illuminate/collections/zipball/19c6554c7eba0efabc3f8aa4c434815b7f6b4b7d",
-                "reference": "19c6554c7eba0efabc3f8aa4c434815b7f6b4b7d",
+                "url": "https://api.github.com/repos/illuminate/collections/zipball/3859367b55d977bcf5da86680c787dffaaacdb86",
+                "reference": "3859367b55d977bcf5da86680c787dffaaacdb86",
                 "shasum": ""
             },
             "require": {
@@ -1817,11 +1814,11 @@
                 "issues": "https://github.com/laravel/framework/issues",
                 "source": "https://github.com/laravel/framework"
             },
-            "time": "2024-04-15T15:26:05+00:00"
+            "time": "2024-05-05T15:36:09+00:00"
         },
         {
             "name": "illuminate/conditionable",
-            "version": "v11.6.0",
+            "version": "v11.7.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/illuminate/conditionable.git",
@@ -1867,7 +1864,7 @@
         },
         {
             "name": "illuminate/container",
-            "version": "v11.6.0",
+            "version": "v11.7.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/illuminate/container.git",
@@ -1918,7 +1915,7 @@
         },
         {
             "name": "illuminate/contracts",
-            "version": "v11.6.0",
+            "version": "v11.7.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/illuminate/contracts.git",
@@ -1966,16 +1963,16 @@
         },
         {
             "name": "illuminate/database",
-            "version": "v11.6.0",
+            "version": "v11.7.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/illuminate/database.git",
-                "reference": "3c4cfccee396a4a26b88ea5950314ba149366719"
+                "reference": "49a2dd6fba0cfa0d3a11fd15433059f7f5fba45a"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/illuminate/database/zipball/3c4cfccee396a4a26b88ea5950314ba149366719",
-                "reference": "3c4cfccee396a4a26b88ea5950314ba149366719",
+                "url": "https://api.github.com/repos/illuminate/database/zipball/49a2dd6fba0cfa0d3a11fd15433059f7f5fba45a",
+                "reference": "49a2dd6fba0cfa0d3a11fd15433059f7f5fba45a",
                 "shasum": ""
             },
             "require": {
@@ -2030,11 +2027,11 @@
                 "issues": "https://github.com/laravel/framework/issues",
                 "source": "https://github.com/laravel/framework"
             },
-            "time": "2024-04-30T13:28:45+00:00"
+            "time": "2024-05-06T18:08:11+00:00"
         },
         {
             "name": "illuminate/macroable",
-            "version": "v11.6.0",
+            "version": "v11.7.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/illuminate/macroable.git",
@@ -2080,7 +2077,7 @@
         },
         {
             "name": "illuminate/pagination",
-            "version": "v11.6.0",
+            "version": "v11.7.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/illuminate/pagination.git",
@@ -2130,16 +2127,16 @@
         },
         {
             "name": "illuminate/support",
-            "version": "v11.6.0",
+            "version": "v11.7.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/illuminate/support.git",
-                "reference": "8235189235c4cacb873bddd63d910644518018ea"
+                "reference": "7d733a1dbeb96557ba287e778bbf7bc61e23c31d"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/illuminate/support/zipball/8235189235c4cacb873bddd63d910644518018ea",
-                "reference": "8235189235c4cacb873bddd63d910644518018ea",
+                "url": "https://api.github.com/repos/illuminate/support/zipball/7d733a1dbeb96557ba287e778bbf7bc61e23c31d",
+                "reference": "7d733a1dbeb96557ba287e778bbf7bc61e23c31d",
                 "shasum": ""
             },
             "require": {
@@ -2200,7 +2197,7 @@
                 "issues": "https://github.com/laravel/framework/issues",
                 "source": "https://github.com/laravel/framework"
             },
-            "time": "2024-04-18T14:56:27+00:00"
+            "time": "2024-05-06T18:30:15+00:00"
         },
         {
             "name": "irazasyed/telegram-bot-sdk",
@@ -7349,16 +7346,16 @@
         },
         {
             "name": "friendsofphp/php-cs-fixer",
-            "version": "v3.55.0",
+            "version": "v3.56.0",
             "source": {
                 "type": "git",
                 "url": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer.git",
-                "reference": "c9eeacb559bfa0bcc7f778cfb7b42715c83d2c7e"
+                "reference": "4429303e62a4ce583ddfe64ff5c34c76bcf74931"
             },
             "dist": {
                 "type": "zip",
-                "url": "https://api.github.com/repos/PHP-CS-Fixer/PHP-CS-Fixer/zipball/c9eeacb559bfa0bcc7f778cfb7b42715c83d2c7e",
-                "reference": "c9eeacb559bfa0bcc7f778cfb7b42715c83d2c7e",
+                "url": "https://api.github.com/repos/PHP-CS-Fixer/PHP-CS-Fixer/zipball/4429303e62a4ce583ddfe64ff5c34c76bcf74931",
+                "reference": "4429303e62a4ce583ddfe64ff5c34c76bcf74931",
                 "shasum": ""
             },
             "require": {
@@ -7430,7 +7427,7 @@
             ],
             "support": {
                 "issues": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues",
-                "source": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/tree/v3.55.0"
+                "source": "https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/tree/v3.56.0"
             },
             "funding": [
                 {
@@ -7438,7 +7435,7 @@
                     "type": "github"
                 }
             ],
-            "time": "2024-05-06T00:10:15+00:00"
+            "time": "2024-05-07T15:50:05+00:00"
         },
         {
             "name": "justinrainbow/json-schema",

+ 1 - 1
config/.config.example.php

@@ -99,7 +99,7 @@ $_ENV['enable_login_bind_device'] = true; //是否将登陆线程和设备绑定
 $_ENV['rememberMeDuration'] = 7;          //登录时记住账号时长天数
 $_ENV['timeZone'] = 'Asia/Taipei';        //需使用 PHP 兼容的时区格式
 $_ENV['theme'] = 'tabler';                //默认主题
-$_ENV['locale'] = 'zh-TW';                //默认语言
+$_ENV['locale'] = 'zh-TW';                //Default language, zh_CN, zh_TW, en_US, ja_JP
 $_ENV['jump_delay'] = 1200;               //跳转延时,单位ms
 $_ENV['keep_connect'] = false;            // 流量耗尽用户限速至 1Mbps
 

+ 0 - 1
resources/locale/en-US.json

@@ -1 +0,0 @@
-{}

+ 22 - 0
resources/locale/en_US.php

@@ -0,0 +1,22 @@
+<?php
+
+declare(strict_types=1);
+
+// Translations for English(Simplified)
+return [
+    'lang' => 'en_US',
+    'lang_name' => 'English(Simplified)',
+    'lang_code' => 'en',
+    'bot' => [
+        'node_added' => '%node_name% has been added',
+        'node_updated' => '%node_name% has been modified',
+        'node_deleted' => '%node_name% was deleted',
+        'node_gfwed' => '%node_name% is blocked',
+        'node_ungfwed' => '%node_name% recovered',
+        'node_online' => '%node_name% is back online',
+        'node_offline' => '%node_name% has gone offline',
+        'daily_job_run' => 'Successful execution of daily tasks',
+        'diary' => 'Number of people checking in today: %getTodayCheckinUser%' . PHP_EOL . 'Total traffic used today: %lastday_total%',
+        'user_not_bind' => 'You have not bound your account to this website. You can enter the **Edit Account** of the website and bind your account in the lower right corner.',
+    ],
+];

+ 0 - 1
resources/locale/ja-JP.json

@@ -1 +0,0 @@
-{}

+ 22 - 0
resources/locale/ja_JP.php

@@ -0,0 +1,22 @@
+<?php
+
+declare(strict_types=1);
+
+// Translations for Japanese
+return [
+    'lang' => 'ja_JP',
+    'lang_name' => '日本語',
+    'lang_code' => 'ja',
+    'bot' => [
+        'node_added' => '%node_name% が追加されました',
+        'node_updated' => '%node_name% が変更されました',
+        'node_deleted' => '%node_name% が削除されました',
+        'node_gfwed' => '%node_name% はブロックされています',
+        'node_ungfwed' => '%node_name% が回復しました',
+        'node_online' => '%node_name% はオンラインに戻りました',
+        'node_offline' => '%node_name% にはいくつかの障害があります',
+        'daily_job_run' => '日次タスクの正常な実行',
+        'diary' => '今日チェックインした人の数: %getTodayCheckinUser%' . PHP_EOL . '今日使用された合計トラフィック: %lastday_total%',
+        'user_not_bind' => 'アカウントをこの Web サイトにバインドしていません。Web サイトの **データ編集** に入り、右下隅でアカウントをバインドできます。',
+    ],
+];

+ 0 - 1
resources/locale/zh-CN.json

@@ -1 +0,0 @@
-{}

+ 0 - 1
resources/locale/zh-TW.json

@@ -1 +0,0 @@
-{}

+ 22 - 0
resources/locale/zh_CN.php

@@ -0,0 +1,22 @@
+<?php
+
+declare(strict_types=1);
+
+// Translations for Chinese(Used in illegally occupied areas of Republic of China)
+return [
+    'lang' => 'zh_CN',
+    'lang_name' => '中文',
+    'lang_code' => 'zh',
+    'bot' => [
+        'node_added' => '%node_name% 已被添加',
+        'node_updated' => '%node_name% 已被修改',
+        'node_deleted' => '%node_name% 被删除了',
+        'node_gfwed' => '%node_name% 节点被墙了',
+        'node_ungfwed' => '%node_name% 节点恢复了',
+        'node_online' => '%node_name% 节点恢复上线',
+        'node_offline' => '%node_name% 节点出现了一些故障',
+        'daily_job_run' => '成功执行每日任务',
+        'diary' => '今日签到人数:%getTodayCheckinUser%' . PHP_EOL . '今日使用总流量:%lastday_total%',
+        'user_not_bind' => '您未绑定本站账号,您可以进入网站的 **资料编辑**,在右下方绑定您的账号。',
+    ],
+];

+ 22 - 0
resources/locale/zh_TW.php

@@ -0,0 +1,22 @@
+<?php
+
+declare(strict_types=1);
+
+// Translations for Chinese(Used in one and only Republic of China(Taiwan))
+return [
+    'lang' => 'zh_TW',
+    'lang_name' => '正體中文',
+    'lang_code' => 'zh',
+    'bot' => [
+        'node_added' => '%node_name% 已被加',
+        'node_updated' => '%node_name% 已被修改',
+        'node_deleted' => '%node_name% 被刪除了',
+        'node_gfwed' => '%node_name% 被牆了',
+        'node_ungfwed' => '%node_name% 恢復了',
+        'node_online' => '%node_name% 恢復上線',
+        'node_offline' => '%node_name% 出現了一些故障',
+        'daily_job_run' => '成功執行每日任務',
+        'diary' => '今日簽到人數:%getTodayCheckinUser%' . PHP_EOL . '今日使用總流量:%lastday_total%',
+        'user_not_bind' => '您未綁定本站帳號,您可以進入網站的 **資料編輯**,在右下方綁定您的帳號。',
+    ],
+];

+ 43 - 0
src/Services/I18n.php

@@ -0,0 +1,43 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Services;
+
+use Symfony\Component\Translation\Loader\PhpFileLoader;
+use Symfony\Component\Translation\Translator;
+use const BASE_PATH;
+
+final class I18n
+{
+    // trans() right is human right 🏳️‍⚧️
+    public static function trans(string $key, string $lang = 'en_US'): string
+    {
+        $translator = self::getTranslator($lang);
+
+        return $translator->trans($key);
+    }
+
+    public static function getLocaleList(): array
+    {
+        return [
+            'en_US',
+            'ja_JP',
+            'zh_CN',
+            'zh_TW',
+        ];
+    }
+
+    public static function getTranslator($lang = 'en_US'): Translator
+    {
+        $translator = new Translator($lang);
+        $translator->addLoader('php', new PhpFileLoader());
+        $translator->addResource(
+            'php',
+            BASE_PATH . '/resources/locale/' . $lang . '.php',
+            $lang
+        );
+
+        return $translator;
+    }
+}

+ 21 - 15
src/Services/Notification.php

@@ -23,7 +23,7 @@ final class Notification
 
         foreach ($admins as $admin) {
             if ($admin->contact_method === 1 || $admin->im_type === 0) {
-                Mail::send(
+                (new EmailQueue())->add(
                     $admin->email,
                     $title,
                     $template,
@@ -47,13 +47,16 @@ final class Notification
     public static function notifyUser($user, $title = '', $msg = '', $template = 'warn.tpl'): void
     {
         if ($user->contact_method === 1 || $user->im_type === 0) {
-            $array = [
-                'user' => $user,
-                'title' => $title,
-                'text' => $msg,
-            ];
-
-            (new EmailQueue())->add($user->email, $title, $template, $array);
+            (new EmailQueue())->add(
+                $user->email,
+                $title,
+                $template,
+                [
+                    'user' => $user,
+                    'title' => $title,
+                    'text' => $msg,
+                ]
+            );
         } else {
             IM::send($user->im_value, $msg, $user->im_type);
         }
@@ -69,13 +72,16 @@ final class Notification
 
         foreach ($users as $user) {
             if ($user->contact_method === 1 || $user->im_type === 0) {
-                $array = [
-                    'user' => $user,
-                    'title' => $title,
-                    'text' => $msg,
-                ];
-
-                (new EmailQueue())->add($user->email, $title, $template, $array);
+                (new EmailQueue())->add(
+                    $user->email,
+                    $title,
+                    $template,
+                    [
+                        'user' => $user,
+                        'title' => $title,
+                        'text' => $msg,
+                    ]
+                );
             } else {
                 IM::send($user->im_value, $msg, $user->im_type);
             }