Callback for handling missing translation keys (Suggestion/proposed code changes) #48862
-
I would like the ability to customise the behaviour of how missing translations are handled. The current behaviour is to return the key unchanged. For example The use case for this is: I would like to add the ability to trigger a log message if a translation key is missed in production to detect when backend-like text such as There are packages to solve this problem using static analysis such as https://github.com/LarsWiegers/laravel-translations-checker, however static analysis is limited and can not pick up run-time generated keys, e.g. I have made a commit on my local clone of the diff --git a/src/Illuminate/Translation/Translator.php b/src/Illuminate/Translation/Translator.php
index e29ba93a2d..f80886aca2 100755
--- a/src/Illuminate/Translation/Translator.php
+++ b/src/Illuminate/Translation/Translator.php
@@ -65,6 +65,13 @@ class Translator extends NamespacedItemResolver implements TranslatorContract
*/
protected $stringableHandlers = [];
+ /**
+ * The callback that is responsible for handling missing translation keys.
+ *
+ * @var callable|null
+ */
+ protected static $missingTranslationKeyCallback;
+
/**
* Create a new translator instance.
*
@@ -153,6 +160,7 @@ public function get($key, array $replace = [], $locale = null, $fallback = true)
return $line;
}
}
+ $key = $this->handleMissingTranslationKey($key, $replace, $locale, $fallback);
}
// If the line doesn't exist, we will return back the key which was requested as
@@ -161,6 +169,36 @@ public function get($key, array $replace = [], $locale = null, $fallback = true)
return $this->makeReplacements($line ?: $key, $replace);
}
+ /**
+ * Handle a missing translation key.
+ *
+ * @param string $key
+ * @param array $replace
+ * @param string|null $locale
+ * @param bool $fallback
+ * @return string
+ */
+ public function handleMissingTranslationKey($key, $replace, $locale, $fallback)
+ {
+ if (isset(static::$missingTranslationKeyCallback)) {
+ return call_user_func(static::$missingTranslationKeyCallback,
+ $key, $replace, $locale, $fallback
+ );
+ }
+ return $key;
+ }
+
+ /**
+ * Register a callback that is responsible for handling missing translation keys.
+ *
+ * @param callable|null $callback
+ * @return void
+ */
+ public static function handleMissingTranslationKeyUsing(?callable $callback)
+ {
+ static::$missingTranslationKeyCallback = $callback;
+ }
+
/**
* Get a translation according to an integer value.
* Example of usage in any service provider: // Edit: the syntax has changed since this feature was originally proposed.
// Please see docs at https://laravel.com/docs/10.x/localization#handling-missing-translation-strings
Translator::handleMissingTranslationKeyUsing(function ($key) {
Log::error("Missing translation key [$key] detected.");
}); |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
To make the code easier to read you could tell GitHub which language it is: To make a PR you should:
The easiest way to get started is a GUI application like the GitHub Desktop (Linux/MacOS/Windows). |
Beta Was this translation helpful? Give feedback.
-
I think that this is a very useful addition, and also one that Taylor might well be willing to accept. Please go ahead and create a PR. The only thing I would personally like to see in addition to the current functionality is the ability to replace the default string returned to the UI i.e. the key with an alternative string i.e. it's a live application, and it is displaying an error message but there is no translation for it, then instead of displaying something cryptic like "sausageservice.errorflourtypeunknown" I might like to analyse the key, find it has the characters ".error" in it and replace it with "An error occurred in this field, but we don't have a translation for your locale" - and I might want to translate the replacement string too (but avoid infinite loops). |
Beta Was this translation helpful? Give feedback.
-
Thank you both. I have created a PR here: #49040 @Sophist-UK My changes in the PR offers functionality to override the string returned when the translation string is missing. The string returned by the callback is used. As for translating the replacement string, the user can do this in the callback they define. As you mentioned, infinite loops could occur, so I've made some changes in the PR to prevent this. Thank you for the suggestion. |
Beta Was this translation helpful? Give feedback.
Thank you both.
I have created a PR here: #49040
@Sophist-UK My changes in the PR offers functionality to override the string returned when the translation string is missing. The string returned by the callback is used. As for translating the replacement string, the user can do this in the callback they define. As you mentioned, infinite loops could occur, so I've made some changes in the PR to prevent this. Thank you for the suggestion.