Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajax also redirects when enableLanguagePersistence is turned on #151

Closed
kayw-geek opened this issue Oct 21, 2019 · 2 comments
Closed

Ajax also redirects when enableLanguagePersistence is turned on #151

kayw-geek opened this issue Oct 21, 2019 · 2 comments

Comments

@kayw-geek
Copy link

Ajax also redirects when enableLanguagePersistence is turned on,Can I add a judgment or configuration item without redirecting when ajax requests?

` if (
// Only add language if it's not empty and ...
$language!=='' && (

                // ... it's not the default language or default language uses URL code ...
                !$isDefaultLanguage || $this->enableDefaultLanguageUrlCode ||

                // ... or if a language is explicitely given, but only if
                // either persistence or detection is enabled.  This way a
                // "reset URL" can be created for the default language.
                $isLanguageGiven && ($this->enableLanguagePersistence || $this->enableLanguageDetection)
            ) && !Yii::$app->request->isAjax
        ) {
            $key = array_search($language, $this->languages);
            if (is_string($key)) {
                $language = $key;
            }
            if (!$this->keepUppercaseLanguageCode) {
                $language = strtolower($language);
            }

            // Calculate the position where the language code has to be inserted
            // depending on the showScriptName and baseUrl configuration:
            //
            //  - /foo/bar -> /de/foo/bar
            //  - /base/foo/bar -> /base/de/foo/bar
            //  - /index.php/foo/bar -> /index.php/de/foo/bar
            //  - /base/index.php/foo/bar -> /base/index.php/de/foo/bar
            //
            $prefix = $this->showScriptName ? $this->getScriptUrl() : $this->getBaseUrl();
            $insertPos = strlen($prefix);

            // Remove any trailing slashes for root URLs
            if ($this->suffix !== '/') {
                if (count($params) === 1 ) {
                    // / -> ''
                    // /base/ -> /base
                    // /index.php/ -> /index.php
                    // /base/index.php/ -> /base/index.php
                    if ($url === $prefix . '/') {
                        $url = rtrim($url, '/');
                    }
                } elseif (strncmp($url, $prefix . '/?', $insertPos + 2) === 0) {
                    // /?x=y -> ?x=y
                    // /base/?x=y -> /base?x=y
                    // /index.php/?x=y -> /index.php?x=y
                    // /base/index.php/?x=y -> /base/index.php?x=y
                    $url = substr_replace($url, '', $insertPos, 1);
                }
            }

            // If we have an absolute URL the length of the host URL has to
            // be added:
            //
            //  - http://www.example.com
            //  - http://www.example.com?x=y
            //  - http://www.example.com/foo/bar
            //
            if (strpos($url, '://')!==false) {
                // Host URL ends at first '/' or '?' after the schema
                if (($pos = strpos($url, '/', 8))!==false || ($pos = strpos($url, '?', 8))!==false) {
                    $insertPos += $pos;
                } else {
                    $insertPos += strlen($url);
                }
            }
            if ($insertPos > 0) {
                return substr_replace($url, '/' . $language, $insertPos, 0);
            } else {
                return '/' . $language . $url;
            }
        }`
@mikehaertl
Copy link
Collaborator

This could only happen if you send AJAX requests without the correct language in the URL. This should never be the case. You should create the AJAX URLs from Yii, e.g. with Url::to(), then they will contain the correct language code.

@kayw-geek
Copy link
Author

Well, this is currently done, just feel that the ajax request does not need to be redirected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants