Download the PHP package wecansync/laravel-translations without Composer
On this page you can find all versions of the php package wecansync/laravel-translations. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download wecansync/laravel-translations
More information about wecansync/laravel-translations
Files in wecansync/laravel-translations
Package laravel-translations
Short Description Manage translations in a seperated table
License MIT
Homepage https://github.com/wecansync/laravel-translations
Informations about the package laravel-translations
Laravel Translation Management
Overview
Laravel Translation Management is a package designed to simplify the management of translations in Laravel applications. It allows you to store translations in a dedicated database table, making it easy to manage multiple languages and update translations as needed.
Features
- Database Storage: Store translations in a dedicated database table for easy management.
- Multi-Language Support: Easily handle translations for multiple languages.
- Configurable Keys: Customize foreign and owner keys as per your application's requirements.
- Simple Integration: Easy to integrate with your existing Laravel models.
Installation
You can install the package via Composer:
Publish config file
Usage
Step 1: Update Your Model
In your Category
model (or any model you wish to manage translations for), add the HasTranslations
trait and define the $translation_model
property
Step 2: Create Blade Form
Create a Blade form for adding category translations. The form should include fields for each language you want to support.
Here's an example for English and Arabic:
English language: Use language ID (example: 1).
translations[1][name]
: English translation of the category name (string)
Arabic language: Use language ID (example: 2).
translations[2][name]
: Arabic translation of the category name (string)
This ensures the translation fields are correctly linked to their respective language IDs.
Example Blade form:
Step 3: Retrieve Translation Data
You can easily retrieve translation data using the getTranslations()
function. This function allows you to fetch translations for a specific field in a specified language.
Parameters
-
languageId
(required):
The ID of the language for which you want to retrieve the translation. -
fieldName
(required):
The name of the field you want to retrieve the translation for. key
(optional):- This parameter allows you to fetch translation data using a specific key instead of the default language ID.
- Useful when you need to query translations based on a custom identifier.
Example Usage
Handling Missing Translations
If a translation does not exist for the specified language or field, the function will return null. You can handle this accordingly in your application:
This way, you can manage translations effectively and ensure that your application behaves gracefully when translations are missing.
Step 3: Delete Translation Data
You can easily delete translation data using the clearTranslations()
function. This function allows you to delete translations data for a specific model in a specified language.
Parameters
-
languageId
(required):
The ID of the language for which you want to delete the translation. key
(optional): This parameter allows you to delete translation data using a specific key instead of the default language ID.
Example Usage
Optional: Manage Translation Directly
You can manage translation data directly when storing multiple records of the main model at the same time using the withTranslations()
function. This function allows you to store or update translation data for a specific model in a specified language.
Configuration
To use withTranslations()
, ensure your model is configured with the following:
Parameters
translations
(required):
The translations data array to store or update
Example Usage
Storing Translations
Updating Translations
Security
If you discover any security-related issues, please report them by emailing [email protected].
Contributing
Contributions are welcome! To contribute, please fork the repository and submit a pull request. Make sure to follow the coding standards and include tests for any new features.
License
The package is open-sourced software licensed under the MIT license.