1: | <?php
2: | /**
3: | * @package OpenCart
4: | *
5: | * @author Daniel Kerr
6: | * @copyright Copyright (c) 2005 - 2022, OpenCart, Ltd. (https://www.opencart.com/)
7: | * @license https://opensource.org/licenses/GPL-3.0
8: | *
9: | * @see https://www.opencart.com
10: | */
11: | namespace Opencart\System\Library;
12: | /**
13: | * Class Language
14: | */
15: | class Language {
16: | /**
17: | * @var string
18: | */
19: | protected string $code;
20: | /**
21: | * @var string
22: | */
23: | protected string $directory;
24: | /**
25: | * @var array<string, string>
26: | */
27: | protected array $path = [];
28: | /**
29: | * @var array<string, string>
30: | */
31: | protected array $data = [];
32: | /**
33: | * @var array<string, array<string, array<string, mixed>>>
34: | */
35: | protected array $cache = [];
36: |
37: | /**
38: | * Constructor
39: | *
40: | * @param string $code
41: | */
42: | public function __construct(string $code) {
43: | $this->code = $code;
44: | }
45: |
46: | /**
47: | * addPath
48: | *
49: | * @param string $namespace
50: | * @param string $directory
51: | *
52: | * @return void
53: | */
54: | public function addPath(string $namespace, string $directory = ''): void {
55: | if (!$directory) {
56: | $this->directory = $namespace;
57: | } else {
58: | $this->path[$namespace] = $directory;
59: | }
60: | }
61: |
62: | /**
63: | * Get
64: | *
65: | * Get language text string
66: | *
67: | * @param string $key
68: | */
69: | public function get(string $key): string {
70: | return $this->data[$key] ?? $key;
71: | }
72: |
73: | /**
74: | * Set
75: | *
76: | * Set language text string
77: | *
78: | * @param string $key
79: | * @param string $value
80: | *
81: | * @return void
82: | */
83: | public function set(string $key, string $value): void {
84: | $this->data[$key] = $value;
85: | }
86: |
87: | /**
88: | * All
89: | *
90: | * @param string $prefix
91: | *
92: | * @return array<string, string>
93: | */
94: | public function all(string $prefix = ''): array {
95: | if (!$prefix) {
96: | return $this->data;
97: | }
98: |
99: | $_ = [];
100: |
101: | $length = strlen($prefix);
102: |
103: | foreach ($this->data as $key => $value) {
104: | if (substr($key, 0, $length) == $prefix) {
105: | $_[substr($key, $length + 1)] = $value;
106: | }
107: | }
108: |
109: | return $_;
110: | }
111: |
112: | /**
113: | * Clear
114: | *
115: | * @return void
116: | */
117: | public function clear(): void {
118: | $this->data = [];
119: | }
120: |
121: | /**
122: | * Load
123: | *
124: | * @param string $filename
125: | * @param string $prefix
126: | * @param string $code Language code
127: | *
128: | * @return array<string, string>
129: | */
130: | public function load(string $filename, string $prefix = '', string $code = ''): array {
131: | if (!$code) {
132: | $code = $this->code;
133: | }
134: |
135: | if (!isset($this->cache[$code][$filename])) {
136: | $_ = [];
137: |
138: | // Load selected language file to overwrite the default language keys
139: | $file = $this->directory . $code . '/' . $filename . '.php';
140: |
141: | $namespace = '';
142: |
143: | $parts = explode('/', $filename);
144: |
145: | foreach ($parts as $part) {
146: | if (!$namespace) {
147: | $namespace .= $part;
148: | } else {
149: | $namespace .= '/' . $part;
150: | }
151: |
152: | if (isset($this->path[$namespace])) {
153: | $file = $this->path[$namespace] . $code . substr($filename, strlen($namespace)) . '.php';
154: | }
155: | }
156: |
157: | if (is_file($file)) {
158: | require($file);
159: | }
160: |
161: | $this->cache[$code][$filename] = $_;
162: | } else {
163: | $_ = $this->cache[$code][$filename];
164: | }
165: |
166: | if ($prefix) {
167: | foreach ($_ as $key => $value) {
168: | $_[$prefix . '_' . $key] = $value;
169: |
170: | unset($_[$key]);
171: | }
172: | }
173: |
174: | $this->data = array_merge($this->data, $_);
175: |
176: | return $this->data;
177: | }
178: | }
179: |

OpenCart API API documentation generated by ApiGen dev-master