Try:
<label for="email">
{{ __('login')['email'] }}
</label>
You can get this approach by changing the default helper by:
use Illuminate\Support\Arr;
function ($key = null, $replace = [], $locale = null)
{
// Default behavior
if (is_null($key)) return $key;
if (trans()->has($key)) return trans($key, $replace, $locale);
// Search in .json file
$search = Arr::get(trans()->get('*'), $key);
if ($search !== null) {-code-8};
// Return .json fallback
$fallback = Arr::get(trans()->get('*', [], config('app.fallback_locale')), $key);
if ($fallback !== null) {-code-10};
// Return key name if not found
else return $key;
}
if you dont know how to change the default helper, create a file with any name (ex:{-code-3}
), then in the file{-code-4}
add this line just before 'Register The Auto Loader'
/*
|
if you also want to use the Variables feature, just like__(welcome.user, ['user'=>'david'])
you must create a new helper on that file:
{-code-7}
and then replace{-code-8}
with{-code-9}
and{-code-10}
with{-code-11}
for Countable feature (ex:{-code-12}
), is the same process, just add this helper:
use Illuminate\Support\Facades\App;
function trans_choice($key, $number, array $replace = [], $locale = null)
{
// Get message
$message = __($key, $replace, $locale);
// If the given "number" is actually an array or countable we will simply count the
// number of elements in an instance.
if (is_array($number) || $number instanceof Countable)
$number = count($number);
$replace['count'] = $number;
return trans_replacements(
trans()->getSelector()->choose($message, $number, $locale = App::getLocale()),
$replace
);
}
here is the file, just in case: bendeckdavid/laravel_locale_nested_json
Laravel not supporting this by default is annoying. However, you can use this function I made:
function ___($path) {
$properties = explode(".", $path);
$base = __($properties[0]);
unset($properties[0]);
foreach($properties as $property) {
// Allows specification of indexes
if(is_numeric($property)) {
$property = intval($property);
}
// If the key has not been found, return the initial parameter like __()
try {
$base = $base[$property];
} catch (\Throwable $th) {
return $path;
}
}
return $base;
}
If you are someone like me stumbling upon here wanting to use a file structure like below without the need to specify keys for each string when using php language files.
-app
-lang
--fr
---Headers.json
A little modification to David G's answer above,
<?php
use Illuminate\Support\Arr;
function ___($path = null, $replace = [], $locale = null)
{
// Default behavior
if (is_null($path)) return $path;
if(!strrpos($path, '.')) {
return $path;
}
$properties = explode(".", $path);
$currentLocale = app()->getLocale();
// If the file/folder doesn't exist
// return the last part of the dot notation
$translated_string = substr($path, strrpos($path, '.') + 1);
if(is_dir(lang_path($currentLocale))) {
if (is_file(lang_path($currentLocale.'\\'.$properties[0].'.json'))) {
$contents = json_decode(file_get_contents(lang_path($currentLocale.'\\'.$properties[0].'.json')), true);
// If no value exists for the key, the key is itself returned
$translated_string = Arr::get($contents, $properties[1], $properties[1]);
}
}
return $translated_string;
}
As of now this doesn't work for files under subdirectories under the locale folder. I am sure, the above code can be optimized, but this should give you an idea.
Also, this function uses triple-underscore{{___()}}
instead of the regular double-underscore{{__()}}
to avoid conflicts.
Works for,
{{___('Messages.Welcome') }} // lang/(locale)/Messages.json
{{___('Welcome')}} // lang/(locale).json
Assuming your json is stored in$json
, you want to parse it into array by usingjson_decode
$data = json_decode($json, TRUE);
You can access it by:
<label for="email">
{{ $data['login']['emailPlaceholder'] }}
</label>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Bootstrap is not exclusively a CSS framework, but its most popular features are CSS-centric. These include a powerful grid, icons, buttons, map components, navigation bars, and more.
https://getbootstrap.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.