Frontend Flashcards
(58 cards)
What is Laravel Mix?
Laravel Mix provides a fluent API for defining webpack build steps for your Laravel application using several common CSS and JavaScript pre-processors
By default, the Laravel application skeleton does not include the lang directory. If you would like to customize Laravel’s language files, how can you publish them using Artisan?
php artisan lang:publish
How is the handling of different languages handled in Laravel?
In the lang directory, files may be stored in their own subdirectories for each language supported by the application. This is the approach Laravel uses to manage translation strings for built-in Laravel features such as validation error messages.
If you, for example, want to support English and Spanish for a messages.php file, you could have the directories lang/en/messages.php and lang/es/messages.php
Translation strings may be defined within JSON files that are placed within the lang directory directly. When taking this approach, each language supported by your application would have a corresponding JSON file within this directory. This approach is recommended for applications that have a large number of translatable strings.
In the config/app.php file the default language for your application is stored under its locale configuration option. Which environmental variables are used to set the default and fallback languages and what is the point of having a fallback language?
APP_LOCALE
is used to set the default language and APP_FALLBACK_LOCALE
is used to set the fallback language. The point of having a fallback language is that you may not have translated every single string out of the language you developed the software in and in case of you not setting a translation up, you may prefer for it to for example display the English text if there isn’t a valid German translation for a string, so that there isn’t just nothing and a lot of users will still be able to tell what they are supposed to be told.
Which method of the App facade is used to determine if the current language is a certain specified language?
App::isLocale('en')
en is just an example here, just use the language code of your language
Eloquent will often automatically pluralize singular strings, but that always happens in English by default. How can you change this language?
(in boot method of AppServiceProvider)Pluralizer::useLanguage('spanish');
If you customize the pluralizer’s language, you should explicitly define your Eloquent model’s table names.
This is not something that you have to learn, but just for your info, the only languages currently supported are:
French, Norwegian-Bokmal, Portuguese, Spanish and Turkish.
All language files return an array of keyed strings. If you want to localize the welcome in the English version of a file, how can you do that?
<?php // lang/en/messages.php return [ 'welcome' => 'Welcome to our application!', ];
Now, if you just call the “welcome” key, it will use the defined welcome in the dedicated localization file of the language.
For applications with a large number of translatable strings, defining every string with a “short key” can become confusing when referencing the keys in your views and it is cumbersome to continually invent keys for every translation string supported by your application. How would a translation string in a JSON look and what is the advantage of using JSON?
(in this example, i am giving the spanish translation for an english site. this would be stored in lang/es.json)
{ "I love programming.": "Me encanta programar." }
Now, if the English site would have the sentence “I love programming.”, but the localization is set to spanish, it would use that text as long as the JSON it is in is called the correct thing for the corresponding language.
How can you check the translated versions of strings?
Using short keys:echo \_\_('example.welcome');
This would retrieve the welcome translation string from the lang/en/example.php file. If the translation does not exist, this would just return the key so in this case “example.welcome”
Using JSON:echo \_\_('I love programming.');
Again, if the translation string does not exist, the \_\_
function will return the translation string key that it was given.
instead of the echo, you can do either option like this using blade:{{ \_\_('messages.welcome') }}
How can you define placeholders in your translation strings?
All placeholders are prefixed with a :
For example, you may define a welcome message with a placeholder name:'welcome' => 'Welcome, :name',
How can you replace placeholders in your translation strings?
To replace the placeholders when retrieving a translation string, you may pass an array of replacements as the second argument to the \_\_
function:echo \_\_('messages.welcome', ['name' => 'dayle']);
If your placeholder contains all capital letters, or only has its first letter capitalized, the translated value will be capitalized accordingly:
'welcome' => 'Welcome, :NAME', // Welcome, DAYLE 'goodbye' => 'Goodbye, :Name', // Goodbye, Dayle
Pluralization between languages can be difficult due to the different rules of different languages. How can you, for your localized versions of your webpage, include different localizations?
Using a | character, you may distinguish singular and plural forms of a string.
For example:'apples' => 'There is one apple|There are many apples',
Of course, you can also use that when working with JSON localizations.
While making a localization it may be relevant to change the translation based on a given variable. Implement an example pluralization that uses different strings for different numbers of an example object.
'apples' => '{0} There are none|[1,19] There are some|[20,*] There are many',
You may also define placeholders while using multiple different tiers of pluralization
After defining a translation string that has pluralization options, how can you check which translation would be chosen for a specific value?
echo trans_choice('messages.apples', 10);
When a package, that may ship with its own language file, has translations, that you would like to change, you shouldn’t dig around in the packages files to find the localization translations but instead do what?
Overriding language files that a package ships with works by placing files in the lang/vendor/{package}/{locale} directory.
For example, if you need to override the English translation strings in messages.php for a package named skyrim/hearthfire, you should place a language file at:
lang/vendor/hearthfire/en/messages.php
Any translation string that you do not override will still be loaded from the package’s original language files
Which file extension do blade template files use?
.blade.php
How can you display data that is passed to your Blade views easily?
{{ $variable }}
You are not limited to displaying the contents of the variables passed to the view. You may also echo the results of any PHP function. In fact, you can put any PHP code you wish inside of a Blade echo statement:
The current UNIX timestamp is {{ time() }}.
Since many other JavaScript frameworks also use curly braces ( {} ) , how can you indicate to Blade that something is not meant to be interpreted by Blade?
By putting an @ symbol in front of it, you can tell blade that an expression is to remain untouched by the Blade engine. Blade will remove the @ symbol and leave it for the relevant framework to interpret.
Example:
Hello, @{{ name }}.
Using Blade, write a pseudo code snippet that only gets executed, if the application is running in a production environment!
@production // Insert code here, that is to only be run in production @endproduction
How can you create a switch statement in Blade?
@switch($i) @case(1) First case... @break @case(2) Second case... @break @default Default case... @endswitch
How can you include a Blade view from within another Blade view and what is the point of doing that?
You can include a different Blade view into your by using @include(‘view_name’)
If there is an element that is to be included into multiple subpages but is exactly or nearly the same on all of the sites, then just creating it once and then including it in the other Blade views reduces the amount of unnecessary code and makes changes apply to every subpage that uses the parent instead of needing to implement the changes in every single subpage
If there is something that you want to include into a loop of any kind, that is only supposed to be ran once even if the loop runs more often, how can you implement that?
@once // Include code to be ran a single time @endonce
What can you implement into your Blade template to run a snippet of raw PHP?
@php // insert PHP code @endphp
Import the Model “App\Models\Flight” using Blade and give it the alias “FlightModel”!
@use('App\Models\Flight', 'FlightModel')
If you want to import something without giving it an alias, you don’t actually have to provide the second argument of the @use statement, but it can be very helpful to make the rest of the code more easily readable.
I'm looping forever.
@endwhile ```No users
@endforelse ```This is {{ $user->name }}
@endforeach ```Examples
{{ $slot }} ``` Once the layout component has been defined, we may create a Blade view that utilizes the component. In this example we could do something like this: ```
This is appended to the master sidebar.
@endsection ```