trinityrank/multilanguage

Add alternate multilanguage tags for same pages but on other language

v1.0.0 2022-11-22 16:37 UTC

This package is auto-updated.

Last update: 2024-10-23 16:21:04 UTC


README

Latest Version on Packagist Total Downloads

Add alternate multilanguage tags for same pages but on other language.

Installation

Step 1: Install package

To get started with Laravel Multilanguage, use Composer command to add the package to your composer.json project's dependencies:

For Frontend and backend

    composer require trinityrank/multilanguage

Only for backend we need this package too

    composer require epartment/nova-dependency-container

Laravel Nova admin - Backend part

Step 2: Database

  • You need to publish migration from package
    php artisan vendor:publish --provider="Trinityrank\Multilanguage\MultilanguageServiceProvider" --tag="multilanguage-migration"
  • And then you need to run migration for alltenant(s)
    php artisan tenant:artisan "migrate"
  • Or only for one speciffic tenant
    php artisan tenant:artisan "migrate" --tenant=[--TENANT-ID--]

Step 3: Update database with default language

  • Update database field "multilang_language" to default language for your website
    UPDATE `articles` SET `multilang_language`='us' WHERE 1;
    UPDATE `pages` SET `multilang_language`='us' WHERE 1;
    UPDATE `categories` SET `multilang_language`='us' WHERE 1;
    UPDATE `static_pages` SET `multilang_language`='us' WHERE 1;

Step 4: Add field

  • Add field to your (Operater) resource into "fields" method
    use Trinityrank\Multilanguage\MultilanguagePanel;
    
    ...
    
    MultilanguagePanel::make()
  • Or if you use conditional fields than just add this into "fields" method
    // use "$this" or "self::", depends of resource structure
    $this->getMultilanguagePanel('Multilanguage', 'multilanguage')
  • Fields where category depends on language select
    // use "$this" or "self::", depends of resource structure
    // $this->getMultilanguageCategory("Language", [Resource::class, Model::class, ['rules']]),
    // example:
    $this->getMultilanguageCategory("Language", [ReviewPageCategory::class, TypesReviewPageCategory::class, ['required']]),

Step 5: If you are using conditional fields

Add this in tenant-default config

    'conditional_fields' => [
        ...

        '{{your_page_type_name}}' => [
            'categories' => [
                'visible' => true,
                'rules' => ['required', 'min:1', 'max:1', ... ]
            ],
        ]

        ...
    ]

Add this in tenant-{{tenant name}} config

    'conditional_fields' => [
        ...

        '{{your_page_type_name}}' => [
            'language' => [
                'visible' => true
            ],
            'categories' => [
                'visible' => true,
                'visibility' => ['onlyOnIndex'],
                'rules' => ['required', 'min:1', 'max:1']
            ],
        ]

        ...
    ]

Step 6: Add languages

In your "config\app.php" add multilanguage locales (use ISO language codes). For example:

    'locales' => [
        "us" => "USA",
        "uk" => "Great Britain",
        "ca" => "Canada",
        "au" => "Australia",
        "de" => "German",
        "at" => "Austria",
    ],

Frontend part

Step 7: Frontend part

Add helper function to "composer.json" file

        "autoload": {
            "files": [
                "vendor/trinityrank/multilanguage/src/Frontend/helpers.php"
            ],
            ...
        } 

And then run:

    composer dump-autoload

And change default "route()" method to "multilang_route()"

Step 8: Add languages

In your "config\app.php" add multilanguage locales (use ISO language codes). For example:

    'locales' => ['us', 'uk', 'ca', 'au', 'de', 'at'],

Step 9: Add hreflang metatags

  • For hreflang metatags add this to yout master.blade file in head tag
    <!-- Multilanguage alternate link tags -->
    {!! Trinityrank\Multilanguage\Frontend\HreflangDisplay::meta_tags($item) !!}
  • At the bottom of app.php add this part of code and change values according to the website settings
    // Url structure scheme (same as multicore tenant config file)
    'services' => [
        'deals' => false,
        'MoneyPage' => [
            'include_category_in_url' => false,
            'slug' => 'best',
        ],
        'News' => [
            'include_category_in_url' => false,
            'slug' => 'news',
        ],
        'Blog' => [
            'include_category_in_url' => false,
            'slug' => false,
        ],
        'ReviewPage' => [
            'include_category_in_url' => false,
            'slug' => 'reviews',
        ]
    ],