# Localization
Localization is how Krayin renders the same UI in any supported language. Each package ships its own translation files under Resources/lang/<locale>/app.php and registers a translation namespace so callers can reference strings with package::path.to.key.
For deeper Laravel-specific guidance see the Laravel localization docs (opens new window).
# โ๏ธ Configure the default and fallback locale
The application-wide default lives in config/app.php:
'locale' => env('APP_LOCALE', 'en'),
'fallback_locale' => env('APP_FALLBACK_LOCALE', 'en'),
| Setting | Why |
|---|---|
locale | Which language Laravel renders if the user has no other preference. |
fallback_locale | Used when a string is missing from the requested locale โ keep this set to en so untranslated keys still render something readable. |
Set both via your .env:
APP_LOCALE=en
APP_FALLBACK_LOCALE=en
# ๐ Create the language files
By convention each package keeps translations under src/Resources/lang/<locale>/app.php. Add a folder per language โ we'll start with English:
packages
โโโ Webkul
โโโ Example
โโโ src
โโโ ...
โโโ Resources
โโโ lang
โโโ en
โโโ app.php
Inside app.php, return an associative array of keys. Nest the array to match your view's dot-notation:
<?php
return [
'examples' => [
'index' => [
'title' => 'Examples',
],
'create-btn' => 'Create Example',
'create-success' => 'Example created successfully.',
'update-success' => 'Example updated successfully.',
'delete-success' => 'Example deleted successfully.',
],
'acl' => [
'examples' => 'Examples',
'create' => 'Create',
'delete' => 'Delete',
],
];
To add another language, copy en/ to fr/, de/, hi/, etc. and translate the values โ keep the keys identical.
# ๐ Register translations with the service provider
The translation namespace is what lets you write @lang('example::examples.index.title'). Register it in boot():
<?php
namespace Webkul\Example\Providers;
use Illuminate\Support\ServiceProvider;
class ExampleServiceProvider extends ServiceProvider
{
public function boot(): void
{
$this->loadTranslationsFrom(__DIR__ . '/../Resources/lang', 'example');
}
}
loadTranslationsFrom() takes the absolute path to the lang/ folder and a short namespace ('example'). After this, callers anywhere in the app can use the example:: prefix.
# ๐ Use translations
# In Blade templates
@lang('example::app.examples.index.title')
# In PHP code
trans('example::app.examples.create-success');
__('example::app.examples.create-success'); // alias for trans()
# With placeholders
Add named placeholders in your language file with : prefix:
'welcome' => 'Welcome back, :name!',
Then pass the replacement when calling the helper:
{{ __('example::app.welcome', ['name' => $user->name]) }}
# ๐งช Verify
Pick a string you just translated and:
- Visit a page that renders it โ confirm the value comes from your
app.php, not the raw key (example::app.examples.index.title). - Change
APP_LOCALEin.envto a locale you've translated for, runphp artisan config:clear, reload, and confirm the page switches language.
If you see the raw key on the page, loadTranslationsFrom() didn't run โ check it's inside boot() and re-run php artisan optimize:clear.