We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Placeholders does not replace in translation with not existing indexes

Hi all,

I use nativeArrayadapter for my translation. in my controller I try to translate a message and send it to user:

$message = $this->translation->_("You are logged in as %username%", ['username' =>$user->username]);

If "You are logged in as %username%" key is not defined in language array, then the index returns as is, without replacement!

See this section of source code:

    /**
     * Returns the translation related to the given key
     *
     * @param string  index
     * @param array   placeholders
     * @return string
     */
    public function query(string! index, placeholders = null) -> string
    {
        var translation;

        if fetch translation, this->_translate[index] {
            return this->replacePlaceholders(translation, placeholders);
        }
        return index;
    }

This issue does not append in getext addapter. U can see the source. But in csvand nativeArray addpter already happens.

I think this is a Bug.

Any idea?

Thanks

edited Jun '15

I solve my problem by overriding nativeArray::query() in main class, as follow:

    /**
     * Returns the translation related to the given key
     */
    public function query($index, $placeholders = null)
    {
        $translation = isset($this->_translate[$index]) // <-- isset() is faster in large arrays and it's NULL-proof
            ? $this->_translate[$index]
            : $index;

        return $this->replacePlaceholders($translation, $placeholders);
    }

May helps.

regards.

If you think this is a bug then it must be posted on Github.



34.6k
Accepted
answer

I've added a fix in the 2.0.3 branch

Great :)

I've added a fix in the 2.0.3 branch