psf
 All Classes Functions Variables
localization.php
1 <?php
2 
3 // Part of simple php framework (spf)
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 - 2017
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 Initialize($folder = NULL)
89  {
90  global $psf_localization, $psf_localization_default_language;
91  if (Localization::$__is_initialized)
92  return;
93 
94  self::$__is_initialized = true;
95 
96  if ($folder === NULL)
97  $folder = $psf_localization;
98 
99  if (!file_exists($folder))
100  throw new Exception("Localization folder " . $folder . " can't be found");
101 
102  if (is_file($folder))
103  throw new Exception("Localization folder " . $folder . " is a file");
104 
105  $files = scandir($folder);
106 
107  foreach ($files as $file)
108  {
109  if (!psf_string_endsWith($file, ".txt"))
110  continue;
111 
112  $id = substr($file, 0, strlen($file) - 4);
113  self::$keys[$id] = new Language($folder . "/" . $file);
114  self::$dl = self::$keys[$id];
115  }
116  if (array_key_exists($psf_localization_default_language, self::$keys))
117  self::$dl = self::$keys[$psf_localization_default_language];
118  self::$fallback = self::$dl;
119  }
120 }