{"id":33955567,"url":"https://github.com/esign/laravel-translation-loader","last_synced_at":"2026-04-05T17:40:10.142Z","repository":{"id":188905112,"uuid":"430708603","full_name":"esign/laravel-translation-loader","owner":"esign","description":"Load translations from the database or other sources.","archived":false,"fork":false,"pushed_at":"2026-03-27T07:01:50.000Z","size":62,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2026-03-27T19:04:37.254Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/esign.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2021-11-22T13:02:34.000Z","updated_at":"2026-03-27T07:00:45.000Z","dependencies_parsed_at":"2026-03-27T09:03:06.767Z","dependency_job_id":null,"html_url":"https://github.com/esign/laravel-translation-loader","commit_stats":null,"previous_names":["esign/laravel-translation-loader"],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/esign/laravel-translation-loader","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esign%2Flaravel-translation-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esign%2Flaravel-translation-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esign%2Flaravel-translation-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esign%2Flaravel-translation-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/esign","download_url":"https://codeload.github.com/esign/laravel-translation-loader/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/esign%2Flaravel-translation-loader/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31444702,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-05T15:22:31.103Z","status":"ssl_error","status_checked_at":"2026-04-05T15:22:00.205Z","response_time":75,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-12-12T20:03:50.473Z","updated_at":"2026-04-05T17:40:10.137Z","avatar_url":"https://github.com/esign.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Load translations from the database or other sources\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/esign/laravel-translation-loader.svg?style=flat-square)](https://packagist.org/packages/esign/laravel-translation-loader)\n[![Total Downloads](https://img.shields.io/packagist/dt/esign/laravel-translation-loader.svg?style=flat-square)](https://packagist.org/packages/esign/laravel-translation-loader)\n![GitHub Actions](https://github.com/esign/laravel-translation-loader/actions/workflows/main.yml/badge.svg)\n\nThis package extends Laravel's default translation functionality, allowing you to load translations from different sources.\nIt ships with a database loader that comes with automatic creation of missing keys and built-in caching support.\n\n## Installation\nYou can install the package via composer:\n\n```bash\ncomposer require esign/laravel-translation-loader\n```\n\nThe package will automatically register a service provider.\n\nThis package comes with a migration to store translations in the database. You can publish the migration file with the following command:\n```bash\nphp artisan vendor:publish --provider=\"Esign\\TranslationLoader\\TranslationLoaderServiceProvider\" --tag=\"migrations\"\n```\n\nThis will publish the following migration:\n```php\nreturn new class extends Migration\n{\n    public function up(): void\n    {\n        Schema::create('translations', function (Blueprint $table) {\n            $table-\u003eid();\n            $table-\u003estring('key');\n            $table-\u003estring('group')-\u003edefault('*');\n            $table-\u003etext('value_en')-\u003enullable();\n            $table-\u003etimestamps();\n\n            $table-\u003eunique(['key', 'group']);\n        });\n    }\n};\n```\n\nOut of the box, it ships with support for the English language and uses our [Underscore Translatable](https://github.com/esign/laravel-underscore-translatable) package to store these languages in different columns. You may add more languages as you wish.\n\nNext up, you can optionally publish the configuration file:\n```bash\nphp artisan vendor:publish --provider=\"Esign\\TranslationLoader\\TranslationLoaderServiceProvider\" --tag=\"config\"\n```\n\nThe config file will be published as config/translation-loader.php with the following contents:\n```php\nreturn [\n    /**\n     * These loaders will load translations from different sources.\n     * You can use any class that implements the TranslationLoaderContract.\n     */\n    'loaders' =\u003e [\n        \\Esign\\TranslationLoader\\Loaders\\DatabaseLoader::class,\n    ],\n\n    /**\n     * This is the loader that combines all of the other loaders together.\n     * This class overrides Laravel's default `translation.loader`.\n     */\n    'aggregate_loader' =\u003e \\Esign\\TranslationLoader\\Loaders\\AggregateLoader::class,\n\n    /**\n     * This is the model that will be used by the DatabaseLoader.\n     * You may provide a class that implements the UnderscoreTranslatable trait.\n     */\n    'model' =\u003e \\Esign\\TranslationLoader\\Models\\Translation::class,\n\n    'cache' =\u003e [\n        /**\n         * The key that will be used to cache the database translations.\n         */\n        'key' =\u003e 'esign.laravel-translation-loader.translations',\n\n        /**\n         * The duration for which database translations will be cached.\n         */\n        'ttl' =\u003e \\DateInterval::createFromDateString('24 hours'),\n\n        /**\n         * The cache store to be used for database translations.\n         * Use null to utilize the default cache store from the cache.php config file.\n         * To disable caching, you can use the 'array' store.\n         */\n        'store' =\u003e null,\n    ],\n\n    /**\n     * Configuration for the custom translator class that handles missing translation keys.\n     * This class overrides Laravel's default `translator` binding.\n     */\n    'translator' =\u003e \\Esign\\TranslationLoader\\Translator::class,\n];\n```\n\n## Usage\nTo create database translations you may use the `create` method on the `Translation` model:\n```php\nuse Esign\\TranslationLoader\\Models\\Translation;\n\nTranslation::create([\n    'group' =\u003e 'messages',\n    'key' =\u003e 'welcome',\n    'value_en' =\u003e 'Hello World!',\n    'value_nl' =\u003e 'Hallo Wereld!',\n]);\n```\n\nFor a more automated approach, consider [automatic creation of database translations](#automatically-creating-missing-translation-keys), eliminating the need for manual key creation.\n\nOnce created, you can retrieve the translations as usual in Laravel:\n```php\ntrans('messages.welcome'); // Hello World!\ntrans('messages.welcome', [], 'nl'); // Hallo Wereld!\n```\n\nFor all possibilities, please refer to the [Localization](https://laravel.com/docs/localization) docs from Laravel.\nBe aware that database-defined translations can overwrite file translations that may exist.\n### Handling missing translation keys\nIn situations where you request a translation key that doesn't exist, you have the option to provide a callback to the translator.\nThis callback will be triggered when the requested translation key is not found.\nPlease note that this callback will not be invoked if the translation key exists but has an empty or null value.\n\nYou can also customize the behavior of the translator by returning a specific value from the callback.\nThis returned value will then be used as the translation for the missing key.\n\nYou may provide this callback using the `setMissingKeyCallback` method on the `Esign\\TranslationLoader\\Facades\\Translator` facade:\n\n```php\nuse Esign\\TranslationLoader\\Facades\\Translator;\n\nTranslator::setMissingKeyCallback(function (string $key, string $locale) {\n    // Implement your custom logic here\n\n    return \"Fallback translation for '$key'\";\n});\n```\n\nIn the provided closure, you can implement any custom logic you need to handle the missing translation keys.\nThis might involve logging, sending notifications, or providing a default translation value based on your application's requirements.\n\n### Automatically creating missing translation keys\nThis package ships with the ability to automatically create database translations in case the key does not yet exist.\nYou may activate this functionality by calling the `createMissingTranslations` on the `Esign\\TranslationLoader\\Facades\\Translator` facade.\nThis is typically done from a service provider within your application:\n```php\nuse Esign\\TranslationLoader\\Facades\\Translator;\n\nTranslator::createMissingTranslations();\n```\n\nNote that this functionality will create translations under the `*` group.\nIn case you need to change this behaviour you may do so by [defining your own `setMissingKeyCallback`](#handling-missing-translation-keys).\n\n### Registering a loader\nIf you need to gather translations from diverse sources, you can achieve this by creating a custom translation loader that implements the `Esign\\TranslationLoader\\Contracts\\TranslationLoaderContract` interface:\n\n```php\nuse Esign\\TranslationLoader\\Contracts\\TranslationLoaderContract;\n\nclass MyTranslationsLoader implements TranslationLoaderContract\n{\n    public function loadTranslations(string $locale, string $group, ?string $namespace = null): array\n    {\n        // Your implementation here\n    }\n}\n```\n\nIntegrate your custom loader by including it in the `loaders` array within the configuration file of this package.\n\n### Caching database translations\nBy default, this package ensures efficient performance by caching your database translations for 24 hours.\nThis caching mechanism uses the default cache driver that you have configured within your Laravel application.\n\nIf you wish to modify the cache duration or switch to a different cache store, please refer to the cache settings within the [configuration file](/config/translation-loader.php).\n\n### Clearing the translations cache\nThe translations cache is automatically maintained when you interact with the `Esign\\TranslationLoader\\Models\\Translation` model.\nHowever, if you make changes outside of these operations, you need to manually clear the cache:\n```bash\nphp artisan translations:clear-cache\n```\n\n### Importing file translations to the database\nThis package ships with an Artisan command that allows you to import file translations into the database.\nThis can be useful when you want to migrate your translations from file-based to database-based storage.\nYou should specify the locales you want to import translations for as a comma-separated list:\n```bash\nphp artisan translations:import-files-to-database --locales=en,nl\n```\n\nYou can optionally specify the `--overwrite` flag to overwrite any existing translations.\n```bash\nphp artisan translations:import-files-to-database --locales=en,nl --overwrite\n```\n\n### FAQ\n\u003cdetails\u003e\n\u003csummary\u003eInstallation conflict with [mcamara/laravel-localization](https://github.com/mcamara/laravel-localization)\u003c/summary\u003e\n\nThe laravel-localization package offers route translation functionality by leveraging Laravel's translator.\nHowever, conflicts may arise due to our package's override of Laravel's translator behavior.\nThis can lead to potential database exceptions when querying translations, hindering the installation of our package.\n\nTo tackle this problem, you can utilize contextual binding within a service provider of your application.\nThis instructs Laravel to employ file-based translation solely when registering translated routes.\n\nInclude the following code in a serviceprovider within your application:\n```php\nuse Illuminate\\Contracts\\Translation\\Translator as TranslatorContract;\nuse Illuminate\\Foundation\\Application;\nuse Illuminate\\Translation\\FileLoader;\nuse Illuminate\\Translation\\Translator;\nuse Mcamara\\LaravelLocalization\\LaravelLocalization;\n\npublic function register(): void\n{\n    $this\n        -\u003eapp\n        -\u003ewhen(LaravelLocalization::class)\n        -\u003eneeds(TranslatorContract::class)\n        -\u003egive(function (Application $app) {\n            $loader = new FileLoader($app['files'], [__DIR__.'/lang', $app['path.lang']]);\n\n            return new Translator($loader, $app-\u003egetLocale());\n        });\n}\n```\n\nNote that this solution only works for any versions above `^2.0` of mcamara/laravel-localization.\n\u003c/details\u003e\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fesign%2Flaravel-translation-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fesign%2Flaravel-translation-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fesign%2Flaravel-translation-loader/lists"}