فهرست منبع

Add character substitution file for transliteration

Visman 1 سال پیش
والد
کامیت
4495f64268
5فایلهای تغییر یافته به همراه74 افزوده شده و 0 حذف شده
  1. 25 0
      app/Core/Func.php
  2. 23 0
      app/Models/Pages/Admin/Update.php
  3. 1 0
      app/config/install.php
  4. 1 0
      app/config/main.dist.php
  5. 24 0
      app/config/translit.default.php

+ 25 - 0
app/Core/Func.php

@@ -48,6 +48,11 @@ class Func
      */
     protected Transliterator|false|null $transl = null;
 
+    /**
+     * Массив подмены символов перед/вместо траслитератор(ом/а)
+     */
+    protected ?array $translArray;
+
     public function __construct(protected Container $c)
     {
         $this->fUrl = $this->c->FRIENDLY_URL;
@@ -321,13 +326,33 @@ class Func
     public function friendly(string $str): string
     {
         if (null === $this->transl) {
+            $useFile = false;
+
             if (empty($this->fUrl['translit'])) {
                 $this->transl = false;
+            } elseif (true === $this->fUrl['translit']) {
+                $this->transl = false;
+                $useFile      = true;
             } else {
                 $this->transl = Transliterator::create($this->fUrl['translit']) ?? false;
+
+                if ($this->transl instanceof Transliterator) {
+                    $useFile = true;
+                }
+            }
+
+            if (
+                true === $useFile
+                && ! empty($this->fUrl['file'])
+            ) {
+                $this->translArray = include "{$this->c->DIR_CONFIG}/{$this->fUrl['file']}";
             }
         }
 
+        if (! empty($this->translArray)) {
+            $str = \strtr($str, $this->translArray);
+        }
+
         if ($this->transl instanceof Transliterator) {
             $str = $this->transl->transliterate($str);
         }

+ 23 - 0
app/Models/Pages/Admin/Update.php

@@ -1001,4 +1001,27 @@ class Update extends Admin
 
         return null;
     }
+
+    /**
+     * rev.71 to rev.72
+     */
+    protected function stageNumber71(array $args): ?int
+    {
+        switch ($args['start'] ?? 1) {
+            case 2:
+                return null;
+            default:
+                $coreConfig = new CoreConfig($this->configFile);
+
+                $coreConfig->add(
+                    'FRIENDLY_URL=>file',
+                    '\'translit.default.php\'',
+                    'WtoHyphen'
+                );
+
+                $coreConfig->save();
+
+                return 2;
+        }
+    }
 }

+ 1 - 0
app/config/install.php

@@ -64,6 +64,7 @@ return [
         'lowercase' => true,
         'translit'  => 'Russian-Latin/BGN;Any-Latin;Latin-ASCII;',
         'WtoHyphen' => true,
+        'file'      => 'translit.default.php',
     ],
 
     'forConfig' => [

+ 1 - 0
app/config/main.dist.php

@@ -78,6 +78,7 @@ return [
         'lowercase' => true,
         'translit'  => 'Russian-Latin/BGN;Any-Latin;Latin-ASCII;',
         'WtoHyphen' => true,
+        'file'      => 'translit.default.php',
     ],
 
     'shared' => [

+ 24 - 0
app/config/translit.default.php

@@ -0,0 +1,24 @@
+<?php
+/**
+ * This file is part of the ForkBB <https://github.com/forkbb>.
+ *
+ * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
+ * @license   The MIT License (MIT)
+ */
+
+declare(strict_types=1);
+
+/**
+ * Массив подмены символов
+ * Используется в методе app\Core\Func\friendly() в случае:
+ * 1. если конфиг FRIENDLY_URL['translit'] содержит строку правил - тогда подмена символов производится до Transliterator::transliterate()
+ * 2. если конфиг FRIENDLY_URL['translit'] равен true - тогда подмена символов выполняется без вызова Transliterator::transliterate()
+ */
+return [
+    'ь' => '',
+    'Ь' => '',
+    'ъ' => '',
+    'Ъ' => '',
+    '\'' => '',
+    '’' => '',
+];