source: trunk/client/inc/hpdf5/spipu/html2pdf/src/Locale.php@ 347

Last change on this file since 347 was 347, checked in by roby, 3 years ago

Aggiornamento per compatibilità con php7.4

File size: 2.5 KB
Line 
1<?php
2/**
3 * Html2Pdf Library - Locale
4 *
5 * HTML => PDF converter
6 * distributed under the OSL-3.0 License
7 *
8 * @package Html2pdf
9 * @author Laurent MINGUET <webmaster@html2pdf.fr>
10 * @copyright 2017 Laurent MINGUET
11 */
12
13namespace Spipu\Html2Pdf;
14
15use Spipu\Html2Pdf\Exception\LocaleException;
16
17class Locale
18{
19 /**
20 * code of the current used locale
21 * @var string
22 */
23 static protected $code = null;
24
25 /**
26 * texts of the current used locale
27 * @var array
28 */
29 static protected $list = array();
30
31 /**
32 * directory where locale files are
33 * @var string
34 */
35 static protected $directory = null;
36
37 /**
38 * load the locale
39 *
40 * @param string $code
41 *
42 * @return void
43 * @throws LocaleException
44 */
45 public static function load($code)
46 {
47 if (self::$directory === null) {
48 self::$directory = __DIR__ . '/locale/';
49 }
50
51 // must be in lower case
52 $code = strtolower($code);
53
54 // must be [a-z-0-9]
55 if (!preg_match('/^([a-z0-9]+)$/isU', $code)) {
56 $e = new LocaleException(
57 'invalid language code'
58 );
59 $e->setLocaleCode($code);
60
61 throw $e;
62 }
63
64 // save the code
65 self::$code = $code;
66
67 // get the name of the locale file
68 $file = self::$directory.self::$code.'.csv';
69
70 // the file must exist
71 if (!is_file($file)) {
72 $e = new LocaleException(
73 'unknown language code. You can create the locale file and push it on the Html2Pdf GitHub project.'
74 );
75 $e->setLocaleCode($code);
76
77 throw $e;
78 }
79
80 // load the file
81 self::$list = array();
82 $handle = fopen($file, 'r');
83 while (!feof($handle)) {
84 $line = fgetcsv($handle);
85 if (!is_array($line) || count($line) !=2) {
86 continue;
87 }
88 self::$list[trim($line[0])] = trim($line[1]);
89 }
90 fclose($handle);
91 }
92
93 /**
94 * clean the locale
95 *
96 * @return void
97 */
98 public static function clean()
99 {
100 self::$code = null;
101 self::$list = array();
102 }
103
104 /**
105 * get a text
106 *
107 * @param string $key
108 * @param string $default
109 *
110 * @return string
111 */
112 public static function get($key, $default = '######')
113 {
114 return (isset(self::$list[$key]) ? self::$list[$key] : $default);
115 }
116}
Note: See TracBrowser for help on using the repository browser.