Skip to content

Commit 5d56f90

Browse files
authored
Fix file manager language (#79)
* fix file manager languages
1 parent c05bc53 commit 5d56f90

File tree

5 files changed

+78
-26
lines changed

5 files changed

+78
-26
lines changed

resources/views/elfinder.blade.php

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
11
@extends(backpack_view('blank'))
22

33
@section('after_scripts')
4-
5-
@include('backpack.filemanager::common_scripts')
6-
@include('backpack.filemanager::common_styles')
7-
8-
<!-- elFinder initialization (REQUIRED) -->
9-
<script type="text/javascript" charset="utf-8">
10-
// Documentation for client options:
11-
// https://github.com/Studio-42/elFinder/wiki/Client-configuration-options
12-
$(document).ready(function() {
13-
$('#elfinder').elfinder({
14-
// set your elFinder options here
15-
@if($locale)
16-
lang: '{{ $locale }}', // locale
4+
@include('backpack.filemanager::localization')
5+
6+
@include('backpack.filemanager::common_scripts', ['locale' => in_array($locale, array_keys($elfinderConfiguredLanguages)) ? $locale : null])
7+
@include('backpack.filemanager::common_styles')
8+
9+
<!-- elFinder initialization (REQUIRED) -->
10+
<script type="text/javascript" charset="utf-8">
11+
// Documentation for client options:
12+
// https://github.com/Studio-42/elFinder/wiki/Client-configuration-options
13+
$(document).ready(function() {
14+
$('#elfinder').elfinder({
15+
// set your elFinder options here
16+
commandsOptions: {
17+
preference: {
18+
langs: @json($elfinderConfiguredLanguages)
19+
}
20+
},
21+
@if($locale)
22+
lang: '{{ $locale }}', // locale
23+
@if($locale !== 'en')
24+
i18nBaseUrl: '{{ \Illuminate\Support\Str::beforeLast(Basset::getUrl("bp-elfinder-i18n-".$locale), "elfinder.") }}/',
1725
@endif
18-
customData: {
19-
_token: '{{ csrf_token() }}'
20-
},
21-
url : '{{ route("elfinder.connector") }}', // connector URL
22-
soundPath: '{{ Basset::getUrl(base_path("vendor/studio-42/elfinder/sounds")) }}',
23-
cssAutoLoad : false,
24-
});
26+
@endif
27+
customData: {
28+
_token: '{{ csrf_token() }}'
29+
},
30+
url : '{{ route("elfinder.connector") }}', // connector URL
31+
soundPath: '{{ Basset::getUrl(base_path("vendor/studio-42/elfinder/sounds")) }}',
32+
cssAutoLoad: false,
2533
});
26-
</script>
34+
});
35+
</script>
2736
@endsection
2837

2938
@php
@@ -46,8 +55,6 @@
4655
@endsection
4756

4857
@section('content')
49-
5058
<!-- Element where elFinder will be created (REQUIRED) -->
5159
<div id="elfinder"></div>
52-
5360
@endsection
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@php
2+
$elfinderLanguages = [
3+
'ar', 'bg', 'ca', 'cs', 'da', 'de', 'el', 'en', 'es', 'fa', 'fo', 'fr', 'fr_CA', 'he', 'hr', 'hu', 'id', 'it', 'ja', 'ko', 'nl', 'no', 'pl', 'pt_BR', 'ro', 'ru', 'si', 'sk', 'sl', 'sr', 'sv', 'tr', 'ug_CN', 'uk', 'vi', 'zh_CN', 'zh_TW',
4+
];
5+
$locales = config('backpack.crud.locales', []);
6+
$elfinderConfiguredLanguages = [];
7+
foreach ($locales as $code => $name) {
8+
if (in_array($code, $elfinderLanguages)) {
9+
$elfinderConfiguredLanguages[$code] = $name;
10+
}
11+
}
12+
// Add English if not present, as it's the fallback
13+
if (!array_key_exists('en', $elfinderConfiguredLanguages)) {
14+
$elfinderConfiguredLanguages['en'] = 'English';
15+
}
16+
17+
// Pre-load other configured languages so they are available when switching in UI
18+
foreach ($elfinderConfiguredLanguages as $lang => $name) {
19+
if ($lang !== 'en' && $lang !== $locale) {
20+
try {
21+
\Backpack\Basset\Facades\Basset::basset('bp-elfinder-i18n-'.$lang, false);
22+
} catch (\Throwable $e) {}
23+
}
24+
}
25+
@endphp

resources/views/standalonepopup.blade.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<!DOCTYPE html>
22
<html lang="{{ app()->getLocale() }}">
33
<head>
4-
5-
@include('backpack.filemanager::common_scripts')
4+
@include('backpack.filemanager::localization')
5+
@include('backpack.filemanager::common_scripts', ['locale' => in_array($locale, array_keys($elfinderConfiguredLanguages)) ? $locale : null])
66
@include('backpack.filemanager::common_styles', ['styleBodyElement' => true])
77
<style type="text/css">
88
.elfinder-workzone {
@@ -25,6 +25,9 @@
2525
// set your elFinder options here
2626
@if($locale)
2727
lang: '{{ $locale }}', // locale
28+
@if($locale !== 'en')
29+
i18nBaseUrl: '{{ \Illuminate\Support\Str::beforeLast(Basset::getUrl("bp-elfinder-i18n-".$locale), ".elfinder") }}/',
30+
@endif
2831
@endif
2932
customData: {
3033
_token: '{{ csrf_token() }}'
@@ -37,6 +40,9 @@
3740
getfile: {
3841
multiple: {{ request('multiple') ? 'true' : 'false' }},
3942
oncomplete: 'destroy'
43+
},
44+
preference: {
45+
langs: @json($elfinderConfiguredLanguages)
4046
}
4147
},
4248
getFileCallback: (file) => {

src/BackpackElfinderController.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Backpack\FileManager;
44

5+
use Backpack\Basset\Facades\Basset;
56
use Illuminate\Support\Facades\Crypt;
67
use Illuminate\Support\Facades\Log;
78

@@ -41,4 +42,17 @@ public function showIndex()
4142
->make('backpack.filemanager::elfinder')
4243
->with($this->getViewVars());
4344
}
45+
46+
protected function getViewVars()
47+
{
48+
$locale = str_replace('-', '_', $this->app->config->get('app.locale'));
49+
50+
if (! array_key_exists('bp-elfinder-i18n-'.$locale, Basset::getNamedAssets())) {
51+
$locale = false;
52+
}
53+
54+
$csrf = true;
55+
56+
return compact('locale', 'csrf');
57+
}
4458
}

src/FileManagerServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function boot()
3333
// Fallback to package views
3434
$this->loadViewsFrom(__DIR__.'/../resources/views', 'backpack.filemanager');
3535

36-
$crudLanguages = array_keys(config('backpack.crud.languages', []));
36+
$crudLanguages = array_keys(config('backpack.crud.locales', []));
3737
foreach ($crudLanguages as $language) {
3838
if ($language === 'en') {
3939
continue;

0 commit comments

Comments
 (0)