简体   繁体   English

如何替换特定页面? [HTML多语种网站]

[英]How to substitute specific pages? [HTML Multilingual Website]

My question is pretty straight forward. 我的问题很简单。

I have a static HTML website in English. 我有一个英文的静态HTML网站。

www.website.com www.website.com

www.website.com/services www.website.com/services

www.website.com/contacts www.website.com/contacts

I also have it translated in German: 我也用德语翻译了它:

www.website.com/de/ www.website.com/de/

www.website.com/de/services www.website.com/de/services

www.website.com/de/contacts www.website.com/de/contacts

My button (flag) for changing language is located next to my navigation: 我用于更改语言的按钮(标志)位于我的导航旁边:

<ul class="language">
   <li class="de"><a href="#"></a></li>
</ul>

Option 1: I can just replace the "#" with the German version of the page. 选项1:我可以用“德语版”页面替换“#”。 For example on www.website.com it is <a href="www.website.com/de/"> and on www.website.com/services it is <a href="www.website.com/de/services"> 例如,在www.website.com上,它是<a href="www.website.com/de/"> ,在www.website.com/services上,它是<a href="www.website.com/de/services">

But this is so much work. 但这是太多的工作。 Is there an easier way for calling pages by using javascript or .htaccess..or whatever you suggest. 是否有一种更简单的方法来使用javascript或.htaccess ..或任何你的建议来调用页面。 My pages are in .html, so the .php option isn't efficient. 我的页面是.html,所以.php选项效率不高。 And adding "id" to every element in order to translate it.. is even more complicated than the first option. 并为每个元素添加“id”以便翻译它......比第一个选项更复杂。

Thanks in advance! 提前致谢!

Ideally, you should probably do that with the server-side language of your website (PHP / ASP / Java / …). 理想情况下,您应该使用网站的服务器端语言(PHP / ASP / Java / ...)来实现。 If you still want to do it in Javascript, you can do something like that to add /de on front of your current location: 如果您仍想在Javascript中执行此操作,则可以执行类似操作以在当前位置添加/de

<a href="www.website.com/de/" id="language">
<script>document.getElementById('language').setAttribute('href', '/de'+document.location.pathname);</script>

yep I suspect this javascript should work: 是的,我怀疑这个JavaScript应该工作:

 $(document).ready(() => { let path = window.location.pathname; if (path.startsWith('/de')) $('a.lang-switch').attr('href', path.substr(3)); else $('a.lang-switch').attr('href', '/de' + path); console.log($('a').attr('href')); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a class="lang-switch">Change Language</a> 

Do you have two distinct copies of the HTML? 你有两个不同的HTML副本吗? If so, this is probably pretty easy. 如果是这样,这可能很容易。

  1. Go with your first option, but only for changing languages. 选择第一个选项,但仅限于更改语言。
  2. Include a <meta> tag that indicates the current language 包含指示当前语言的<meta>标记
  3. Use a javascript event listener to intercept each link clicked and insert the language based on the above meta tag. 使用javascript事件侦听器拦截单击的每个链接,并根据上面的元标记插入语言。 Something like this; 像这样的东西;

 let lang = document.getElementsByName('lang')[0].getAttribute('lang'); // Get the parent DIV, add click listener... document.body.addEventListener("click", function(e) { // e.target was the clicked element if(e.target && e.target.nodeName == "A") { e.target.href.replace('yourwebsite.com', `yourwebsite.com/${lang}/`); } }); 
 <meta name='lang' lang='de'> 

References: Part of code taken from an answer here 参考文献: 部分代码取自此处的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM