Download the PHP package upon/mlang without Composer

On this page you can find all versions of the php package upon/mlang. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package mlang

MLang - The Performant Way to Handle Multilingual Laravel Models

Stop using JSON columns. Start using proper database structure for translations.

Total Downloads Latest Stable Version License PHP Version GitHub Stars

Quick Start • Why MLang? • Features • Documentation • Contributing


Why Another Translation Package?

Most Laravel translation packages store translations in JSON columns. This works, but has serious limitations:

MLang takes a different approach: Each translation is a proper database row with row_id (linking translations together) and iso (language code). This means:

Quick Start

Why MLang?

Feature MLang JSON-based packages
Database queries on translations Native SQL queries Requires JSON functions
Index support Full index support Limited/None
Full-text search Native support Complex workarounds
Bulk operations Built-in methods Manual implementation
Translation statistics getStats(), getCoverage() Not available
Multi-language insert Single method call Multiple inserts
Rate limiting Built-in protection Not available
Security helpers Input validation & sanitization Varies

When to Use MLang

Choose MLang if you need:

Stick with JSON-based packages if:

Documentation

Features

Core Features

Feature Description
Row-based translations Each translation is a database row - queryable, indexable, searchable
Auto language scoping Queries automatically filter by current locale
Route model binding Works seamlessly with Laravel's route model binding
Fallback support Automatic fallback to default language when translation missing
Browser detection Middleware to detect user's preferred language

Developer Experience

Feature Description
One-line multi-language insert createMultiLanguage() creates all translations at once
Bulk operations Update/delete all translations for a record in one call
Translation statistics getStats() and getCoverage() for analytics
Artisan commands mlang:migrate, mlang:generate for easy management
Facade API Fluent interface: MLang::forModel(Product::class)->...

Security & Performance

Feature Description
Input validation Built-in validation for locales, table names, model classes
SQL injection prevention All queries use Laravel Query Builder
Rate limiting Built-in protection for bulk operations
Safe with factories Works before migrations run

Installation

Requirements

PHP Laravel
>= 8.1 10.x, 11.x, 12.x

Step 1: Install

Step 2: Configure Models

Step 3: Add Trait to Models

Alternative: You can also extend Upon\Mlang\Models\MlangModel if you prefer inheritance over traits.

Step 4: Run Migration

This adds two columns to your tables:

Artisan Commands

The package provides several Artisan commands for managing translations:

Command Arguments Options Description
mlang:migrate - --table=TABLE_NAME
--rollback
Add MLang columns to tables
Use --rollback to remove columns
mlang:generate {model?}
{locale?}
- Generate translations for models
Optionally specify model name and locale

Command Examples

Using the Facade

MLang comes with a powerful facade that provides a fluent interface for interacting with the package.

Core Facade Methods

Method Parameters Returns Description
forModel() object\|string $model Mlang Set the model to work with (chainable)
getModelName() - string Get the current model name
getTableName() - string\|null Get the table name for current model
getTableNames() - array Get all table names from configured models
getModels() - array Get all configured model class names
getCurrentModel() - string\|null Get current model class name
getModelInstance() - object\|null Get instance of current model

Migration & Generation Methods

Method Parameters Returns Description
migrate() ?string $table = null Mlang Add MLang columns to table (chainable)
rollback() ?string $table = null Mlang Remove MLang columns from table (chainable)
generate() ?string $model = null, ?string $locale = null Mlang Generate translations for model (chainable)

🆕 Multi-Language Operations

Method Parameters Returns Description
createMultiLanguage() array $attributes, ?array $languages = null, ?array $translatedAttributes = null array Create record in multiple languages at once
getAllTranslations() int\|string $id Collection Get all language versions of a record by ID
updateAllTranslations() int\|string $id, array $attributes int Update all translations by ID (returns count)
deleteAllTranslations() int\|string $id int Delete all translations by ID (returns count)
copyToLanguage() Model\|int\|string $sourceModelOrId, string $targetLanguage, array $overrideAttributes = [] Model\|null Copy record to another language (accepts ID or model)

🆕 Translation Statistics & Analysis

Method Parameters Returns Description
getStats() - array Get translation statistics (total, unique, per language)
getCoverage() - float Get translation coverage percentage
getIncompleteTranslations() - Collection Get records with missing translations

Basic Usage Examples

🆕 Multi-Language Insert (NEW!)

Create a record in multiple languages simultaneously:

🆕 Translation Statistics (NEW!)

Get insights into your translations:

🆕 Bulk Translation Operations (NEW!)

Manage all translations for a record using its regular ID - no need to know about row_id!

Configuration Options

The package offers various configuration options to fine-tune its behavior:

Working with Factories

The package is designed to work safely with factories even before migrations have run. It intelligently detects when MLang columns don't exist and avoids trying to use them in such cases.

To ensure smooth operation with factories:

  1. Set 'auto_generate' => false in your config file when using factories before migrations.
  2. The trait will automatically detect missing columns and adjust behavior accordingly.
  3. After running migrations, you can enable auto-generation features.

Trait-based approach benefits: The trait-based approach makes it easy to work with factories before migrations, with enhanced column existence detection and graceful fallbacks.

Managing Translations

Using Artisan Commands

Generate translations for all models:

Generate translations for a specific model:

Generate for a specific language:

Remove a language from a table:

Using the Facade

You can also manage translations programmatically using the facade:

Language Detection

To automatically detect the user's browser language:

  1. Add the locale middleware to your app/Http/Kernel.php file:

To manually set the language:

Query Usage

Both package versions provide the same query methods for working with multilingual content.

