Mult-lingua site with regular expressions

Asked

Viewed 40 times

0

Hey, guys, all right?

I have a problem to identify the page with the language at the same time. Just pulling the index with the languages and the other pages not.

This is the url expression: main.php

$urlpatterns = array(
    '/' => 'index.php',
    '/(?P<lang>\S+)' => 'index.php',
    '/(?P<lang>\S+)/quem-somos' => 'quem-somos.php',
    '/(?P<lang>\S+)/contato' => 'contato.php'
);

And this is the configuration of php translator.

if (isset($_GET['lang'])) { 
    $lang = explode("/", $_GET['lang']);

    if ($lang[0] == "br") { $lang[0] = "br"; require_once ('br.php'); } 
    if ($lang[0] == "en") { $lang[0] = "en"; require_once ('en.php'); } 
    if ($lang[0] == "es") { $lang[0] = "es"; require_once ('es.php'); } 

} elseif (!isset($_GET['lang'])) {
    require_once ('br.php');
}

The URL looks like this: meusite.com.br/br or meusite.com.br/br/

But only search the home page, when I search for meusite.com.br/br/who-we are or meusite.com.br/en/who-we are, do not seek.

Could someone help me?

If I format everything on php translator., expressions will be ignored in main.php?

  • Well, your expression for language is (?P<lang>\S+), which is basically any string with one or more characters that are not blank spaces. The URL /en/quem-somos is a sequence of more than one character that are not blank spaces, so the displayed file should be index.php?

  • @Andersoncarloswoss I get it. What should I take out of this expression of mine to work? To identify the en-separate of who-we are: en**/**who we are?

  • Indicating that lang must not possess / at value, for example, (?P<lang>[^/]+). This will define "lang" as any sequence of one or more characters that are not the bar. You can edit the expression to ignore other characters too if desired.

  • When I use this form it ends up giving error here in my code in the line of preg_match(): &#xA;function url_response($urlpatterns) {&#xA; foreach ($urlpatterns as $pcre => $app) {&#xA; if(preg_match("@^{$pcre}$@", REQUEST_URI, $_GET)) {&#xA; include(APPLICATION_DIR.'/'.$app);&#xA; exit();&#xA; } else {&#xA; $erro = true;&#xA; }&#xA; }&#xA; if(isset($erro) && $erro == true){&#xA; include("app/404.php");&#xA; }&#xA; return;&#xA;}&#xA;

  • What mistake would that be?

  • It worked I ended up taking a parentheses in my edition. It worked as I expected. Thanks for the help!!!!!!

Show 1 more comment

1 answer

1


The problem is in the regular expression you are using. You have defined lang as (?P<lang>\S+), however the \S house with any character other than white spaces (' ', \n, \t, etc.).

Thus, the URL /en/quem-somos is interpreted entirely as lang and the displayed page will be index.php, because you set it:

'/(?P<lang>\S+)' => 'index.php'

How are you defining that lang will be an element of path, the ideal is that you delimite its value with the delimiter character of the path: the bar, /. For this, one way to implement would be to define that lang can be anything except the bar:

(?P<lang>[^/]+)

And in this way, only en will be considered lang in the URL /en/quem-somos.

  • Good afternoon!!! It worked perfectly!!!!! Thank you for your willingness and help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.