Loading...
 
Skip to main content

Construction error in $lang, $lang_custom ??

Status
Closed
Subject
Construction error in $lang, $lang_custom ??
Version
18.x
Category
  • Support request
Feature
i18n (Multilingual, l10n, Babelfish)
Resolution status
New
Submitted by
hman
Lastmod by
hman
Rating
(1)
Related-to
Description

Today I have fought with a stubborn error in the output of my tools that extract and process the contents of tra() clauses. I could not find out what on earth changed key/value pairs, until I found an astonishing explanation: It's a bug, no, actually a feature of PHP that I didn't think of, and I believe also the devs who constructed Tikis tra() logic.
Basically there is an include file language.php which defines something like this:

Copy to clipboard
$lang = [ "first key" => "first value", ... "nth key" => "nth value", ];


Then there is another include file, custom.php, which can be edited through the custom translation admin page, which does something like this:

Copy to clipboard
$lang_custom = array( "something" => "else", );


Then -if the bug ever gets fixed that this is omitted- custom.php close with a merge:

Copy to clipboard
$lang = array_merge($lang, $lang_custom);


This is used so users/admins can modify the translation given in language.php. Newer values for keys given overwrite older definitions upon an array_merge.

Normally, this works as intended. BUT something completely different can happen if keys with numbers appear...

Voilà, according to PHP manual

"If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended. "

Which produces very funny (NOT) results, which are very hard to debug. And - language.php for German (de.de) DOES contain several numeric keys.

Also, a user or admin could at any time enter numeric keys. IMHO this "feature" should be documented and measures against numerical user entries be taken, unless I am completely mistaken. But simply try this little PHP demonstrator:

$lang is taken from the real language.php.

Copy to clipboard
<?php $lang = [ "1" => "1", "5" => "5", "10" => "10", "15" => "15", "30" => "30", "2" => "2", "4" => "4", "6" => "6", "7" => "7", "8" => "8", "9" => "9", "20" => "20", "40" => "40", "50" => "50", "60" => "60", ]; $lang_custom = array( "something" => "else", ); $merge = array_merge($lang, $lang_custom); print_r($merge); ?>

Solution

https://gitlab.com/tikiwiki/tiki/-/commit/682ab40b7b0de60853b2839a59986b0d1279170a
Backported in Tiki 23.x, 22.x, 21.x, 18.x

Importance
8
Easy to solve?
5
Priority
40
Demonstrate Bug on Tiki 19+
Demonstrate Bug (older Tiki versions)
Ticket ID
7712
Created
Saturday 27 March, 2021 17:16:29 UTC
by hman
LastModif
Friday 04 June, 2021 16:13:59 UTC


Show PHP error messages