-1

I need to make landing page on 2 languages, swapping language by click on the flag.

How I understand, I need to save language state in user's localstorage / cookies.

I see here 3 options:

  • Upload on user machine .json with content on 2 languages. At the opening of page script will check localstorage for availability of lang variable and after it fill page by JS.

  • Save lang variable in cookies, and at appeal to the server, server must make magic and send back page built on user language.

  • Make duplicates of each page in each language.

Question 1:
Which way better, when and why? (cases: one page landing, site with many pages)

Question 2:
When I make duplicates of page, is it normal to have no index.html in root directory at all, but:

/en/index.html  
/ru/index.html  

and .htaccess file that redirect to /en/ by default, check cookies for lang variable, and if exist redirect user to specific folder.

Mitrofan Ius
  • 45
  • 1
  • 6
  • 1
    I think `best way` is primarily opinion biaised and depend of how you will do it. For example, if you use some framework (Symfony for example) you have component to make it works. – Mickaël Leger Jul 24 '18 at 10:10
  • Way too broad but generally speaking you'd have a CMS that stores page/language data in the database with maybe a config/csv file for holding short translations (login -> inloggen) for instance. Wordpress, Joomla and Drupal all support this natively or through plugins I believe. – CD001 Jul 24 '18 at 10:15
  • Thanks @Mickael-Leger , but I do not yet use frameworks. On the Internet there are many different solutions, I would like to understand how not to run into problems with SEO or performance. – Mitrofan Ius Jul 24 '18 at 10:19
  • @MitrofanIus the best/easiest way todo it in your case is option 2. For the performance option 3 would be a little bit better but you will have unmaintainable code. – marcramser Jul 24 '18 at 10:26
  • array with data on both languages – Leo Tahk Jul 24 '18 at 10:50

2 Answers2

1

I would store each one of translations in a json file (en.json, ru.json) and change languages using url parameters. index.html?lang=ru (This way you don't have to handle different paths)

If the url parameter exists, save it on localstorage, load the respective json and fill the page with the tanslated strings. If there is no url parameter, look in the localstorage for a language. If there is nothing there, assume a default (en, for example) and load the translated strings.

Bruno Camarneiro
  • 545
  • 4
  • 19
  • No, don't do that. A cookie is what you want to do in this case not a GET param you have to send for every request. – marcramser Jul 24 '18 at 10:22
  • I'm not advocating that he has to send the parameter in all GET requests. He just needs to do that when the user wants to change the language. And yes, probably is better to use cookies to store it (that really depends on his needs - https://stackoverflow.com/a/19869560/1694735). After that, since that option is already saved "somewhere", he doesn't need to add the parameter to all requests. Oh, and I made the assumption that he has access to all JSON files (locally or by requesting them) – Bruno Camarneiro Jul 24 '18 at 11:02
1

Question 1: Option 1: Good practice if you have some sort of frontend framework (like Angular or React). But if you have a static (or only serverside rendering) page it is not really the way to go. Because it add unnecessary complexity to your code.

Option 2: Good way to do it if you only have backend rendering (for example with PHP).

Option 3: Do this only if you have static webpage (only HTML/CSS) which does not change. And you don't have programming experience at all.

Question 2: Well no you always have a index.html but you can redirect to the index.html of the default language.

marcramser
  • 579
  • 1
  • 10
  • 23