Translation.php
928 Bytes
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
<?php declare( strict_types=1 );
namespace AC\Asset\Script\Localize;
use InvalidArgumentException;
final class Translation {
/**
* @var array
*/
private $translations;
public function __construct( array $translations ) {
$this->translations = $translations;
}
public static function create( array $translations ): self {
return new self( $translations );
}
public function with_translation( array $translations ): self {
return self::create( array_merge( $this->translations, $translations ) );
}
public function get_translation( string $component = null ): array {
$translations = $this->translations;
if ( $component ) {
if ( ! isset( $translations[ $component ] ) ) {
throw new InvalidArgumentException( sprintf( 'Undefined component %s for translation.', $component ) );
}
$translations = [
$component => $translations[ $component ],
];
}
return $translations;
}
}