Model Query Scopes

Scope Method Parameters Description Example
trFind() int\|string $id, ?string $iso = null Find record by row_id in current (or specified) language Category::trFind(1)
trWhere() array\|string\|Closure $conditions Query with auto language filter and id→row_id mapping Category::trWhere('status', 'active')

Finding Records

Querying with Conditions

Route Model Binding

The package enhances Laravel's route model binding to automatically fetch the correct language version:

How it works:

Understanding Interface and Trait Relationship

When implementing MLang in your models, it's important to understand the relationship between the interface and trait:

  1. The Interface (MlangContractInterface) defines the contract that your models must fulfill to be MLang-compatible.

  2. The Trait (MlangTrait) provides the actual implementation of the methods required by the interface.

You must use both together:

This provides several benefits:

🆕 Helper Classes

The package includes organized helper classes for better code organization and reusability.

SecurityHelper Methods

Method Parameters Returns Description
validateLocale() string $locale bool Validate locale format (throws exception if invalid)
validateTableName() string $table bool Validate table name (throws exception if invalid)
validateColumnName() string $column bool Validate column name (throws exception if invalid)
validateModelClass() string $model bool Validate model class exists (throws exception if invalid)
validateLocales() array $locales bool Validate multiple locales at once
tableExists() string $table bool Check if table exists in database
columnExists() string $table, string $column bool Check if column exists in table
sanitizeValue() mixed $value mixed Remove null bytes and control characters
sanitizeAttributes() array $attributes array Sanitize all values in array
isValidRowId() mixed $rowId bool Check if value is valid row_id (positive integer)
checkRateLimit() string $key, int $maxAttempts = 60, int $decayMinutes = 1 bool Rate limiting check for bulk operations

Example Usage:


LanguageHelper Methods

Method Parameters Returns Description
getConfiguredLanguages() - array Get all configured languages from config
getFallbackLanguage() - string Get fallback language from config
getCurrentLocale() - string Get current application locale
isLanguageConfigured() string $locale bool Check if language is in configuration
parseAcceptLanguageHeader() ?string $header string Parse Accept-Language header to get best match
validateAndGetLocale() ?string $locale = null string Validate locale or return fallback
getMissingLanguages() array $existingLanguages array Get languages not in existing array
getLanguageName() string $locale string Get human-readable language name
sortLanguagesByPriority() array $languages array Sort languages (current first, then config order)
isAutoGenerateEnabled() - bool Check if auto-generation is enabled
shouldObserveDuringConsole() - bool Check if observer runs during console
getConfiguredModels() - array Get all configured model classes

Example Usage:


TranslationHelper Methods

Method Parameters Returns Description
getExistingTranslations() Model $model, int\|string $rowId array Get array of existing locale codes for row_id
createMultiLanguageRecord() Model $model, array $attributes, array $languages, ?array $translatedAttributes = null array Create record in multiple languages
generateRowId() Model $model int Generate new unique row_id
handleUniqueConstraints() Model $model, array $attributes, string $language array Handle unique constraints by appending suffixes
getUniqueIndexes() string $table array Get unique indexes for table (DB-agnostic)
copyToLanguage() Model $sourceModel, string $targetLanguage, array $overrideAttributes = [] Model\|null Copy record to another language
deleteAllTranslations() Model $model, int\|string $rowId int Delete all translations for row_id
updateAllTranslations() Model $model, int\|string $rowId, array $attributes int Update all translations for row_id
getTranslationStats() Model $model array Get statistics (total, unique, per language)

Example Usage:


QueryHelper Methods

Method Parameters Returns Description
applyLanguageFilter() Builder $query, ?string $locale = null Builder Add language filter to query
applyRowIdFilter() Builder $query, int\|string $rowId Builder Add row_id filter to query
getAllTranslations() Model $model, int\|string $rowId Collection Get all language versions of record
findByRowIdAndLocale() Model $model, int\|string $rowId, ?string $locale = null Model\|null Find specific translation
getRecordsWithIncompleteTranslations() Model $model Collection Get records missing some translations
scopeCurrentLanguage() Builder $query Builder Scope to current language only
scopeWithCompleteTranslations() Builder $query Builder Scope to records with all translations
buildMlangQuery() Model $model, array $conditions = [], ?string $locale = null Builder Build query with language awareness
getTranslationCoverage() Model $model float Get coverage percentage (0-100)

Example Usage:

Security Best Practices

This package includes several security features:

  1. Input Validation: All user inputs (model names, table names, locales) are validated before use
  2. SQL Injection Prevention: Uses Laravel's Query Builder exclusively, no raw SQL with user input
  3. Sanitization: Automatic sanitization of string values to remove null bytes and control characters
  4. Rate Limiting: Built-in rate limiting for bulk operations to prevent abuse
  5. Type Safety: Strong typing throughout the codebase with PHP 8.3+ features

Security Guidelines

Contributing

Contributions are welcome! Here's how you can help:

  1. Star the repo - It helps others discover MLang
  2. Report bugs - Open an issue with reproduction steps
  3. Suggest features - We'd love to hear your ideas
  4. Submit PRs - Bug fixes and improvements welcome

Support the Project

If MLang helps you build multilingual Laravel apps, please consider:

License

MIT License - see LICENSE for details.


Built with care by Charisma Design

Report BugRequest Feature • Contact


All versions of mlang with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
ext-filter Version *
illuminate/database Version ^v11.44.7|12.*
illuminate/support Version ^v11.44.7|12.*
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package upon/mlang contains the following files

Loading the files please wait ...