spatie / laravel-tags

Add tags and taggable behaviour to your Laravel app

Home Page:https://freek.dev/609-an-opinionated-tagging-package-for-laravel-apps

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Option to disable translations

Reex11 opened this issue · comments

Hi!
Thank you for creating such a great package.

I (and many others as I imagine) would like to have the option to disable using translations.
Please vote if you need this functionality.

I've created a fork without translations as a temp solution.

commented

+1

Hello guys,

I've a found a workaround for this issue: Setting the locale hardcoded to English. I don't like to copy&paste code from modules, so please give us the ability to deactivate translation.

This new function goes to your model:

public function syncTagsWithoutLocale($tags)
{
    $className = static::getTagClassName();

    $tags = collect($className::findOrCreate($tags, null, 'en'));

    $this->tags()->sync($tags->pluck('id')->toArray());

    return $this;
}

public function scopeWithAnyTagsWithoutLocale($query, $tags, $type = null)
{
    $tags = static::convertToTags($tags, $type, 'en');

    return $query
        ->whereHas('tags', function ($query) use ($tags) {
            $tagIds = collect($tags)->pluck('id');

            $query->whereIn('tags.id', $tagIds);
        });
}

And getting the name by

$tag->getTranslation('name', 'en')

If this can disabled in an easy, and testable way, I'd accept a PR for this. Closing for now as my team won't actively work on this. If you need this, please start a discussion in the ideas category.

My workaround: extend Spatie's tag model with an own tag model (make sure to set this in the package's config) and override the getLocale method:

public static function getLocale()
{
    return 'noi18n';
}

A PR to the original Tag model like the following should do the job:

public static function getLocale()
{
    return config('tags.uses_translations') ? app()->getLocale() : 'noi18n';
}

@SebastianSchoeps it is actually not a workaround, but the updated way of doing it :)

Funny :-). Thanks for your PR! Well, maybe my post helps someone who doesn't RTFM like me but searches the issues...

In some projects (especially those that include international terms) we wish to avoid tag translation.
Since this is my case, I kept the translation feature by adding the tag translation on-the-go. Check it out:

        $words = array('apple', 'orange', 'kiwi', 'banana', 'strawberry');
        foreach($words as $word)
        {
            $tag = \Spatie\Tags\Tag::findOrCreate($word); 
            $tag->setTranslation('name', 'en', $word);
            $tag->setTranslation('name', 'el', $word);
            $tag->save();
            $photo->attachTag($tag);
        }

It will fill the database with entries like this
name ->{"el": "apple", "en": "apple"}
slug -> {"el": "apple", "en": "apple"}

This way, switching language from English to Greek, the content to be displayed will always be the same.
PLUS: I have the translation functionality available, for those cases where I actually need to do tag translation.
And all this without hacking or overriding the core code.