Welcome to mirror list, hosted at ThFree Co, Russian Federation.

L10N.php « ncp-web - github.com/nextcloud/nextcloudpi.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e00a7909625a2ef289da0a668b4d1697e500869b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php

define('GENERATE_TEMPLATES', false);

class L10N
{
  private $translations = [];
  const default_language = "en_US";
  private $language;
  private $l10n_template = [];

  public function __construct($desired_languages, $l10n_dir, $modules_path = null)
  {
    if (!isset($desired_languages)) {
      $desired_languages = "";
    }

    $l10n_dir = rtrim($l10n_dir, '/');
    $available_languages = array_filter(scandir($l10n_dir),
        function ($s) {
          return pathinfo($s, PATHINFO_EXTENSION) == "json";
        });
    $available_languages = array_map(
        function ($s) {
          return basename($s, ".json");
        },
        $available_languages);

    $desired_lang = $this->load_language_setting($modules_path);
    if ($desired_lang != "auto") {
      $desired_languages = $desired_lang;
    }
    $lang = $this->find_language($available_languages, $desired_languages);
    $this->language = $lang;
    if ($lang === L10N::default_language || !file_exists(join('/', [$l10n_dir, $lang . ".json"])) || !$this->load($lang, $l10n_dir, $modules_path)) {
      $this->language = L10N::default_language;
    }
  }

  function load($lang, $l10n_dir, $modules_path)
  {
    $modules_path = join('/', [rtrim($modules_path, '/'), $l10n_dir]);
    $files = [join('/', [$l10n_dir, $lang . ".json"])];
    if (is_dir($modules_path)) {
      foreach (scandir($modules_path) as $dir) {
        $file_path = join('/', [$modules_path, trim($dir, '/'), $lang . ".json"]);
        if (is_file($file_path)) {
          array_push($files, $file_path);
        }
      }
    }

    $jsonError = null;
    foreach ($files as $file) {
      $module_name = pathinfo(pathinfo($file, PATHINFO_DIRNAME), PATHINFO_BASENAME);
      if ($module_name == pathinfo($l10n_dir, PATHINFO_BASENAME)) {
        $module_name = "__core__";
      }
      $json = json_decode(file_get_contents($file), true);
      if (!is_array($json)) {
        $jsonError = json_last_error();
        continue;
      }
      $this->translations[$module_name] = $json['translations'];
    }
    return ($jsonError == null);
  }

  function save($lang)
  {
    $cfg = 'ncp-web.cfg';
    $line = file_get_contents( $cfg );
    $str  = '';
    if (preg_match('/^LANGUAGE_=\[(([_\w]+,)*[_\w]+)\]$/', $line, $matches)) {
      $options = explode(',', $matches[1]);
      foreach ($options as $option) {
        $opt = trim( $option, "_" );
        $str .= $opt == $lang ? '_' . $opt . '_,' : $opt . ',';
      }
    }
    $str = 'LANGUAGE_=[' . rtrim( $str, ',' ) . ']';
    return file_put_contents( $cfg , $str );
  }

  function load_template($module)
  {
    if (is_file("./l10n_templates/$module.json")) {
      $this->l10n_template[$module] =
          json_decode(file_get_contents("./l10n_templates/$module.json"), true)['translations'];
    } else {
      $this->l10n_template[$module] = [];
    }
  }

  function save_template($module)
  {
    $f_handle = fopen("./l10n_templates/$module.json", "w");
    fwrite($f_handle, json_encode(['translations' => $this->l10n_template[$module]]));
    fclose($f_handle);
  }

  public function __($text, $module = "__core__")
  {
    if (GENERATE_TEMPLATES) {
      $this->load_template($module);
      if (!isset($this->l10n_template[$module][$text])) {
        $this->l10n_template[$module][$text] = $text;
        $this->save_template($module);
      }
    }
    if (isset($this->translations[$module]) && isset($this->translations[$module][$text])) {
      return $this->translations[$module][$text];
    }
    return $text;
  }

  function find_language(array $available_languages, $desired_language)
  {

    $available_languages = array_flip($available_languages);
    $langs = [];
    preg_match_all('~([\w-]+)(?:[^,\d]+([\d.]+))?~', strtolower($desired_language), $matches, PREG_SET_ORDER);

    foreach ($matches as $match) {

      list($a, $b) = explode('-', $match[1]) + array('', '');
      $value = isset($match[2]) ? (float)$match[2] : 1.0;

      if (isset($available_languages[$match[1]])) {
        $langs[$match[1]] = $value;
        continue;
      }

      if (isset($available_languages[$a])) {
        $langs[$a] = $value - 0.1;
      }

    }
    arsort($langs);

    if (count($langs) == 0) {
      return L10N::default_language;
    }
    return array_keys($langs)[0];
  }

  function load_language_setting($modules_path)
  {
    $webui_config_file = 'ncp-web.cfg';
    $fh = fopen($webui_config_file, 'r')
    or exit('{ "output": "' . $webui_config_file . ' read error" }');
    $lang = "auto";
    while ($line = fgets($fh)) {
      // drop down menu
      if (preg_match('/^LANGUAGE_=\[(([_\w]+,)*[_\w]+)\]$/', $line, $matches)) {
        $options = explode(',', $matches[1]);
        foreach ($options as $option) {
          if ($option[0] == "_" && $option[count($option) - 1] == "_") {
            fclose($fh);
            $lang = trim($option, '_');
          }
        }
        fclose($fh);
        return $lang;
      }
    }
  }
}