How to make a site with pretty URLs with i18n?












1














First of all, I read this issue which seemed to me to correspond to solving my problem but I don't understand it after several rereading and testing: https://github.com/i18next/react-i18next/issues/325
(maybe because I'm not using react)



I also tried this one (with .htaccess) : https://github.com/i18next/i18next/issues/144



But it gives a 404 error.



I recently tried to make a site with i18next, which works fine but I would like to be able to add nicer URLs with country codes.





Example instead of (which change the language):



https://www.mywebsite.com/index.html?lng=fr-BE



this



https://www.mywebsite.com/fr-BE





Knowing that I can't use back-end language like PHP, etc... that I use an Apache web server and that I only use i18next with jquery.i18next.



To describe my problem a little more in depth, how could I make sure that when a user arrives on any page it adds the country code?
What if it already has a country code in the URL, change the user's language?



And above all, is it possible?



Thank you for reading my issue and hoping that you can help me with my problem. :)



P.S.: Please excuse me for my English










share|improve this question



























    1














    First of all, I read this issue which seemed to me to correspond to solving my problem but I don't understand it after several rereading and testing: https://github.com/i18next/react-i18next/issues/325
    (maybe because I'm not using react)



    I also tried this one (with .htaccess) : https://github.com/i18next/i18next/issues/144



    But it gives a 404 error.



    I recently tried to make a site with i18next, which works fine but I would like to be able to add nicer URLs with country codes.





    Example instead of (which change the language):



    https://www.mywebsite.com/index.html?lng=fr-BE



    this



    https://www.mywebsite.com/fr-BE





    Knowing that I can't use back-end language like PHP, etc... that I use an Apache web server and that I only use i18next with jquery.i18next.



    To describe my problem a little more in depth, how could I make sure that when a user arrives on any page it adds the country code?
    What if it already has a country code in the URL, change the user's language?



    And above all, is it possible?



    Thank you for reading my issue and hoping that you can help me with my problem. :)



    P.S.: Please excuse me for my English










    share|improve this question

























      1












      1








      1







      First of all, I read this issue which seemed to me to correspond to solving my problem but I don't understand it after several rereading and testing: https://github.com/i18next/react-i18next/issues/325
      (maybe because I'm not using react)



      I also tried this one (with .htaccess) : https://github.com/i18next/i18next/issues/144



      But it gives a 404 error.



      I recently tried to make a site with i18next, which works fine but I would like to be able to add nicer URLs with country codes.





      Example instead of (which change the language):



      https://www.mywebsite.com/index.html?lng=fr-BE



      this



      https://www.mywebsite.com/fr-BE





      Knowing that I can't use back-end language like PHP, etc... that I use an Apache web server and that I only use i18next with jquery.i18next.



      To describe my problem a little more in depth, how could I make sure that when a user arrives on any page it adds the country code?
      What if it already has a country code in the URL, change the user's language?



      And above all, is it possible?



      Thank you for reading my issue and hoping that you can help me with my problem. :)



      P.S.: Please excuse me for my English










      share|improve this question













      First of all, I read this issue which seemed to me to correspond to solving my problem but I don't understand it after several rereading and testing: https://github.com/i18next/react-i18next/issues/325
      (maybe because I'm not using react)



      I also tried this one (with .htaccess) : https://github.com/i18next/i18next/issues/144



      But it gives a 404 error.



      I recently tried to make a site with i18next, which works fine but I would like to be able to add nicer URLs with country codes.





      Example instead of (which change the language):



      https://www.mywebsite.com/index.html?lng=fr-BE



      this



      https://www.mywebsite.com/fr-BE





      Knowing that I can't use back-end language like PHP, etc... that I use an Apache web server and that I only use i18next with jquery.i18next.



      To describe my problem a little more in depth, how could I make sure that when a user arrives on any page it adds the country code?
      What if it already has a country code in the URL, change the user's language?



      And above all, is it possible?



      Thank you for reading my issue and hoping that you can help me with my problem. :)



      P.S.: Please excuse me for my English







      javascript apache url-rewriting i18next pretty-urls






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 19:57









      John DelmezJohn Delmez

      62




      62
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Totally possible.



          So first in your .htaccess file you'd want to have something like this:



          RewriteEngine On
          RewriteRule ^(w+)/?([w-]+)?$ $1.html?lng=$2


          This would rewrite a URL of the form



          www.example.com/index/fr-BE to www.example.com/index.html?lng=fr-BE



          So I'm using this approach because you may want to consider the language parameter optional and default it to a certain value. And this rule will also redirect



          www.example.com/some_page to www.example.com/some_page.html



          That's that with the friendly URLs. Now for the part of getting the language param from the query string. Without using a server side language (like PHP) you can't be using the value from 'lng' parameter, but following the guide from here you can use window.location.pathname to get the language string, when present.



          So for a URL of the form www.example.com/index/fr-BE, you could be using the following code:



          let params = window.location.pathname.split('/').slice(1);

          if(typeof params[1] !== 'undefined') {
          console.log(params[1]); // <-- should be 'fr-BE'
          i18next.setLng(params[1]);
          }


          Hope this helps.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452331%2fhow-to-make-a-site-with-pretty-urls-with-i18n%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Totally possible.



            So first in your .htaccess file you'd want to have something like this:



            RewriteEngine On
            RewriteRule ^(w+)/?([w-]+)?$ $1.html?lng=$2


            This would rewrite a URL of the form



            www.example.com/index/fr-BE to www.example.com/index.html?lng=fr-BE



            So I'm using this approach because you may want to consider the language parameter optional and default it to a certain value. And this rule will also redirect



            www.example.com/some_page to www.example.com/some_page.html



            That's that with the friendly URLs. Now for the part of getting the language param from the query string. Without using a server side language (like PHP) you can't be using the value from 'lng' parameter, but following the guide from here you can use window.location.pathname to get the language string, when present.



            So for a URL of the form www.example.com/index/fr-BE, you could be using the following code:



            let params = window.location.pathname.split('/').slice(1);

            if(typeof params[1] !== 'undefined') {
            console.log(params[1]); // <-- should be 'fr-BE'
            i18next.setLng(params[1]);
            }


            Hope this helps.






            share|improve this answer


























              0














              Totally possible.



              So first in your .htaccess file you'd want to have something like this:



              RewriteEngine On
              RewriteRule ^(w+)/?([w-]+)?$ $1.html?lng=$2


              This would rewrite a URL of the form



              www.example.com/index/fr-BE to www.example.com/index.html?lng=fr-BE



              So I'm using this approach because you may want to consider the language parameter optional and default it to a certain value. And this rule will also redirect



              www.example.com/some_page to www.example.com/some_page.html



              That's that with the friendly URLs. Now for the part of getting the language param from the query string. Without using a server side language (like PHP) you can't be using the value from 'lng' parameter, but following the guide from here you can use window.location.pathname to get the language string, when present.



              So for a URL of the form www.example.com/index/fr-BE, you could be using the following code:



              let params = window.location.pathname.split('/').slice(1);

              if(typeof params[1] !== 'undefined') {
              console.log(params[1]); // <-- should be 'fr-BE'
              i18next.setLng(params[1]);
              }


              Hope this helps.






              share|improve this answer
























                0












                0








                0






                Totally possible.



                So first in your .htaccess file you'd want to have something like this:



                RewriteEngine On
                RewriteRule ^(w+)/?([w-]+)?$ $1.html?lng=$2


                This would rewrite a URL of the form



                www.example.com/index/fr-BE to www.example.com/index.html?lng=fr-BE



                So I'm using this approach because you may want to consider the language parameter optional and default it to a certain value. And this rule will also redirect



                www.example.com/some_page to www.example.com/some_page.html



                That's that with the friendly URLs. Now for the part of getting the language param from the query string. Without using a server side language (like PHP) you can't be using the value from 'lng' parameter, but following the guide from here you can use window.location.pathname to get the language string, when present.



                So for a URL of the form www.example.com/index/fr-BE, you could be using the following code:



                let params = window.location.pathname.split('/').slice(1);

                if(typeof params[1] !== 'undefined') {
                console.log(params[1]); // <-- should be 'fr-BE'
                i18next.setLng(params[1]);
                }


                Hope this helps.






                share|improve this answer












                Totally possible.



                So first in your .htaccess file you'd want to have something like this:



                RewriteEngine On
                RewriteRule ^(w+)/?([w-]+)?$ $1.html?lng=$2


                This would rewrite a URL of the form



                www.example.com/index/fr-BE to www.example.com/index.html?lng=fr-BE



                So I'm using this approach because you may want to consider the language parameter optional and default it to a certain value. And this rule will also redirect



                www.example.com/some_page to www.example.com/some_page.html



                That's that with the friendly URLs. Now for the part of getting the language param from the query string. Without using a server side language (like PHP) you can't be using the value from 'lng' parameter, but following the guide from here you can use window.location.pathname to get the language string, when present.



                So for a URL of the form www.example.com/index/fr-BE, you could be using the following code:



                let params = window.location.pathname.split('/').slice(1);

                if(typeof params[1] !== 'undefined') {
                console.log(params[1]); // <-- should be 'fr-BE'
                i18next.setLng(params[1]);
                }


                Hope this helps.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 25 '18 at 19:57









                Dan D.Dan D.

                574213




                574213






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452331%2fhow-to-make-a-site-with-pretty-urls-with-i18n%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                    Calculate evaluation metrics using cross_val_predict sklearn

                    Insert data from modal to MySQL (multiple modal on website)