Download the PHP package alex-no/field-lingo without Composer

On this page you can find all versions of the php package alex-no/field-lingo. 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 field-lingo

πŸ—‚οΈ Field-lingo

Packagist Version PHP Version Total Downloads

Field-lingo β€” lightweight library to easily work with database columns that store multiple language versions of the same attribute in one row (e.g. name_en, name_uk, name_ru). It provides a simple, consistent mechanism to reference "structured localized attribute names" (like @@name) and transparently map them to the actual column name_<lang> according to current language settings.

This repository contains full integrations for:


πŸ“‹ Table of Contents


🌍 Overview

Field-lingo provides three Yii2 adapters that transparently translate specially formatted field names into language-specific attributes. The pattern is simple: a prefix (by default @@) marks a structured field name. When Field-lingo encounters a name like @@name, it resolves the current language and converts that token to name_{lang} (for example name_en or name_uk).

Works in:

Primary goals:


πŸ“¦ Requirements

Framework-specific requirements:

Optional but recommended:


🧩 Key classes

These adapters rely on a shared trait LocalizedAttributeTrait which performs the core parsing and resolution logic.


βš™οΈ Quick Start

Installation

Choose your framework adapter:

Yii2

1. Install

Optional Recommendation

For automatic user language detection, it is recommended to install:

Note: This package requires its own separate configuration.

Basic idea

In DB table we keep language-specific columns:

In code we refer to @@name. FieldLingo maps @@name β†’ name_{lang} (e.g. name_uk) depending on Yii::$app->language.


Configure

Add to params (or any config area) the LingoActive section (example):

Notes:

  • Per-model overrides have higher priority than adapter-level defaults.
  • The trait reads Yii::$app->params['LingoActive'] by adapter name or model class name.

Configuration options

Main options:

These options may be set globally, per-class (LingoActiveRecord / LingoActiveQuery) or per-model.

Yii3

1. Install

2. Extend your models

3. Use localized attributes

4. Database Connection

The compatibility layer uses PDO for database access. You can configure it in two ways:

1. Via Dependency Injection (recommended):

2. Via Environment Variables:

5. Optional: Integrate with Translator service

6. Working with Relations

Compatibility Layer

The Yii3 adapter includes a compatibility layer (src/Adapters/Yii3/Compatibility/) that provides basic ActiveRecord and ActiveQuery functionality using PDO. This layer includes:

This compatibility layer is designed to work until Yii3 has an official stable ActiveRecord implementation.

Key Differences from Yii2:

See examples/Yii3/ for complete examples and detailed documentation.

Laravel

1. Extend your Eloquent models

2. Use localized attributes

See examples/Laravel/ for complete examples.

Symfony

1. Extend your Doctrine entities

2. Create repository

3. Use in controllers

See examples/Symfony/ for complete examples and configuration.


βš™οΈ Detailed Usage (Yii2)


🧠 LocalizedAttributeTrait β€” behavior summary

The LocalizedAttributeTrait does the heavy lifting:

You can call $this->convertLocalizedFields([ ... ]) to map arrays of fields at once.


πŸš€ Usage examples

ActiveRecord

When using LingoActiveRecord, you can reference localized attributes directly in code:

Notes for ActiveRecord:

  • Because hasAttribute() is available, missing localized columns are validated according to isStrict.
  • If you rely on toArray() or fields() to export language-aware data, ensure the adapter or model calls convertLocalizedFields() where appropriate.

ActiveQuery

LingoActiveQuery resolves names used in select(), andWhere(), orderBy(), groupBy() and similar places.

CRITICAL: Override the find() method To use LingoActiveQuery, you must override the find() method in your model:

Now you can use @@ fields in queries:

Notes for ActiveQuery:

  • Query layer cannot check hasAttribute() easily before SQL execution. The trait returns language-specific candidates and the DB will determine if the column exists.
  • Without overriding find(), your queries will use standard ActiveQuery and @@ fields will not be converted.

ActiveDataProvider

LingoActiveDataProvider is helpful when you expose sorting/filtering to external requests (like GridView) and need to map @@ tokens to real DB columns.

Basic usage:

Usage with GridView:

Advanced: Custom sort configuration

Notes for ActiveDataProvider:

  • LingoActiveDataProvider automatically converts @@ field names in sort attributes and filter conditions.
  • When defining custom sort attributes, use @@ notation consistently across query, sort config, and GridView columns.
  • The provider works seamlessly with Yii2's pagination and filtering mechanisms.

πŸ”„ Fallback Mechanism

Field-lingo includes a smart fallback system to handle missing localized columns gracefully. The behavior depends on the isStrict configuration option.

How Fallback Works

When you request a localized attribute (e.g., @@title with current language = uk):

  1. Library looks for title_uk

    • If exists β†’ returns title_uk βœ…
    • If not exists β†’ proceeds to step 2
  2. Check isStrict mode:

    • If isStrict = true β†’ throws MissingLocalizedAttributeException 🚫
    • If isStrict = false β†’ tries fallback language (step 3)
  3. Fallback to defaultLanguage:
    • Library looks for title_{defaultLanguage} (e.g., title_en if defaultLanguage = 'en')
    • If exists β†’ returns title_en βœ…
    • If not exists β†’ returns candidate name title_uk (DB will handle error if column truly missing)

