-
-
Notifications
You must be signed in to change notification settings - Fork 12
Implement recursive splitting for translation keys #12
Conversation
|
can u walk me through what exactly have u done ? |
| } | ||
| protected function recSave(array $pre_format, string $key, string $code, string|null $v) | ||
| { | ||
| $codeContent = collect($pre_format)->get($code, []); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Get the language array, if it does not exists initialize an empty array.
| protected function recSave(array $pre_format, string $key, string $code, string|null $v) | ||
| { | ||
| $codeContent = collect($pre_format)->get($code, []); | ||
| $pre_format[$code] = collect($codeContent)->mergeRecursive(self::explodeTranlsationKey($key, $v))->toArray(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Set the language to a new array that is recursively merged from the existing array and the newly exploded key.
| if (strpos($key, '.')) { | ||
| $assoc = []; | ||
| $exp = explode('.', $key); | ||
| $doneSplitting = !Str::contains($nestedTranslationKey, '.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Check if there is more . left to split on
| $final = array_pop($assoc); | ||
| $a = key($final); | ||
| $b = current($final); | ||
| if ($doneSplitting) return [$nestedTranslationKey => $value]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is no more dots that means we have reached the end of the line and can return the last part of the key ($nestedTranslationKey) and the value of this translation.
| if (is_array($b)) { | ||
| $c = key($b); | ||
| $d = current($b); | ||
| $currentLevel = collect(explode('#', Str::replaceFirst('.', '#', $nestedTranslationKey))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We replace the first . with a # to avoid exploding to many levels at once.
| $c = key($b); | ||
| $d = current($b); | ||
| $currentLevel = collect(explode('#', Str::replaceFirst('.', '#', $nestedTranslationKey))); | ||
| $collection = collect([$currentLevel[0] => self::explodeTranlsationKey($currentLevel[1], $value)]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we build an array with the most left part (before the first dot/hash) as the key and the return value of this same function as value.
For the key 'abc.fgh.lmn.pqr' this line is reached 3 times once with the currentlevel 'abc' once with 'fgh' and once with 'lmn'
| $d = current($b); | ||
| $currentLevel = collect(explode('#', Str::replaceFirst('.', '#', $nestedTranslationKey))); | ||
| $collection = collect([$currentLevel[0] => self::explodeTranlsationKey($currentLevel[1], $value)]); | ||
| return $collection->toArray(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we return this recursively built array
|
I hope those comments help you understand the change. If there is more details needed, feel free to ask |
|
oh ok, awesome, thanks again 💋 |
Based on the conversation in #11 I implemented a recursive function to split the translation keys into associative arrays
Closes #11