Coder Social home page Coder Social logo

Comments (8)

mikehaertl avatar mikehaertl commented on August 28, 2024

I don't use that user module. Does it work without it? Do you have to add any URL configuration for that module?

from yii2-localeurls.

flybot avatar flybot commented on August 28, 2024

As I understand, the problem arises precisely when called from any module. Looks like as Yii2 bug.
In addition, there is a problem when using links with data-method=POST. After redirect the request is converted to GET. I think this is also a Yii bug.
But if you will have a decision it will be very good.
The bug is in this application

from yii2-localeurls.

mikehaertl avatar mikehaertl commented on August 28, 2024

Hmm. I've used it from modules and had no problems so far. But I can check again.

About the data-method POST links: They should already point to the correct language URL. Because when you load the page, which contains such a link, you should already be redirected to your language. So this should never be a problem.

from yii2-localeurls.

wartur avatar wartur commented on August 28, 2024

I think, that dektrium/yii2-user have bad realisation. Me need this solution and I will see what not work there. Maybe fix it.

from yii2-localeurls.

wartur avatar wartur commented on August 28, 2024

Generate promlem this:
https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseUrl.php#L132-L138

It is occurs bacause this
https://github.com/codemix/yii2-localeurls#example-language-selection-widget

$route = Yii::$app->controller->route; // <---- THIS
$appLanguage = Yii::$app->language;
$params = $_GET;
//......

In $route we get wrong route if the module is activated. (e.g. dektrium/yii2-user).

As second version it is Yii bug somewhere in that place where I have pointed above.

Yii::$app->controller->route // MAY BE THIS IS BUG!

Or this on generate
https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseUrl.php#L132-L138

FOR FIX. Need use additional normalize in language switcher. It's bad hack but it's working.

$route = Yii::$app->controller->route;
$moduleId = Yii::$app->controller->module->getUniqueId();
$params = $_GET;

if($moduleId) {
    $route = strpos($route, $moduleId) == 0 ? substr($route, strlen($moduleId) + 1) : $route;
}

array_unshift($params, $route);

$localeUrlsLanguages = Yii::$app->localeUrls->languages;
foreach ($localeUrlsLanguages as $language) {
    $params['language'] = $language;

    $label = self::label($language);
    if ($label === null) {
        throw new yii\base\InvalidConfigException('Language translate not support');
    } else {
        $this->items[] = [
            'label' => $label,
            'url' => $params,
        ];
    }
}
parent::init();

Late with my the project, so who can write this bug in the Community framework. Good by!

from yii2-localeurls.

mikehaertl avatar mikehaertl commented on August 28, 2024

I think, all you have to change is to add a / in front of the route:

<?php
$route = '/'.Yii::$app->controller->route;

I've tested it here and it works, but there may be situations where it's still making problems, not sure.

@wartur Can you confirm, that this fix works for you?

from yii2-localeurls.

wartur avatar wartur commented on August 28, 2024

@mikehaertl yea! it's work! Thanks. It's good implementation.

Now I publick own Langage Switcher. I get image from Joomla Langage switcher. If you want, you can set your own images.

<?php

namespace app\widget\special;

use Yii;
use yii\helpers\Html;

/**
 * Переключатель языков.
 * Выводит флаги языков по тому же принципу как это сделано в Joomla
 */
class LanguageSwitcher extends \yii\widgets\Menu
{
    /**
     * Специальная конфигурация меню
     */
    public function init()
    {
        // отключим энкодер лейблов
        $this->encodeLabels = false;

        // конфигугируем меню
        $route = '/' . Yii::$app->controller->route;
        $appLanguage = Yii::$app->language;
        $params = $_GET;

        array_unshift($params, $route);

        $localeUrlsLanguages = Yii::$app->localeUrls->languages;
        foreach ($localeUrlsLanguages as $language) {
            $params['language'] = $language;

            $this->items[] = [
                'label' => self::imageLabel($language),
                'url' => $params,
                'active' => $language == $appLanguage
            ];
        }
        parent::init();
    }

    /**
     * Генерация изображений меню
     * В качестве изображений берутся изображения Joomla
     * @staticvar array $labels список изображений
     * @param string $code код языка
     * @return string HTML строка с генерацией изображений для языка
     * @throws yii\base\InvalidConfigException исключение происходит, если нет конфигурации для данного языка
     */
    public static function imageLabel($code)
    {
        static $labels = [];

        if (empty($labels)) {
            $labels = [
                'en-US' => Html::img('/info/media/mod_languages/images/en.gif', [
                    'alt' => self::label('en-US'),
                    'title' => self::label('en-US'),
                ]),
                'ru' => Html::img('/info/media/mod_languages/images/ru.gif', [
                    'alt' => self::label('ru'),
                    'title' => self::label('ru'),
                ]),
                'de' => Html::img('/info/media/mod_languages/images/de.gif', [
                    'alt' => self::label('de'),
                    'title' => self::label('de'),
                ]),
                'it' => Html::img('/info/media/mod_languages/images/it.gif', [
                    'alt' => self::label('it'),
                    'title' => self::label('it'),
                ]),
                'es' => Html::img('/info/media/mod_languages/images/es.gif', [
                    'alt' => self::label('es'),
                    'title' => self::label('es'),
                ]),
            ];
        }

        if (empty($labels[$code])) {
            throw new yii\base\InvalidConfigException('Language translate not support');
        }

        return $labels[$code];
    }

    /**
     * Генерация названий языков на разных языках
     * @staticvar array $labels список изображений
     * @param string $code код языка
     * @return string строка с названием языка с переводом на текущем языке
     * @throws yii\base\InvalidConfigException исключение происходит, если нет конфигурации для данного языка
     */
    public static function label($code)
    {
        static $labels = [];

        if (empty($labels)) {
            $labels = [
                'en-US' => Yii::t('app/language_switcher', 'English'),
                'ru' => Yii::t('app/language_switcher', 'Russian'),
                'de' => Yii::t('app/language_switcher', 'German'),
                'it' => Yii::t('app/language_switcher', 'Italian'),
                'es' => Yii::t('app/language_switcher', 'Spanish'),
            ];
        }

        if (empty($labels[$code])) {
            throw new yii\base\InvalidConfigException('Language translate not support');
        }

        return $labels[$code];
    }

}

It's my draft style

.language-switcher {

}

.language-switcher .container{
    text-align: right;
    position: relative;
    margin-top: -20px;
    z-index: 2;
}

.language-switcher ul{
    margin: 0;
    padding: 0;
    list-style: none;
}

.language-switcher li{
    display: inline-block;
    margin-left: 5px;
    margin-right: 5px;
    width: 25px;
    text-align: center;
}

.language-switcher li.active {
    /*border-bottom: red medium solid;*/
    background-color: #222;
    border: #080808 1px solid;

    -webkit-border-bottom-right-radius: 5px;
    -webkit-border-bottom-left-radius: 5px;
    -moz-border-radius-bottomright: 5px;
    -moz-border-radius-bottomleft: 5px;
    border-bottom-right-radius: 5px;
    border-bottom-left-radius: 5px;
}

screenshot from 2015-03-11 12 37 23

Thanks all. Good by.

from yii2-localeurls.

mikehaertl avatar mikehaertl commented on August 28, 2024

Ok, thanks for sharing.

from yii2-localeurls.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.