From 7e917680dd0f8cf8ed8fba8cb30fc1677429b897 Mon Sep 17 00:00:00 2001 From: bbdoc Date: Thu, 5 Dec 2024 16:12:28 +0100 Subject: [PATCH] Translate Form Names --- include/cache_handler.php | 15 +++++++++++++++ include/functions.php | 25 +++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/include/cache_handler.php b/include/cache_handler.php index c089dbe..1e43be4 100644 --- a/include/cache_handler.php +++ b/include/cache_handler.php @@ -14,6 +14,8 @@ global $file_localePkmnData; $file_localePkmnData = "./.cache/localePkmnData_".$locale.".json"; +global $file_localeFormsData; +$file_localeFormsData = "./.cache/localeFormsData_".$locale.".json"; global $file_localeItemsData; $file_localeItemsData = "./.cache/localeItemsData_".$locale.".json"; @@ -129,6 +131,19 @@ $localePkmnData_json = file_get_contents($repo_locales."pokemon_en.json"); } +// Cache FormsNames locale file + +global $localeFormsData_json; +if (file_exists($file_localeFormsData) && (filemtime($file_localeFormsData) > (time() - 60 * 60 * $repo_locales_cache ))) { + $localeFormsData_json = file_get_contents($file_localeFormsData); +} else if ( @fopen($repo_locales."/forms_".$locale.".json", 'r') ) { + $localeFormsData_json = file_get_contents($repo_locales."/forms_".$locale.".json"); + file_put_contents($file_localeFormsData, $localeFormsData_json); +} else if (isset($locale)) { + $localeFormsData_json = file_get_contents($repo_locales."forms_en.json"); +} + + // Cache itemNames locale file global $localeItemsData_json; diff --git a/include/functions.php b/include/functions.php index d7db67f..ce895a2 100644 --- a/include/functions.php +++ b/include/functions.php @@ -21,7 +21,7 @@ function get_form_name($pokemon_id, $form_id) { if ($pokemon['id'] == "$pokemon_id") { if ( $pokemon['form']['id'] == "$form_id" && $pokemon['form']['id'] <> 0) { - return $pokemon['form']['name']; + return translate_form($pokemon['form']['name']); } } } @@ -51,7 +51,7 @@ function get_all_forms($pokemon_id) { if ($pokemon['id'] == "$pokemon_id") { if ( $pokemon['form']['id'] <> "0" && !in_array( ucfirst($pokemon['form']['name']), $form_exclude ) ) { - $forms[$pokemon['form']['id']] = $pokemon['form']['name']; + $forms[$pokemon['form']['id']] = translate_form($pokemon['form']['name']); } } } @@ -161,6 +161,27 @@ function translate_mon($word) } } +function translate_form($word) +{ + $locale = @$_SESSION['locale']; + if ($locale == "en") { + return $word; exit(); + } + + global $localeFormsData; + global $localeFormsData_json; + + if ($localeFormsData == null) { + $localeFormsData = json_decode($localeFormsData_json, true); + } + + if (isset($localeFormsData[$word])) { + return $localeFormsData[$word]; + } else { + return $word; + } +} + function translate_item($word) { $locale = @$_SESSION['locale'];