Configuration Examples

Strict mode (recommended for development):

Non-strict mode with fallback (production-friendly):

Practical Example

Per-Model Fallback Configuration

You can override fallback behavior for specific models:

Recommendation:

  • Use isStrict = true during development to catch missing translations early
  • Use isStrict = false in production to gracefully handle missing translations with fallback

⚠️ Exception

MissingLocalizedAttributeException is thrown when isStrict is enabled and a localized attribute candidate does not exist (only thrown when attribute existence can be checked).

Make sure this exception is available in the adapter namespace or imported where the trait is used.


🧩 Advanced topics / hooks


πŸ”€ Migration Guide

Migrating an existing Yii2 project to Field-lingo is straightforward. Follow these steps:

Step 1: Install the package

Step 2: Prepare database schema

If you don't have localized columns yet, add them to your tables:

Step 3: Configure Field-lingo

Add configuration to config/params.php or config/web.php:

Step 4: Update your models

Before (standard ActiveRecord):

After (LingoActiveRecord):

Step 5: Update controllers and views

Before:

After:

Step 6: Update DataProviders

Before:

After:

Step 7: Update GridView columns

Before:

After:

Step 8: Test thoroughly

Migration Checklist

Gradual Migration Strategy

You can migrate gradually by:

  1. Keep both old and new columns during transition period
  2. Migrate model by model instead of all at once
  3. Use per-model configuration to customize behavior:

πŸ”§ Troubleshooting

Problem: @@field notation is not working in queries

Symptoms: Queries like Post::find()->where(['@@title' => 'Test']) fail or @@title is treated as literal string.

Solution:

  1. Make sure you've overridden the find() method in your model:

  2. Check that you're importing the correct class:

Problem: getAttribute('@@field') returns null or wrong value

Possible causes:

  1. Configuration not loaded

    • Check Yii::$app->params['LingoActive'] is properly configured
    • Verify config file is being loaded
  2. Column doesn't exist in database

    • If isStrict = true, you'll get MissingLocalizedAttributeException
    • If isStrict = false, library will try fallback language
    • Check database schema: SHOW COLUMNS FROM your_table
  3. Language format mismatch
    • Current language: Yii::$app->language (e.g., en-US, uk)
    • Library uses first part: en-US β†’ en
    • Make sure column names match: title_en, title_uk, etc.

Problem: GridView sorting not working with localized fields

Solution:

  1. Use LingoActiveDataProvider instead of ActiveDataProvider:

  2. Configure sort attributes with @@ notation:

Problem: How to check if Field-lingo is working correctly?

Quick test:

Problem: Exception "MissingLocalizedAttributeException"

Cause: isStrict = true and requested localized column doesn't exist in the table.

Solutions:

  1. Add missing column to database:

  2. Use fallback mode (non-strict):

  3. Add only columns you need:
    • If you only support English and Ukrainian, only create *_en and *_uk columns
    • Set defaultLanguage to one you always have

Problem: Getting "Unknown column" SQL error

Cause: Query uses @@field but it wasn't converted to actual column name.

Check:

  1. Model extends LingoActiveRecord
  2. Query uses LingoActiveQuery (via overridden find())
  3. DataProvider uses LingoActiveDataProvider
  4. Column actually exists in database

FAQ

Q: Can I use multiple prefixes like @@ and ##?

A: Yes! Configure as array:

Q: Can I change the language dynamically during runtime?

A: Yes, Field-lingo reads Yii::$app->language on each call:

Q: Does Field-lingo work with relations?

A: Yes, as long as related models also extend LingoActiveRecord:

Q: Can I use this in forms and validation?

A: Yes, but reference actual column names in rules:

In forms, you can use @@ notation for display:


🧱 Core design

Core/Localizer.php β€” centralized logic for mapping structured names to real column names.

Core/Contracts/LocalizerInterface.php β€” contract for Localizer implementations.

The core can be reused later for adapters (Laravel Eloquent, Doctrine, plain SQL builders).


πŸ“ Directory Structure

Examples

πŸ§ͺ Testing

Unit tests in tests/. PHPUnit recommended. Example:

🀝 Contribution

Contributions welcome! Suggested workflow:

  1. Fork repository.

  2. Create feature branch.

  3. Add tests.

  4. Open pull request.

Please follow PSR-12 and add PHPDoc (English) for public APIs.

πŸ—ΊοΈ Roadmap

πŸ“„ License

MIT. See LICENSE.

πŸ“¬ Contact

*Field-lingo Β© 2025 Oleksandr Nosov. Released under the MIT License.


All versions of field-lingo with dependencies

PHP Build Version
Package Version
Requires php Version >=8.2
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 alex-no/field-lingo contains the following files

Loading the files please wait ...