whitecube / nova-page

Static pages content management for Laravel Nova
https://whitecube.github.io/nova-page
MIT License
238 stars 41 forks source link

Localization #20

Closed krizzdev closed 5 years ago

krizzdev commented 5 years ago

In the Documentation and on novapackages we can see Text Fields with the localization feature.

I saw you removed the ability to load templates with the locale parameter. What was the reason and what would be the best way to enable localization. Making multiple Templates for every language?

toonvandenbos commented 5 years ago

Hi @ChrisSFR,

It depends on which package you use for translating the fields. If the package claims to store translations in a json column, then it will simply work as expected with our package, because translated field values are stored in the same json file.

We have several websites running with this package & using translated fields and have not encountered any issues yet.

Which translated fields package do you use?

voidgraphics commented 5 years ago

To add onto the previous answer, we felt it was a bad decision to force a particular way to handle translated data, so we removed it and if you need it, you can choose how to make it work yourself. We aim to be as little opinionated as possible.

If you're curious about how we do it, we use spatie/laravel-translatable on both our models and our Nova Page templates to define which fields contain translated data.

Then, we use mrmonat/nova-translatable to make that translatable data editable in Nova. Note that Spatie themselves have made a similar package, but we found mrmonat's UI was better for our needs.

Also I am closing this since it is not an issue and rather a question.

krizzdev commented 5 years ago

Thank you for your quick answer. Thats exactly what I was searching for. Btw thanks for this great package!

f-liva commented 4 years ago

To add onto the previous answer, we felt it was a bad decision to force a particular way to handle translated data, so we removed it and if you need it, you can choose how to make it work yourself. We aim to be as little opinionated as possible.

If you're curious about how we do it, we use spatie/laravel-translatable on both our models and our Nova Page templates to define which fields contain translated data.

Then, we use mrmonat/nova-translatable to make that translatable data editable in Nova. Note that Spatie themselves have made a similar package, but we found mrmonat's UI was better for our needs.

Also I am closing this since it is not an issue and rather a question.

spatie/nova-translatable required the HasTranslations trait to be added to the model behind the nova resource (in that case the Template), so at the saving of the template we got a Method App\Nova\Templates\Home::setTranslation does not exist. error.

How do you handled the spatie's package here on nova page?

voidgraphics commented 4 years ago

The setTranslation method is a part of the HasTranslations trait from the spatie/laravel-translatable which should indeed be applied on the template class. If it complains that the method does not exist, either you did not apply the trait on your template, or there is a problem with the trait itself.

f-liva commented 4 years ago

HasTranslations must be applied to the Model referenced by a Nova Resource so where is that Model? No way to place a fake setTranslations method, the request doesn't get the values submitted with the form

voidgraphics commented 4 years ago
<?php 

namespace App\Nova\Templates;

class Home extends Template {

    use HasTranslations;

    // ...
}

No need for a fake setTranslations method, it's part of the HasTranslations trait.

f-liva commented 4 years ago

No man

HasTranslations seguire an eloquent model instance

voidgraphics commented 4 years ago

There is no eloquent model. There is a template class instead. I have used the HasTranslation trait exactly the way I showed you before and it works fine.

f-liva commented 4 years ago

Please provide a full example with fields and db. If I use HasTranslations don't work

f-liva commented 4 years ago

I don't know what magic you are doing, but I just updated spatie/nova-translatable to the latest version 4.2.1, and as soon as I open the page editing page I get this error:

Argument 2 passed to Spatie\NovaTranslatable\Translatable::Spatie\NovaTranslatable\{closure}() must be an instance of Illuminate\Database\Eloquent\Model, instance of App\Nova\Templates\Home given

This is my Template class, and don't work.

<?php

namespace App\Nova\Templates;

use Illuminate\Http\Request;
use Laravel\Nova\Fields\Text;
use Spatie\NovaTranslatable\Translatable;
use Spatie\Translatable\HasTranslations;
use Whitecube\NovaPage\Pages\Template;

class Home extends Template
{
    use HasTranslations;

    /**
     * Get the fields displayed by the resource.
     *
     * @param Request $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            Translatable::make([
                Text::make('Foobar'),
            ]),
        ];
    }

    /**
     * Get the cards available for the request.
     *
     * @param Request $request
     * @return array
     */
    public function cards(Request $request)
    {
        return [];
    }
}
gldrenthe89 commented 4 years ago

To add onto the previous answer, we felt it was a bad decision to force a particular way to handle translated data, so we removed it and if you need it, you can choose how to make it work yourself. We aim to be as little opinionated as possible.

If you're curious about how we do it, we use spatie/laravel-translatable on both our models and our Nova Page templates to define which fields contain translated data.

Then, we use mrmonat/nova-translatable to make that translatable data editable in Nova. Note that Spatie themselves have made a similar package, but we found mrmonat's UI was better for our needs.

Also I am closing this since it is not an issue and rather a question.

Do you stil get this to work? cause i am not. what are you doing different?

f-liva commented 4 years ago

That's what I did.

/**
     * Get the fields displayed by the resource.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function fields(Request $request)
    {
        $locales = LaravelLocalization::getLocalesOrder();

        $fields = [];

        foreach ($locales as $localeCode => $properties) {
            $fields[] = Text::make("Text", "text_{$localeCode}");
        }

        return $fields;
    }
gldrenthe89 commented 4 years ago

thanks that worked like a charm

nidhalkratos commented 5 months ago

Hi, I have read this thread, and even though It is called "Localization" everyone was talking about translation, which just one part of localization. When we say localization, it is not just the language. For example, some page section may be completely hidden for some countries, so sections are country specific, ... For example, if you visit apple's website from the US or from Europe, you will not see the same sections visible. For example, you will see the Apple Vision Pro product section only when you visit the site from the US. Now, back to our discussion here, It makes more sense if we made it possible to make some pages locale-specific. We can achieve that by adding a new 'locale' field in the 'pages' table. And then, we let the user specify it from Nova. And then, when loading the page, we can load the correct page based on the locale user's selected locale. With the help true localization packages such as https://github.com/mcamara/laravel-localization we could achieve full localization. This of course in addition to all the things said about the translation in the current thread. We can make this change in a way that it can be optionally enabled in the config file to give more flexibility to people that are interested in true localization. I can make the necessary changes, and create a pull request, the question is, @voidgraphics are you still open to accept pull requests for this package ?