How to translate back, using Yii:t() method?

Well, Yii:t()… it’s work fine! Superfine ;D

In my dictionary ‘dic’ (messages/en/dic.php) has written something like this:




<?php

return array (

   'key1' => 'value1',

   'key2' => 'value2',

);



Now i’ve to do inverse operation: i’ve got ‘value1’.

Can i get ‘key1’ using same (‘dic’) dictionary?

You can use php method array_search(http://php.net/manual/en/function.array-search.php), it will return you key, which is value you are looking for.

Also you can override Yii::t method, or write new method, in class that extends YiiBase class, or just write new class, depending on your needs.

problem is how to get this array…

Is digging to Yii::app()->messages in right direction? :)

Easy one I think:

I have two functions in my module:

[Situation]

Module name: MyModule

Message file: messages

Source language: English

Target language: Dutch

[Functions]

public static function t($str=’’,$params=array(),$dic=‘messages’) {

return Yii::t("MyModule.".$dic, $str, $params);

}

public static function t_inv($str=’’,$params=array(),$dic=‘messages’) {

&#036;source = Yii::app()-&gt;sourceLanguage;


&#036;target = Yii::app()-&gt;language;


return Yii::t(&quot;MyModule.&quot;.&#036;dic, &#036;str, &#036;params,&#036;target,&#036;source);

}

So if you want to translate say, ‘Blue’ into Dutch (‘Blauw’), you call t. If you want to translate the Dutch word back into English, you call t_inv.

echo Yii::app()->getModule(‘MyModule’)->t(‘Blue’); will produce ‘Blauw’ on the Dutch version of the website.

Yii::app()->getModule(‘MyModule’)->t_inv(‘Blauw’) will produce ‘Blue’ on the Dutch version of the website.

So to cut things short, you only need to swap the source and the target on the original Yii:t call.

this solution supposes just two dictionary files

it works but it’s not exactly what i need :)

i don’t need translation

I’ve to get key of message by it’s value array

cause it’s key for database table

Hey, try to use the array_flip php function.

What does it do?

keys become values and values become keys…

so, all you have to do is to create the file that returns the "inverse" array as

inverseDic.php




$invArray = require('dic.php');

return array_flip($invArray);



Not tested, just a thought

Hope this helps!

:)

Not enough perfect code but has the place to be :)

I’ve tried to find Yii’s method to get this array.

it works, of couse! :)