1

I Have Url structure for multi language system like this:

for default language ie: en

localhost/cms

And Another Language Like:

localhost/cms/fr

localhost/cms/de

localhost/cms/es

Now I have this function for detect empty url language short name parts and set to default language:

function urlss(){

    $uri = $_SERVER['REQUEST_URI'];

    $languages = ['en', 'fr', 'es', 'de'];

    // this is my default language
    $defaultLang = 'en';

    // $currentLang is the language I'll use to show the contents
    $currentLang = $defaultLang;

    $uri = ltrim(rawurldecode($uri), '/');
    $parts = explode('/', $uri);

    if( ! empty($parts) && in_array(strtolower($parts[0]), $languages)) {
        $currentLang = array_shift($parts);
    }

    $routableUri = implode('/', $parts);

return $routableUri;

}

Check in Url: localhost/cms/ and Output Is: cms/ and not work true.

In action I need to check My url, if not detect language short name ie: en default language = en and then put in session or cookie But if detect language short name ie: fr es de set session or cookie to this language.

How do can I fix my problem and check my url for language short name?!

1
  • Are you sure you want to write your own routing system? Why not use an existing one that can handle all that parsing stuff for you? Commented Jun 19, 2018 at 7:36

4 Answers 4

1

I would approach this problem using a regular expression.

// Prepare request URI, available languages and default language
$url = $_SERVER['REQUEST_URI'];
$languages = ['en', 'fr', 'es', 'de'];
$default = 'en';

// Build regular expression and attempt to match language in URL
preg_match('/^\/cms\/(' . implode('|', $languages) . ')\//', $url, $match);

// Set language to match or default to English
$language = $match[1] ?? $default;

// Remove default language from URL
$url = preg_replace("/\/{$default}/", '', $url);
Sign up to request clarification or add additional context in comments.

12 Comments

all output is en. ie: check localhost/cms/fr output is : en. localhost/cms ouput is: en localhost/cms/de output is 'en' and more .....
Your example URLs show the language portion being the first part of the request URI - example.com/de, example.com/fr? Please update your question to show representative examples.
Ok. You Right. I edit My Q. Actually i check in my localhost url and then in real url.
I've updated my answer based on the URL always starting with /cms/. If this isn't correct, please update your question again.
thanks worked now. but for route url i need to print full structure like this: for another lang cms/fr/article/slug/... and for default lang cms/article/slug/....
|
0

The regex approach is better one another way is you can take an intersection of $languages array with URL $parts.

function urlss(){

    $uri = $_SERVER['REQUEST_URI'];

    $languages = ['en', 'fr', 'es', 'de'];

    // this is my default language
    $defaultLang = 'en';

    // $currentLang is the language I'll use to show the contents
    $currentLang = $defaultLang;

    $uri = ltrim(rawurldecode($uri), '/');
    // uri might have many / characters
    $parts = explode('/', $uri);

    if( ! empty($parts)) {
        $result = array_uintersect($languages , $parts, "strcasecmp");
        if(!empty($result)){ 
           $currentLang = $result[0];
           $parts = array_diff($parts, [ $result[0] ]);
        }
    }

    $routableUri = implode('/', $parts);

    return $routableUri;

}

Comments

0
$languages = ['en', 'fr', 'es', 'de'];
$defaultLang = 'en';

$url = $_SERVER['REQUEST_URI'];
$url = rtrim($url, '/');//remove last slash if available
$parsed_arr = parse_url($url);//not necessary if you don't want to validate url 
$segments = explode("/", $parsed_arr['path']);//split parsed url into segments
$language = end($segments); //take the last one

if(in_array($language,$languages))
    $currentLang = $language;
else
    $currentLang = $defaultLang;     

Comments

0

Lets have a try this code :

Check This out list()

Rest you can easily change as per your need but i have corrected the logic of function

<?php
   $currentUrls= array('cms/es','cms/en','cms/de','cms/fr','cms/');
   echo 'Before::<br >';
   foreach($currentUrls as $currentUrl){
      echo ($currentUrl);
     echo '<hr/>';
   }

function urlss($currentUrl){
    $uri =$currentUrl;
    $languages = ['en', 'fr', 'es', 'de'];
    // this is my default language
   $defaultLang = 'en';
   // $currentLang is the language I'll use to show the contents
   $currentLang = $defaultLang;
   $uri = ltrim(rawurldecode($uri), '/');       
   list($cms,$url_language) = explode('/', $uri);     

  //if langugage is in array then 
  if( in_array( strtolower($url_language), $languages)){
      $currentLang = $url_language;      
  }else { 
      $currentLang = $defaultLang;      
  }

  $newUrl = array($cms,$currentLang);
  $routableUri = implode('/', $newUrl);
  return $routableUri;
 }

 echo '<br ><br >After::<br >';
 foreach($currentUrls as $currentUrl){
    echo urlss($currentUrl);
    echo '<hr/>';
 }

?>

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.