Skip to content

Commit

Permalink
Supports multi-country locales on Translatable
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioribeiro authored and ifox committed Jul 20, 2021
1 parent 5d9c995 commit c5b341d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
12 changes: 0 additions & 12 deletions src/Helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,3 @@ function capsules($capsule = null) {
return $manager;
}
}

if (!function_exists('twill_locales')) {
function twill_locales() {
return $allLanguages = collect(config('translatable.locales'))->map(function ($locale, $index) {
return collect($locale)->map(function ($country) use ($locale, $index) {
return is_numeric($index)
? $locale
: "$index-$country";
});
})->flatten()->toArray();
}
}
16 changes: 14 additions & 2 deletions src/Helpers/i18n_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@ function twillTrans($key, $replace = [])
*/
function getLocales()
{
return twill_locales() ?? [config('app.locale')];
$locales = collect(config('translatable.locales'))->map(function ($locale, $index) {
return collect($locale)->map(function ($country) use ($locale, $index) {
return is_numeric($index)
? $locale
: "$index-$country";
});
})->flatten()->toArray();

if (blank($locales)) {
$locales = [config('app.locale')];
}

return $locales;
}
}

Expand All @@ -30,7 +42,7 @@ function getLanguagesForVueStore($form_fields = [], $translate = true)
{
$manageMultipleLanguages = count(getLocales()) > 1;
if ($manageMultipleLanguages && $translate) {
$allLanguages = Collection::make(twill_locales())->map(function ($locale, $index) use ($form_fields) {
$allLanguages = Collection::make(getLocales())->map(function ($locale, $index) use ($form_fields) {
return [
'shortlabel' => strtoupper($locale),
'label' => getLanguageLabelFromLocaleCode($locale),
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/Admin/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ protected function getIndexTableColumns($items)
'html' => $column['html'] ?? false,
]);
}
if ($this->moduleHas('translations') && count(twill_locales()) > 1) {
if ($this->moduleHas('translations') && count(getLocales()) > 1) {
array_push($tableColumns, [
'name' => 'languages',
'label' => twillTrans('twill::lang.listing.languages'),
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Behaviors/HasTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public function getActiveLanguages()
'published' => $translation->active ?? false,
];
})->sortBy(function ($translation) {
$localesOrdered = twill_locales();
$localesOrdered = getLocales();
return array_search($translation['value'], $localesOrdered);
})->values();
}
Expand Down

0 comments on commit c5b341d

Please sign in to comment.