我一直在尝试在我的网站上设置多语言选项。 我的网站是基于引导的一页HTML。 我看了下面的代码
function ChangeLanguage(selected) {
alert(selected.value()); // Do whatever you want in here.
}
<div class="btn-group dropdown pull-right">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" >
<span class="lang-sm lang-lbl-full" lang="en"></span> <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" >
<li><span class="lang-sm lang-lbl-full" onclick="ChangeLanguage(this)" lang="ar" value="ar"></span></li>
<li><span class="lang-sm lang-lbl-full" onclick="ChangeLanguage(this)" lang="en" value="en"></span></li>
<li><span class="lang-sm lang-lbl-full" onclick="ChangeLanguage(this)" lang="fr" value="fr"></span></li>
</ul>
</div>
我不知道另一种语言的调用方式。
所以我的问题是如何调用正确的div,以便它可以工作。
如果有比使用多个div更好的方法,那么我将不知所措。
答案 0 :(得分:0)
实际上,多语言与Bootstrap无关,对于一页网站,您可以使用Javascript和CSS来实现。这是一个非常简单的示例:
类别名称为lang
的元素将由语言选择器(类别名称为ar
,en
,fr
进行过滤以定义语言)。
(function($) {
$('#lang-selector').on('change', function() {
$('body').attr('data-lang', $(this).val());
});
})(jQuery);
.lang {
display: none;
}
body[data-lang="ar"] .lang.ar {
display: initial;
}
body[data-lang="en"] .lang.en {
display: initial;
}
body[data-lang="fr"] .lang.fr {
display: initial;
}
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body data-lang="ar">
Select Language:
<select id="lang-selector">
<option value="ar">Arabic</option>
<option value="en">English</option>
<option value="fr">French</option>
</select>
<hr>
<h2 class="lang ar">Arabic</h2>
<h2 class="lang en">English</h2>
<h2 class="lang fr">French</h2>
<div class="content">
<p class="lang ar">
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa.
Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim.
</p>
<p class="lang en">
Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium.
Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim.
</p>
<p class="lang fr">
Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue.
Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem.
</p>
</div>
</body>