Thursday 12 September 2019

In the WordPress i18n project, add prefixes by triggering internal links with variables

We are working on a localization project in WordPress. In fact, we can correctly add the active browser URL with language support. I'll add the details and the entire code below. Don't worry, my question will not be so open-ended and I will make it more specific.

Step 1

First of all, we should add a variable in the link structure to the WordPress algorithm.

function custom_rewrite_basic_query_vars( $query_vars ){
    $query_vars[] = 'lang';
    return $query_vars;
}
add_filter( 'query_vars', 'custom_rewrite_basic_query_vars' );

Thus, WordPress detects a variable such as ?lang= and includes it in the query mechanism.

Step 2

Using the "add_rewrite_rule" function, you need to add the variable ?lang= to run in the background of WordPress's persistent link structure.

Easy Example:

/*
LANG PREFIX + INDEX = localhost/en/ = localhost/?lang=en
*/

add_rewrite_rule(
    '^(en|fr|de|ru|tr)/?$',
    'index.php?lang=$matches[1]',
    'top'
);

I will not add the remaining instances one by one, because you should use close to 20 rewrite rules. (Date, Category, Comment, Page, Text, Pagination, etc.)

NOTE: Those who work on such a project and want to use these rules will add it if requested.


Now our persistent connection structure supports variables for the language option.

localhost/en/hello-world

localhost/ru/hello-world

localhost/de/04/07/2019

localhost/fr/page/2

etc. all the links you can think of.

Step 3

If a variable is detected from the browser (for example: lang = en), we will display the contents of the previously saved inter-language translated.

At this point we do not need any support. But when we get to step 4, we're stuck.

Step 4

Activating the prefix language variable that was added before, users when using the links within the site, on all in-site navigation links.

This means that if a user has access to the localhost/fr/hello-world link, they must reach the localhost/fr/contact link when they return to the Home Page or click the Contact link.

To do this, you need to add the language prefix that is currently active in the browser to all in-site links.

Unfortunately, this is the only point where we hang out and we can't find a solution.

In fact, we tried most things before writing here. (Of course within our knowledge). We even looked at plug-ins that offer some language support.

e.g. <WP Multilang>

Of course, it's so complicated and so much code. We didn't even understand the plug-in.

There is, of course, a logical way of doing this, and I hope that person sees this post and answers. Thank you sincerely for all your supports.



from In the WordPress i18n project, add prefixes by triggering internal links with variables

No comments:

Post a Comment