Skip to content

Instantly share code, notes, and snippets.

@sauron
Created June 10, 2022 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sauron/406b587445b5c96bc2e6997cb9b9441f to your computer and use it in GitHub Desktop.
Save sauron/406b587445b5c96bc2e6997cb9b9441f to your computer and use it in GitHub Desktop.
Override translation static copy from the Twill CMS.

FORM Creation

File: resources/views/admin/settings/interface.blade.php

@extends('twill::layouts.form', [
    'contentFieldsetLabel' => 'Home',
])

@section('contentFields')
@formField('input', [
    'name' => 'home-a11yHeading',
    'label' => 'home-a11yHeading',
    'placeholder' => __('fe.home.a11yHeading'),
])

@formField('input', [
    'name' => 'home-cta_search-aria',
    'label' => 'home-cta_search-aria',
    'placeholder' => __('fe.home.cta_search.aria'),
])

@formField('input', [
    'name' => 'home-lead-pt1',
    'label' => 'home-lead-pt1',
    'placeholder' => __('fe.home.lead.pt1'),
])

Create a settings section that will handle the form for an admin to override the default UI in the translation files. (In this case resources/lang/en/fe.php) To generate the UI form from the translation file we need to use the following snippet:

use Illuminate\Support\Arr;

$labels = Arr::dot(__('fe'));

foreach ($labels as $key => $value) {
    $name = str_replace('.', '_', $key); // Replacing the dot notation by dash to avoid issues when saving the data.
    echo("
        @formField('input', [
            'name' => '{$name}',
            'label' => '{$name}',
            'placeholder' => __('fe.{$key}')
        ])
    ");
}

The above will output all the form fields for the translation file that you can paste in the settings form as shown in the first example above. When saving the form it will be persisted in the Settings table in the DB.

Retrieving the overrided keys

To retrieve the translation values from the DB, the ones that were updated in the form, for using in a transformer like:

use A17\Twill\Repositories\SettingRepository;

class LabelsTransformer
{
    public function transform()
    {
        return [
            'home' => [
                'a11yHeading' => $this->val('home-a11yHeading') ?? __('fe.home.a11yHeading'),
                'cta_search' => [
                    'aria' => $this->val('home-cta_search-aria') ?? __('fe.home.cta_search.aria'),
                ],
                'lead' => [
                    'pt1' => $this->val('home-lead-pt1') ?? __('fe.home.lead.pt1'),
          ...

We need to use the following snippet:

use A17\Twill\Repositories\SettingRepository;

$data = app(SettingRepository::class)->where('section', 'interface')->get()->pluck('value', 'key')->toArray();

$fe = [];
foreach ($data as $key => $value) {
    $fe[str_replace('-', '.',$key)] = "\$this->val({$key}) ?? __('fe.{str_replace('-', '.',$key)}');";
}

$labels = Arr::undot($fe); // returns the structure as shown in the transformer example

Note: The function $this->val() just filter the data retrieved from the DB, it can be something as simple as:

    protected function val($key)
    {
        return app(SettingRepository::class)->byKey($key, 'interface');
    }

But needs to be optmized to avoid back and forth to the DB.

More automated?

If you want to do something more clever, you can just pull what was updated and overwrite the translation file:

use A17\Twill\Repositories\SettingRepository;

$updatedData = app(SettingRepository::class)->where('section', 'interface')->get()->whereNotNull('value')->pluck('value', 'key')->toArray();

$fe = [];
foreach ($updatedData as $key => $value) {
    $fe[str_replace('-', '.',$key)] = $value; // Restore the dot notation.
}

$labels = array_merge(__('fe'), Arr::undot($fe));

The $labels variable will hold the content of the file resources/lang/en/fe.php but with the overrided labels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment