From e9944f2460217d5709ecfb955fe9e7f13d47da7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=9Cmit=20Tun=C3=A7?= Date: Wed, 1 Oct 2025 04:14:46 -0300 Subject: [PATCH] Add translation helper functions: Introduced a new file for translation-related helper functions to enhance localization capabilities. Updated composer.json to autoload the new helper file, ensuring easy access to functions for managing languages, translations, and their statuses. --- app/Helpers/translation_helpers.php | 284 ++++++++++++++++++++++++++++ composer.json | 5 +- 2 files changed, 288 insertions(+), 1 deletion(-) create mode 100644 app/Helpers/translation_helpers.php diff --git a/app/Helpers/translation_helpers.php b/app/Helpers/translation_helpers.php new file mode 100644 index 0000000..d71464b --- /dev/null +++ b/app/Helpers/translation_helpers.php @@ -0,0 +1,284 @@ +getLocale()); + } +} + +if (!function_exists('current_language_code')) { + /** + * Get current language code + */ + function current_language_code(): string + { + return app()->getLocale(); + } +} + +if (!function_exists('default_language')) { + /** + * Get default language + */ + function default_language(): ?Language + { + return Language::getDefault(); + } +} + +if (!function_exists('default_language_code')) { + /** + * Get default language code + */ + function default_language_code(): string + { + $defaultLanguage = Language::getDefault(); + return $defaultLanguage ? $defaultLanguage->code : config('app.locale'); + } +} + +if (!function_exists('available_languages')) { + /** + * Get all active languages + */ + function available_languages(): Collection + { + return Language::getActive(); + } +} + +if (!function_exists('available_language_codes')) { + /** + * Get all active language codes + */ + function available_language_codes(): array + { + return Language::active()->pluck('code')->toArray(); + } +} + +if (!function_exists('translate_model')) { + /** + * Get translated value for a model field + * + * @param mixed $model Model instance + * @param string $field Field name to translate + * @param string|null $languageCode Language code (null for current) + * @param bool $fallback Use fallback to default language + * @return mixed + */ + function translate_model($model, string $field, ?string $languageCode = null, bool $fallback = true): mixed + { + if (!method_exists($model, 'translate')) { + return $model->{$field} ?? null; + } + + return $model->translate($field, $languageCode, $fallback); + } +} + +if (!function_exists('has_translation')) { + /** + * Check if model has translation for field and language + */ + function has_translation($model, string $field, ?string $languageCode = null): bool + { + if (!method_exists($model, 'hasTranslation')) { + return false; + } + + $languageCode = $languageCode ?? current_language_code(); + return $model->hasTranslation($field, $languageCode); + } +} + +if (!function_exists('translation_progress')) { + /** + * Get translation progress for a model + */ + function translation_progress($model, ?string $languageCode = null): int + { + if (!method_exists($model, 'getTranslationProgress')) { + return 0; + } + + $progress = $model->getTranslationProgress(); + + if ($languageCode) { + return $progress[$languageCode] ?? 0; + } + + return $progress; + } +} + +if (!function_exists('user_can_manage_language')) { + /** + * Check if current user can manage a language + */ + function user_can_manage_language(string $languageCode): bool + { + $user = auth()->user(); + + if (!$user || !method_exists($user, 'canManageLanguage')) { + return false; + } + + return $user->canManageLanguage($languageCode); + } +} + +if (!function_exists('user_managed_languages')) { + /** + * Get languages that current user can manage + */ + function user_managed_languages(): Collection + { + $user = auth()->user(); + + if (!$user || !method_exists($user, 'getManagedLanguages')) { + return collect(); + } + + return $user->getManagedLanguages(); + } +} + +if (!function_exists('user_managed_language_codes')) { + /** + * Get language codes that current user can manage + */ + function user_managed_language_codes(): array + { + $user = auth()->user(); + + if (!$user || !method_exists($user, 'getManagedLanguageCodes')) { + return []; + } + + return $user->getManagedLanguageCodes(); + } +} + +if (!function_exists('switch_language')) { + /** + * Switch application language + */ + function switch_language(string $languageCode): bool + { + $language = Language::findByCode($languageCode); + + if (!$language || !$language->is_active) { + return false; + } + + app()->setLocale($languageCode); + + if (auth()->check()) { + session(['locale' => $languageCode]); + } + + return true; + } +} + +if (!function_exists('get_translation_status_badge')) { + /** + * Get translation status badge HTML + */ + function get_translation_status_badge(string $status): string + { + $badges = [ + 'draft' => 'Draft', + 'review' => 'Review', + 'approved' => 'Approved', + 'published' => 'Published', + ]; + + return $badges[$status] ?? $badges['draft']; + } +} + +if (!function_exists('get_translation_status_color')) { + /** + * Get translation status color + */ + function get_translation_status_color(string $status): string + { + $colors = [ + 'draft' => 'gray', + 'review' => 'warning', + 'approved' => 'success', + 'published' => 'primary', + ]; + + return $colors[$status] ?? 'gray'; + } +} + +if (!function_exists('get_language_flag')) { + /** + * Get language flag emoji or icon + */ + function get_language_flag(string $languageCode): string + { + $language = Language::findByCode($languageCode); + return $language ? ($language->flag ?? '🌐') : '🌐'; + } +} + +if (!function_exists('format_language_name')) { + /** + * Format language name with native name + */ + function format_language_name(string $languageCode): string + { + $language = Language::findByCode($languageCode); + + if (!$language) { + return $languageCode; + } + + return "{$language->name} ({$language->native_name})"; + } +} + +if (!function_exists('translation_exists')) { + /** + * Check if a translation exists in database + */ + function translation_exists(string $modelClass, int $modelId, string $field, string $languageCode): bool + { + return Translation::forModel($modelClass, $modelId) + ->forField($field) + ->forLanguage($languageCode) + ->exists(); + } +} + +if (!function_exists('get_translation_value')) { + /** + * Get translation value directly from Translation model + */ + function get_translation_value(string $modelClass, int $modelId, string $field, ?string $languageCode = null): ?string + { + $languageCode = $languageCode ?? current_language_code(); + + $translation = Translation::forModel($modelClass, $modelId) + ->forField($field) + ->forLanguage($languageCode) + ->published() + ->first(); + + return $translation ? $translation->field_value : null; + } +} + diff --git a/composer.json b/composer.json index d6aa7cd..b3ff955 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,10 @@ "App\\": "app/", "Database\\Factories\\": "database/factories/", "Database\\Seeders\\": "database/seeders/" - } + }, + "files": [ + "app/Helpers/translation_helpers.php" + ] }, "autoload-dev": { "psr-4": {