outl1ne / nova-settings

A Laravel Nova tool for editing custom settings using native Nova fields.
MIT License
271 stars 99 forks source link

Text doesnt translate when changing locale in boot method #163

Closed arturasfrontit closed 1 year ago

arturasfrontit commented 1 year ago
public function boot()
{
    parent::boot();

    Nova::serving(function (ServingNova $event) {
        app()->setLocale('lt');

        Nova::translations(array_merge(
            json_decode(file_get_contents(lang_path('lt.json')), true),
            json_decode(file_get_contents(lang_path('vendor/nova/lt.json')), true)),
        );
    });

    NovaSettings::addSettingsFields([
        Panel::make(__('Order settings', locale: 'lt'), [
            Text::make(__('Requisites', locale: 'lt'), 'lt_order_requisites')
                ->rules('nullable'),

            Text::make(__('Bill series', locale: 'lt'), 'lt_order_bill_series')
                ->rules('nullable'),

            Text::make(__('Reservation bill series', locale: 'lt'), 'lt_order_reservation_bill_series')
                ->rules('nullable'),

            Text::make(__('Bill writer', locale: 'lt'), 'lt_order_bill_writer')
                ->rules('nullable'),

            Textarea::make(__('Order details', locale: 'lt'), 'lt_order_details')
                ->help(__('Settings order bill details', locale: 'lt'))
                ->rules('nullable'),
        ]),
    ]);
}

lt.json

{
  "novaSettings.navigationItemTitle": "Nustatymai",
  "novaSettings.saveButtonText": "Išsaugoti",
  "novaSettings.noSettingsFieldsText": "No settings fields have been defined.",
  "novaSettings.settingsSuccessToast": "Atnaujinta!",
  "novaSettings.general": "Bendra"
}

image

marttinnotta commented 1 year ago

It does not work because you are currently loading your translations after your gettext calls. Callback that you pass into Nova::serving will fire after NovaSettings::addSettingsFields which you don't want.

Move your NovaSettings::addSettingsFields call into callback that is passed to Nova::serving and it will work.

arturasfrontit commented 1 year ago

Whose translations is for frontend custom packages and etc: https://nova.laravel.com/docs/4.0/customization/localization.html#frontend

I can remove that code and menu will stil have correct translations.

after Nova serving I got declared Nova menu and it works out of the box.

Nova::serving(function (ServingNova $event) {
    app()->setLocale('lt');
});

Nova::mainMenu(function (Request $request) {
  return [
      MenuSection::dashboard(Main::class)->icon('chart-bar'),

      MenuSection::make(__('E-commerce'), [
          MenuItem::resource(Subscription::class),

  ];
});
arturasfrontit commented 1 year ago

Also published translations doesn't translate UI:

{
  "novaSettings.navigationItemTitle": "Settings",
  "novaSettings.saveButtonText": "Save settings",
  "novaSettings.noSettingsFieldsText": "No settings fields have been defined.",
  "novaSettings.settingsSuccessToast": "Settings successfully updated!",
  "novaSettings.general": "General"
}
marttinnotta commented 1 year ago

Not sure what you are saying exactly but your code you provided still was not correct - in your code you called gettext (__ function) for field names before you included your translation file but you will have to include your translation file before using gettext calls.

I tried it out on my project, created new language file and included it like so:

// resources/lang/vendor/nova/en.json

{
  "novaSettings.navigationItemTitle": "My custom settings",
  "novaSettings.saveButtonText": "My custom save settings",
  "novaSettings.noSettingsFieldsText": "My custom no settings fields have been defined.",
  "novaSettings.settingsSuccessToast": "My custom settings successfully updated!",
  "novaSettings.general": "My custom general",
  "novaSettings.myPanelTitle": "My custom panel title",
  "novaSettings.myTextField": "My custom text field label"
}
    // NovaServiceProvider.php

    public function boot()
    {
        parent::boot();

        Nova::serving(function () {
            Nova::translations(lang_path('vendor/nova/en.json'));

            NovaSettings::addSettingsFields([
                Panel::make(__('novaSettings.myPanelTitle'), [
                    Text::make(__('novaSettings.myTextField'), 'my_text_field'),
                ]),
            ], [] );
        });
    }

And this is the result https://d.pr/i/TWhmve

arturasfrontit commented 1 year ago

thank you!

working solution:

Nova::serving(function (ServingNova $event) {
    app()->setLocale('lt');

    Nova::translations(array_merge(
        json_decode(file_get_contents(lang_path('lt.json')), true),
        json_decode(file_get_contents(lang_path('vendor/nova/lt.json')), true),
        json_decode(file_get_contents(lang_path('vendor/nova-settings/lt.json')), true),
    ));

    resolve(Settings::class)();
});