dialect / transedit
Translations for Laravel with inline edit support
Installs: 3 448
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 3
Forks: 0
Open Issues: 0
Requires (Dev)
- orchestra/testbench: ~3.0
- dev-master
- v1.1.0
- 1.0.0
- 0.9.0
- 0.8.0
- 0.7.1
- 0.7.0
- 0.6.1
- 0.6.0
- 0.5.2
- 0.5.1
- 0.5.0
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4
- 0.3.5
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.2
- 0.2.1
- 0.2.0
- 0.1.0
- dev-analysis-PG1vZP
- dev-analysis-prwW4a
- dev-create-migrations
- dev-analysis-RPQjpn
- dev-analysis-W6apae
- dev-analysis-EP3o3v
- dev-analysis-W6dRmE
- dev-analysis-BMomgg
- dev-laraveltranslations
- dev-analysis-86WExl
- dev-analysis-zR596K
- dev-remove-larvel-model-chaching
- dev-analysis-zewaYK
- dev-cache
- dev-laravel57
- dev-analysis-876Px3
This package is auto-updated.
Last update: 2025-01-08 15:24:46 UTC
README
TransEdit is a Laravel plugin designed to store and manage localization strings in a database with built-in caching. It offers a flexible and interactive way to handle language translations, including an optional edit mode, which allows users to edit translations directly in the browser by simply double-clicking any highlighted text.
Table of Contents
Features
- Database-driven localizations – Store translations in the database instead of the traditional file-based approach.
- Built-in cache support – Improve performance by caching translations.
- Browser-based editing – Enable an edit mode that allows users to directly modify translations by double-clicking them.
- Laravel integration – Seamless integration with Laravel’s localization features.
- Vue and Inertia integration – Easily incorporate TransEdit with Vue or Inertia-based applications.
Installation
-
Require the package via Composer:
composer require dialect/transedit
-
Publish the configuration, assets, and migrations:
php artisan vendor:publish --provider="Dialect\TransEdit\TransEditServiceProvider"
This command will publish:
- Config file to
config/transedit.php
- Assets (JavaScript and Vue components) to your
resources
folder - Migrations to
database/migrations
- Config file to
-
Add the Vue component
If you’re using Laravel Mix, add the TransEdit Vue component to your main JavaScript file (commonlyresources/assets/js/app.js
orresources/js/app.js
) and recompile:Vue.component('transedit', require('./components/transedit/TransEdit.vue'));
Then, run:
npm run dev
or
npm run prod
according to your build process.
-
Run the database migrations:
php artisan migrate
This will create the necessary tables for storing translations.
-
Enable TransEdit in your front-end
-
Include the TransEdit script in your layout (e.g.,
resources/views/layout.blade.php
):<script src="/js/transedit.js"></script>
-
If you are using Vue, register TransEdit as a global property in your
app.js
(or equivalent):Vue.prototype.transEdit = window.transEdit;
-
If you are using Inertia, attach
transEdit
in your Inertia app setup, like so:import { createInertiaApp } from '@inertiajs/inertia-vue3'; import { createApp, h } from 'vue'; createInertiaApp({ title: (title) => `${title} - ${appName}`, resolve: (name) => resolvePageComponent( `./Pages/${name}.vue`, import.meta.glob('./Pages/**/*.vue'), ), setup({ el, App, props, plugin }) { const theApp = createApp({ render: () => h(App, props) }); // Make transEdit available globally theApp.config.globalProperties.transEdit = window.transEdit; return theApp .use(plugin) .mount(el); }, progress: { color: '#4B5563', }, });
-
That’s it! TransEdit is now installed and ready for use in your Laravel project.
Configuration
After running the publish command, a configuration file will be placed at config/transedit.php
. This file allows you to customize how TransEdit handles caching, database settings, default locale behavior, and more. Adjust these settings as desired to suit your application’s needs.
Usage
Using Keys
TransEdit offers convenient helper methods to set and retrieve translations using keys.
-
Setting a translation key and value
transEdit()->setKey('greeting', 'Hello');
-
Retrieving a translation by key
$value = transEdit()->getKey('greeting'); // returns "Hello"
-
Helper function shorthand
// Set a key transEdit('example.key', 'Some value', 'locale'); // 'locale' is optional // Get a key transEdit('example.key'); // returns "Some value"
-
Working with variables
// Positional variables transEdit('You have $1 months left on your subscription of $2.', ['12', 'Netflix']); // Named variables transEdit('You have $MONTHS months left on your subscription of $SERVICE.', [ 'MONTHS' => '12', 'SERVICE' => 'Netflix' ]);
-
Enable/Disable edit mode
transEdit()->enableEditMode(); // Double-click on highlighted text to edit transEdit()->disableEditMode(); // Turn off in-browser editing
Note: Locale settings and edit-mode are session-based, meaning they apply to individual user sessions.
Using Phrases
TransEdit also supports using full phrases or sentences directly in place of a key.
-
Setting a phrase
// If the phrase does not exist in the database, TransEdit will create it transEdit('Welcome to our website!'); // Optionally, you can include a locale transEdit('Welcome to our website!', null, 'en');
-
Retrieving a phrase
echo transEdit('Welcome to our website!');
-
Using variables in a phrase
transEdit('Hello, $NAME! Thank you for joining us.', ['NAME' => 'Alice']);
-
Editing phrases in-browser
transEdit()->enableEditMode();
Anywhere you have
transEdit('A new phrase here')
in your Blade view, you can double-click and edit it directly in the browser (if your Vue/JS setup is correct). -
Why use phrases?
- Speed & Convenience: No need to manage separate key strings or maintain complex nested arrays.
- Automatic Handling: TransEdit manages insertion, updates, and caching for these phrases.
- Iterative Development: Wrap new text in
transEdit('...')
. Later, if you want more structure, you can easily rename or reorganize these translations.
Artisan Commands
1. Add Existing Translations
This command scans your lang
folder for Laravel language files and imports them into the TransEdit database:
php artisan transedit:add-lang-files-to-database
For example, if you have a file lang/sv/article.php
containing:
<?php return [ "recipe" => "Recept", ];
TransEdit will import the key article.recipe
into the database. You can then retrieve it with:
transEdit('article.recipe');
just like you would with @lang('article.recipe')
.
2. Add Missing Translations (with Migration Option)
The Add Missing Phrases command scans your resources/
folder for any calls to transEdit('...')
that do not already exist in the database. It then either inserts them into your database directly or creates a migration so you can manage and track these new phrases in version control.
php artisan transedit:add-missing-phrases
-
Default behavior: Creates a new migration file in
database/migrations/
.- After running the command, you’ll see a message indicating a migration has been created.
- You can then run
php artisan migrate
to insert the missing phrases into your database. - This is ideal if you want to keep version control records of when new phrases are added.
-
Pushing directly to the database:
php artisan transedit:add-missing-phrases --direct=1
This immediately inserts any missing phrases for all locales into the database without creating a migration file.
Below is a summary of the two approaches:
Why choose a migration approach?
- Ensures that adding translations is tracked in source control.
- Allows rollback with
php artisan migrate:rollback
if needed. - Useful in team environments where changes to translations should be approved or reviewed.
Why choose the direct approach?
- Faster and easier for quick adjustments.
- Ideal for smaller projects or development environments where versioning of translations is not critical.
Examples
Below is a short snippet demonstrating how to use TransEdit in your application:
// Create or update a language name transEdit()->setLocaleLanguageName('en', 'English'); // ----- Using Keys ----- // // Set a key for the default or current locale transEdit()->setKey('header.welcome', 'Welcome to our website'); transEdit('header.welcome', 'Welcome to our website'); // short syntax // Retrieve a key echo transEdit('header.welcome'); // "Welcome to our website" // ----- Using Phrases ----- // // Directly create or retrieve a phrase echo transEdit('This is a phrase-based translation.'); // With variables echo transEdit('Thank you, $NAME, for signing up!', ['NAME' => 'Alice']); // Enable edit-mode (for the current user/session) transEdit()->enableEditMode();
Publishing and Assets
-
Assets
Published toresources/assets
(or the equivalent path for your setup).
They include Vue components and the JavaScript filetransedit.js
. -
Config
Published toconfig/transedit.php
. -
Migrations
Published todatabase/migrations
.
Make sure to run:
php artisan vendor:publish --provider="Dialect\TransEdit\TransEditServiceProvider"
to get all these files where they need to be.
That’s everything you need to start translating and editing your Laravel app with TransEdit!
Feel free to open an issue or submit a pull request on GitHub if you have any questions or improvements. Enjoy!