22
33namespace Kirby \Cms ;
44
5- use Exception ;
65use Kirby \Data \Data ;
6+ use Kirby \Exception \Exception ;
77use Kirby \Filesystem \F ;
8+ use Throwable ;
89
910/**
10- * With helper methods provides get language translations or
11- * loads from custom `translations` root
11+ * Manages the translations string for a language,
12+ * either from the language file or `translations` root
1213 * @since 5.0.0
1314 *
1415 * @package Kirby Cms
1920 */
2021class LanguageTranslations
2122{
22- protected array $ data ;
23-
2423 public function __construct (
2524 protected Language $ language ,
26- self | array $ translations = []
25+ protected array $ data = []
2726 ) {
28- $ this ->setTranslations ($ translations );
27+ $ this ->data = [...$ this ->load (), ...$ this ->data ];
28+ }
29+
30+ /**
31+ * Deletes the current language translations file
32+ * if custom root defined
33+ */
34+ public function delete (): void
35+ {
36+ if ($ file = $ this ->root ()) {
37+ if (F::remove ($ file ) !== true ) {
38+ throw new Exception ('The language translations could not be deleted ' );
39+ }
40+ }
2941 }
3042
3143 /**
@@ -37,33 +49,33 @@ public function get(string $key, string $default = null): string|null
3749 }
3850
3951 /**
40- * Loads the language translations based on custom roots for provided language code
52+ * Loads the language translations based on custom roots
4153 */
42- public function load (array $ default = [] ): array
54+ public function load (): array
4355 {
4456 if ($ file = static ::root ()) {
4557 try {
4658 return Data::read ($ file );
47- } catch (Exception ) {
48- return $ default ;
59+ } catch (Throwable ) {
60+ // skip when an exception thrown
4961 }
5062 }
5163
52- return $ default ;
64+ return [] ;
5365 }
5466
5567 /**
5668 * Saves the language translations in the custom root
69+ * @return $this
5770 * @internal
5871 *
59- * @return $this
6072 */
6173 public function save (array $ translations = []): static
6274 {
63- $ this ->setTranslations ( $ translations) ;
75+ $ this ->data = $ translations ;
6476
6577 if ($ root = $ this ->root ()) {
66- Data::write ($ root , $ translations );
78+ Data::write ($ root , $ this -> data );
6779 }
6880
6981 return $ this ;
@@ -74,15 +86,8 @@ public function save(array $translations = []): static
7486 */
7587 public function root (): string |null
7688 {
77- $ kirby = App::instance ();
78- $ root = $ kirby ->root ('translations ' );
79- $ file = ($ root ?? '' ) . '/ ' . $ this ->language ->code () . '.php ' ;
80-
81- if (
82- $ root !== null &&
83- F::exists ($ file ) === true
84- ) {
85- return $ file ;
89+ if ($ root = App::instance ()->root ('translations ' )) {
90+ return $ root . '/ ' . $ this ->language ->code () . '.php ' ;
8691 }
8792
8893 return null ;
@@ -111,30 +116,19 @@ public function set(string $key, string|null $value = null): static
111116 }
112117
113118 /**
114- * Set translations
115- *
116- * @return $this
119+ * Returns translations
117120 */
118- public function setTranslations ( self | array $ translations = [] ): static
121+ public function toArray ( ): array
119122 {
120- if (empty ($ translations ) === false ) {
121- if ($ translations instanceof self) {
122- $ this ->data = $ translations ->toArray ();
123- } else {
124- $ this ->data = $ translations ;
125- }
126- } else {
127- $ this ->data = static ::load ();
128- }
129-
130- return $ this ;
123+ return $ this ->data ;
131124 }
132125
133126 /**
134- * Returns translations
127+ * Updates the translations data
135128 */
136- public function toArray ( ): array
129+ public function update ( array $ data = [] ): static
137130 {
138- return $ this ->data ;
131+ $ this ->data = $ data ;
132+ return $ this ;
139133 }
140134}
0 commit comments