Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Aug 2, 2025. It is now read-only.

Conversation

@TheFehr
Copy link
Contributor

@TheFehr TheFehr commented Sep 22, 2021

Based on the conversation in #11 I implemented a recursive function to split the translation keys into associative arrays

Closes #11

@ctf0
Copy link
Owner

ctf0 commented Sep 22, 2021

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, []);
Copy link
Contributor Author

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();
Copy link
Contributor Author

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, '.');
Copy link
Contributor Author

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];
Copy link
Contributor Author

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)));
Copy link
Contributor Author

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)]);
Copy link
Contributor Author

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();
Copy link
Contributor Author

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

@TheFehr
Copy link
Contributor Author

TheFehr commented Sep 22, 2021

I hope those comments help you understand the change. If there is more details needed, feel free to ask

@ctf0
Copy link
Owner

ctf0 commented Sep 22, 2021

oh ok, awesome, thanks again 💋

@ctf0 ctf0 merged commit 202da81 into ctf0:master Sep 22, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Translation keys get deleted

2 participants