Introduction

GP247 uses the gp247_language_render() function to render languages. The syntax is similar to Laravel's default trans() helper, but with additional language management features through the database.

1. Language Processing Logic

How gp247_language_render() Works

When you call the function gp247_language_render('abc.xyz'), the system processes in the following order:

Step 1: Search in Database

  • Query the gp247_languages table
  • Find the value in the text column with conditions:
    • Column code = 'abc.xyz'
    • Column location = current language (e.g., envija...)

Step 2: Fallback to Laravel Translation

  • If not found in the database, the system will use Laravel's trans() helper
  • Search in the lang/{locale}/ directory
  • In the example above: find the key xyz in the file lang/{locale}/abc.php

Illustration Example

// Call render function
echo gp247_language_render('product.add_to_cart');

// Step 1: Search in database
// SELECT text FROM gp247_languages 
// WHERE code = 'product.add_to_cart' 
// AND location = 'vi'

// If found: return value from database (e.g., "Add to Cart")
// If not found: move to Step 2

// Step 2: Use trans()
// Search in file lang/en/product.php
// return [
//     'add_to_cart' => 'Add to Cart',
// ];

Priority Order

Database (gp247_languages) > Laravel Translation Files (lang/)

2. Updating Languages in GP247

You have 2 ways to load languages into the GP247 system:


Method 1: Add Data to Database

A. Update for Community (GitHub Repositories)

If you want to contribute to the GP247 community, update at the following repositories:

1. GP247 Core

File: DataLocaleSeeder.php
Link: https://github.com/gp247net/core/blob/master/src/DB/seeders/DataLocaleSeeder.php#L275

// Example of adding new language
[
    'code' => 'product.name',
    'text' => 'Product Name',
    'location' => 'en',
    'sort' => 0,
],
[
    'code' => 'product.name',
    'text' => 'Tên sản phẩm',
    'location' => 'vi',
    'sort' => 0,
],

2. GP247 Front

File: DataFrontDefaultSeeder.php
Link: https://github.com/gp247net/front/blob/master/src/DB/seeders/DataFrontDefaultSeeder.php#L85

3. GP247 Shop

File: DataShopInitializeSeeder.php
Link: https://github.com/gp247net/shop/blob/master/src/DB/seeders/DataShopInitializeSeeder.php#L200

Adding New Language (Example: Japanese)

Step 1: Add language type to gp247_admin_language table

File: DataDefaultSeeder.php
Link: https://github.com/gp247net/core/blob/master/src/DB/seeders/DataDefaultSeeder.php#L219

[
    'code' => 'ja',
    'name' => 'Japanese',
    'icon' => 'flag-icon-jp',
    'sort' => 30,
    'status' => 1,
],

Step 2: Add language values to gp247_languages table

[
    'code' => 'product.add_to_cart',
    'text' => 'カートに追加',
    'location' => 'ja',
    'sort' => 0,
],

Note: By default, GP247 only supports English (en) and Vietnamese (vi).


B. Update for Personal Website (Fastest Method)

If you only want to update for your own website, without forking the repository:

Step 1: Add new language type (if needed)

  • Access Admin Panel: {domain}/gp247_admin/language
  • Add new language (e.g., Japanese - code: ja)

Step 2: Manage language values

  • Access: {domain}/gp247_admin/language_manager
  • Add new or edit individual language values

Example of adding language via Admin:

Code: product.add_to_cart
Location: en
Text: Add to Cart

Method 2: Update via Laravel Files

Update data in language files following Laravel's default syntax.

Directory Structure

lang/
├── en/
│   ├── product.php
│   ├── cart.php
│   └── ...
├── vi/
│   ├── product.php
│   ├── cart.php
│   └── ...
├── en.json
└── vi.json

Example: File lang/en/product.php

 'Product Name',
    'price' => 'Price',
    'add_to_cart' => 'Add to Cart',
    'description' => 'Description',
    'in_stock' => 'In Stock',
    'out_of_stock' => 'Out of Stock',
];

Example: File lang/en.json

{
    "Welcome": "Welcome",
    "Login": "Login",
    "Register": "Register"
}

Usage in Code

// Method 1: Use gp247_language_render()
echo gp247_language_render('product.add_to_cart');

// Method 2: Use trans() helper (fallback)
echo trans('product.add_to_cart');

// Method 3: Use __() helper
echo __('product.add_to_cart');

Laravel Reference Documentation

Details about localization in Laravel:
https://laravel.com/docs/12.x/localization#introduction


3. Important Notes

Priority Order

Method 1 (Database) > Method 2 (Laravel Files)

The system will prioritize searching in the database first. Only when not found will it fallback to Laravel files.

Recommendations

  1. For large projects: Use Method 1 (Database) for easy management via Admin Panel
  2. For small projects: Use Method 2 (Laravel Files) for simplicity
  3. For community contributions: Update to GitHub repositories (Method 1A)

Performance

  • Database queries are automatically cached
  • Laravel translation files are cached by the Laravel framework
  • No significant performance difference between the 2 methods

4. Practical Examples

Scenario: Adding language for product page

Using Method 1 (Database - Admin Panel)

  1. Access {domain}/gp247_admin/language_manager
  2. Click "Add New"
  3. Fill in information:
    • Code: product.view_detail
    • Location: en
    • Text: View Details
  4. Click "Save"

Using Method 2 (Laravel File)

  1. Open file lang/en/product.php
  2. Add line:
'view_detail' => 'View Details',
  1. Save file

Usage in Blade Template



    {{ gp247_language_render('product.view_detail') }}



@lang('product.view_detail')

Conclusion

GP247's language system combines the advantages of both database management and Laravel translation files, providing high flexibility for developers and editors. Choose the method that suits your project needs!

Contact & Contribute: