Answer the question
In order to leave comments, you need to log in
Yii::t() and autocomplete - how to do it?
Something I do not understand the logic.
There is a table of tags, say, in English "id, name, frequency"
And there is a form with autocomplete, in which I want the tags to be substituted for the current language, say, ru
How should I use Yii::t()?
public static function suggest($keyword, $limit = 20)
{
$tags = self::find()
->where(['like', 'name', $keyword])
->orderBy('frequency DESC, name')
->limit($limit)
->all();
$names = array();
foreach ($tags as $tag) {
$names[] = $tag->name;
}
return $names;
}
Answer the question
In order to leave comments, you need to log in
It’s easier for you to change the structure to name_ru and name_en and already search in the desired column depending on the locale
Yii::t is mainly for interface translation. That is, we need pre-translated phrases in php or db.
You can, in principle, use your case. The problem is that the tags are not known in advance. respectively translation too. You need to hang a handler on the event of an untranslated phrase. My module https://github.com/zelenin/yii2-i18n-module
would be perfect for you
without any additions, it would store any phrases, including tags in the database, you would translate them as they come in.
Have questions?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question