psf
localization.php
1 <?php
2 
3 // Part of php simple framework (psf)
4 
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 
15 // Copyright (c) Petr Bena <petr@bena.rocks> 2015 - 2018
16 
17 if (!defined("PSF_ENTRY_POINT"))
18  die("Not a valid psf entry point");
19 
20 require_once (dirname(__FILE__) . "/../default_config.php");
21 
22 class Language
23 {
24  protected $data = [];
25 
26  function Get($key)
27  {
28  if (!array_key_exists($key, $this->data))
29  return NULL;
30  return $this->data[$key];
31  }
32 
33  function __construct($datafile)
34  {
35  $handle = fopen("$datafile", "r");
36  if (!$handle)
37  throw new Exception('Unable to load ' . $datafile);
38  while (($line = fgets($handle)) !== false)
39  {
40  $line = str_replace("\n", "", $line);
41  // process the line read.
42  if (psf_string_startsWith($line, "//"))
43  continue;
44  if (!strpos($line, ":"))
45  continue;
46  $key = substr($line, 0, strpos($line, ':'));
47  $value = substr($line, strpos($line, ':') + 1);
48  $this->data[$key] = $value;
49  }
50  }
51 }
52 
55 {
56  protected static $__is_initialized = false;
57  protected static $fallback = NULL;
58  protected static $keys = [];
59  protected static $dl;
60 
61  public static function SetDefaultLanguage($id)
62  {
63  self::Initialize();
64  if (!array_key_exists($id, self::$keys))
65  throw new Exception('Unknown language id');
66  }
67 
68  public static function Get($key, $l = NULL)
69  {
70  self::Initialize();
71  $x = self::$dl->Get($key);
72  if (!$x && self::$fallback != self::$dl)
73  $x = self::$fallback->Get($key);
74  if (!$x)
75  return "[$key]";
76  return $x;
77  }
78 
79  public static function SetLanguage($id)
80  {
81  self::Initialize();
82  if (!array_key_exists($id, self::$keys))
83  throw new Exception('Unknown language');
84 
85  self::$dl = self::$keys[$id];
86  }
87 
88  public static function IsKnown($id)
89  {
90  self::Initialize();
91  return array_key_exists($id, self::$keys);
92  }
93 
94  public static function Initialize($folder = NULL)
95  {
96  global $psf_localization, $psf_localization_default_language;
97  if (Localization::$__is_initialized)
98  return;
99 
100  self::$__is_initialized = true;
101 
102  if ($folder === NULL)
103  $folder = $psf_localization;
104 
105  if (!file_exists($folder))
106  throw new Exception("Localization folder " . $folder . " can't be found");
107 
108  if (is_file($folder))
109  throw new Exception("Localization folder " . $folder . " is a file");
110 
111  $files = scandir($folder);
112 
113  foreach ($files as $file)
114  {
115  if (!psf_string_endsWith($file, ".txt"))
116  continue;
117 
118  $id = substr($file, 0, strlen($file) - 4);
119  self::$keys[$id] = new Language($folder . "/" . $file);
120  self::$dl = self::$keys[$id];
121  }
122  if (array_key_exists($psf_localization_default_language, self::$keys))
123  self::$dl = self::$keys[$psf_localization_default_language];
124  self::$fallback = self::$dl;
125  }
126 }
Localization system.