Oihana PHP System

filterLanguages.php

Table of Contents

Functions

filterLanguages()  : array<string, string|null>|null
Filter an array or object of translations according to the given or available languages.

Functions

filterLanguages()

Filter an array or object of translations according to the given or available languages.

filterLanguages(array<string, string>|object|null $fields[, array<string|int, string>|null $languages = null ][, callable|null $sanitize = null ]) : array<string, string|null>|null

This helper transforms an input array/object from the client to prepare a multilingual (i18n) property. It keeps only string or null values, allows optional transformation or sanitization via a callback.

Parameters
$fields : array<string, string>|object|null

Input array or object of translations.

$languages : array<string|int, string>|null = null

Optional array of languages to filter the i18n definitions. If null, no filtering is applied.

$sanitize : callable|null = null

Optional callback to transform or sanitize each value. Signature: fn(string|null $value, string $lang): string|null

Tags
example
$translations = [
'fr' => 'Bonjour <span style="color:red">monde</span>',
'en' => 'Hello <span style="color:red">world</span>',
'de' => 42, // ignored because not string/null
'es' => null
];

// Basic filtering for 'fr' and 'en'
$filtered = filterLanguages($translations, ['fr', 'en']);
// [
//     'fr' => 'Bonjour <span style="color:red">monde</span>',
//     'en' => 'Hello <span style="color:red">world</span>'
// ]

// Filtering with HTML sanitization
$sanitized = filterLanguages($translations, ['fr', 'en'], function($value, $lang) {
if (is_string($value)) {
return preg_replace('/(<[^>]+) style=".*?"/i', '$1', $value);
}
return $value;
});
// [
//     'fr' => 'Bonjour <span>monde</span>',
//     'en' => 'Hello <span>world</span>'
// ]

// Filtering with custom transformation: uppercase strings
$upper = filterLanguages($translations, ['fr', 'en'], fn($v, $lang) => is_string($v) ? strtoupper($v) : $v);
// [
//     'fr' => 'BONJOUR <SPAN STYLE="COLOR:RED">MONDE</SPAN>',
//     'en' => 'HELLO <SPAN STYLE="COLOR:RED">WORLD</SPAN>'
// ]
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
array<string, string|null>|null

Filtered translations matching the languages, or null if input is empty.


        
On this page

Search results