Перейти до основного вмісту

Use in Blade

Current language:​

{{evo()->getLocale()}}
or
{{evo()->getConfig('lang')}}

Default language:​

{{evo()->getConfig('s_lang_default')}}

List of frontend languages by comma:​

{{evo()->getConfig('s_lang_front')}}

Translation of phrases:​

In Blade:
@lang('phrase')

In Controller:
__('phrase')

Localized versions of your page for Google hreflang​

{!!sLang::hreflang()!!}

Language Switcher​

Show current language anywhere with name or shortname

{{Str::upper(sLang::langSwitcher()[evo()->getConfig('lang')]['short'])}}

Implementing a Language Switcher in Blade template

@foreach(sLang::langSwitcher() as $lang)
<a href="{{$lang['link']}}">{{Str::upper($lang['short'])}}</a>
@endforeach

Example returns langSwitcher

^ array:2 [▼
"uk" => array:6 [▼
"name" => "Українська"
"short" => "Укр"
"ISO 639-1" => "uk"
"ISO 639-3" => "ukr"
"country" => "Ukraine"
"link" => "https://example.com/"
]
"en" => array:6 [▼
"name" => "English"
"short" => "Eng"
"ISO 639-1" => "en"
"ISO 639-3" => "eng"
"country" => "English"
"link" => "https://example.com/en/"
]
]

By default, sLang offers 2 menu areas. This is the Main Menu and the Footer Menu. These areas are built on TV menu_main and menu_footer parameters and displayed in the resource settings tab.

Data preparation in BaseController.php

use Seiger\sLang\Models\sLangContent;

...

public function globalElements()
{
// Tree menu
$this->data['mainMenu'] = sLangContent::withTVs(['tv_image'])
->where('hidemenu', 0)
->whereTv('menu_main', 1)
->orderBy('parent_id')
->orderBy('menuindex')
->active()
->get()
->toTreeParent(0);

// Simple menu
$this->data['mainMenu'] = sLangContent::withTVs(['tv_image'])
->whereTv('menu_main', 1)
->where('hidemenu', 0)
->orderBy('menuindex')
->active()
->get();

$this->data['footerMenu'] = sLangContent::whereTv('menu_footer', 1)
->where('hidemenu', 0)
->orderBy('menuindex')
->active()
->get();
}

Output in the Blade template

@if($mainMenu)
<ul>
@foreach($mainMenu as $menu)
<li>
@if($menu->id == evo()->documentObject['id'])
<a>{{$menu->menutitle}}</a>
@else
<a href="{{$menu->fullLink}}" {!! $menu->linkAttributes !!}>{{$menu->menutitle}}</a>
@endif

@if($menu->children->count())
<ul>
@foreach($menu->children as $child)
<li>
@if($child->id == evo()->documentObject['id'])
<a>{{$child->menutitle}}</a>
@else
<a href="{{$child->fullLink}}" {!! $child->linkAttributes !!}>{{$child->menutitle}}</a>
@endif
</li>
@endforeach
</ul>
@endif
</li>
@endforeach
</ul>
@endif

For unlimited nesting, move the menu item into a recursive partial.

views/partials/menu-item.blade.php

<li>
@if($item->id == evo()->documentObject['id'])
<a>{{$item->menutitle}}</a>
@else
<a href="{{$item->fullLink}}" {!!$item->linkAttributes!!}>{{$item->menutitle}}</a>
@endif

@if($item->children->count())
<ul>
@foreach($item->children as $child)
@include('partials.menu-item', ['item' => $child])
@endforeach
</ul>
@endif
</li>

Usage:

@if($mainMenu)
<ul>
@foreach($mainMenu as $item)
@include('partials.menu-item', ['item' => $item])
@endforeach
</ul>
@endif

TV variables​

The withTVs() scope makes it easy to retrieve TV parameters associated with a resource. For example, the tv_image parameter.

Get in the controller.

$resource = sLangContent::withTVs(['tv_image'])->active()->first();

Display in the template.

{{$resource->tv_image}}

The whereTv() method allows you to use a filter based on the value of the TV parameter if necessary.

$resources = sLangContent::withTVs(['tv_image'])->whereTv('tv_image', '!=', '')->get();

Combine multiple helpers:

$resources = sLangContent::lang('uk')->withTVs(['color', 'price'])->whereParent($parentId)->active()->get();

Deprecated: The langAndTvs() helper is deprecated since 1.0.8 and will be removed in v1.2. Replace it with the lang() and withTVs() scopes.

Resource fields in Admin panel​

You can control the display of resource fields on general tabs through an event sLangDocFormFieldRender.

Usage example from your plugin:

Event::listen('evolution.sLangDocFormFieldRender', function($params) {
if ($params['content']['template'] == 7) {
if ($params['name'] == 'introtext') {
return view('slang.introtext', $params)->render();
}
}
});