[11.x] Optimize loadTranslationsFrom
function for simplicity and clarity
#54407
+127
−6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Current Behavior
When developing packages or performing specific translation loading operations, there are two methods available for loading translations:
loadJsonTranslationsFrom
andloadTranslationsFrom
. Currently, it is not possible to register translations without a namespace using theloadTranslationsFrom
method.Issue
When using the
loadTranslationsFrom
method to register a translation, the only way to use it with the__()
,trans()
, ortrans_choice()
methods is by specifying the namespace, like namespace::file.foo.bar. This often results in repetitive and long names when developing packages, making the code cumbersome and less readable.Example :
Although this issue can be resolved using
loadJsonTranslationsFrom()
, it may cause specific problems when multiple packages are used, and JSON translations do not offer the same functionality as theloadTranslationsFrom()
method.Proposed Change
We propose adding an
addPath
method to theTranslator
andFileLoader
classes and making the$namespace
parameter in theServiceProvider
class'sloadTranslationsFrom
method default to null. When anull
value is provided, the directory should be included in the translation loader without needing a namespace.This change will not break backward compatibility and will allow developers to load external translation directories without a namespace, preserving the original behavior.
Backward Compatibility
This pull request does not create any backwards incompatibility. Added some specific tests to ensure it won't work.