2
2
3
3
namespace Jetcod \Laravel \Translation \Traits ;
4
4
5
- use Illuminate \Database \Eloquent \Model ;
6
5
use Illuminate \Database \Eloquent \Relations \MorphMany ;
7
6
use Illuminate \Database \Eloquent \Relations \MorphOne ;
7
+ use Illuminate \Support \Collection ;
8
8
use Jetcod \Laravel \Translation \Models \Translation ;
9
9
10
10
trait TranslatableTrait
11
11
{
12
- private $ cachedModel ;
12
+ private $ cachedTranslationModels ;
13
13
14
14
public function __get ($ key )
15
15
{
16
16
if ($ this ->isRelation ($ key )) {
17
17
return $ this ->getRelationValue ($ key );
18
18
}
19
19
20
- // Check if the attribute is translatable
21
- if ($ translation = $ this ->getTranslation ($ key )) {
22
- return $ translation ;
20
+ // Check if the attribute translation exists
21
+ $ translation = $ this ->getTranslation ($ key );
22
+ if ($ translation instanceof Translation) {
23
+ return $ translation ->value ;
23
24
}
24
25
25
26
// Fallback to the default behavior
@@ -37,9 +38,12 @@ public function translations(): MorphMany
37
38
}
38
39
39
40
/**
40
- * Returns translatable attribute(s) .
41
+ * Retrieves the list of attributes that are translatable for the current model .
41
42
*
42
- * @return null|array|string
43
+ * If the `TRANSLATABLE_ATTRIBUTES` constant is defined on the model, it will be returned.
44
+ * Otherwise, `null` will be returned, indicating that all attributes are translatable.
45
+ *
46
+ * @return null|array the list of translatable attributes, or `null` if all attributes are translatable
43
47
*/
44
48
protected function getTranslatableAttributes ()
45
49
{
@@ -50,19 +54,31 @@ protected function getTranslatableAttributes()
50
54
return null ;
51
55
}
52
56
53
- private function getTranslation (string $ key ): ?string
57
+ /**
58
+ * Retrieves the translation for the given attribute key, if the attribute is translatable.
59
+ *
60
+ * @param string $key the attribute key to retrieve the translation for
61
+ *
62
+ * @return null|Translation the translation model, or null if the attribute is not translatable
63
+ */
64
+ private function getTranslation (string $ key ): ?Translation
54
65
{
55
66
if ($ this ->isTranslatableAttribute ($ key )) {
56
- $ model = $ this ->getCachedModel ();
67
+ $ collection = $ this ->getCachedTranslations ();
57
68
58
- if ($ model instanceof Model) {
59
- return $ key == $ model ->key ? $ model ->value : null ;
60
- }
69
+ return $ collection ->firstWhere ('key ' , $ key );
61
70
}
62
71
63
72
return null ;
64
73
}
65
74
75
+ /**
76
+ * Determines whether the given attribute key is a translatable attribute.
77
+ *
78
+ * @param string $key the attribute key to check
79
+ *
80
+ * @return bool `true` if the attribute is translatable, `false` otherwise
81
+ */
66
82
private function isTranslatableAttribute (string $ key ): bool
67
83
{
68
84
$ translatables = $ this ->getTranslatableAttributes ();
@@ -74,8 +90,13 @@ private function isTranslatableAttribute(string $key): bool
74
90
return is_array ($ translatables ) ? in_array ($ key , $ translatables ) : $ key == $ translatables ;
75
91
}
76
92
77
- private function getCachedModel ()
93
+ /**
94
+ * Retrieves the cached translation models for the current locale.
95
+ *
96
+ * @return Collection the collection of translation models
97
+ */
98
+ private function getCachedTranslations (): Collection
78
99
{
79
- return $ this ->cachedModel = $ this ->cachedModel ?: $ this ->translation ()->locale (app ()->getLocale ())->first ();
100
+ return $ this ->cachedTranslationModels = $ this ->cachedTranslationModels ?: $ this ->translation ()->locale (app ()->getLocale ())->get ();
80
101
}
81
102
}
0 commit comments