调整网页以支持外语
我创建了一组 13 个 HTML/CSS/JS 模板,用于英语语言的 CMS 集成。
我需要包括对外语的支持,特别是俄语、中文和阿拉伯语。最初的在线搜索尚未找到任何中央资源来指导 HTML 中支持不同语言所需的内容。
我知道我需要查看字体堆栈和字符编码等内容,并且阿拉伯语模板将需要我的整个布局切换的特殊支持,以实现从右到左的阅读风格。
任何人都可以向我指出一些可靠的资源,以便以符合标准的方式执行此操作吗?所有模板必须满足 WCAG 2.0 AA 要求。
I have created a set of 13 HTML/CSS/JS templates for CMS integration in the English language.
I need to include support for foreign languages, specifically Russian, Chinese and Arabic. Initial searches online haven't turned up any central resource for guidance on what is required for supporting different languages in HTML.
I understand I'll need to look at things like my font-stacks and character encoding and the Arabic templates will need particular support with my entire layout switching for the right-to-left reading style.
Can anyone point me to some reliable resources for doing this in a standards-compliant way? All templates must meet WCAG 2.0 AA requirements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
步骤 1
首先,假设我们有以下 HTML:
此 DIV 层包含我们的标题。现在,我们决定希望该游戏有多种语言版本。我们的第一步是向 div 添加一个类,以便我们稍后可以识别它:
步骤 2
准备就绪后,我们就只剩下两步了。首先,我们将创建一个包含翻译的 XML 文件。在此 XML 文件中,我们可以存储多个短语的翻译,并且可以在稍后阶段轻松添加更多语言。我们将此文件另存为 languages.xml 并将其保存在与 HTML 文件相同的文件夹中。
您将在 ; 代替。
标记之间存储要翻译的所有短语。您将每个短语存储在
标记中。为了识别正在翻译的短语,我们需要添加id=”title”
。该名称应与您在 HTML 中分配的 CSS 类的名称相匹配。最后,我们可以将翻译放入其中并用定义语言的标签将其包围。例如,我们需要将意大利语文本放在
标签之间。请记住,您可以轻松更改这些标签的名称 - 例如,您可以选择使用
和步骤 3
完成后,您只需添加 jQuery 来读取 XML 文件并根据所选语言替换 DIV 的内容。给你:
这就是所需的所有代码 - 这次再看一遍并添加注释:
就是这样!刷新您的页面,应该会加载意大利语版本,替换默认的英语版本。在上面的示例中,我们手动设置语言:
我们可以通过 PHP 轻松设置:
或者通过从 URL 读取它 – 您可以使用 这个 jQuery 插件 可以做到这一点。
额外技巧
当您添加更多语言时,您会发现短语在某些语言中较长,而在其他语言中较短。我们可能希望为每种语言提供自定义 CSS。以上面的示例为例,我们最初会遇到以下情况:
如果我们希望意大利语有较小的字体怎么办?简单的!我们需要对 jQuery 进行轻微修改:
现在我们已经添加了该行,我们只需添加以下 CSS:
您的意大利语文本现在应该更小。 注意:为了使其正常工作,您必须将新语言 CSS 定义放在默认语言 CSS 定义之下。交换这两条线是行不通的。
Step 1
To start, let’s assume we have the following HTML:
This DIV layer contains our title. Now, we have decided we want this title to be available in multiple languages. Our first step is adding a class to the div so we can identify it later on:
Step 2
With that ready, we’re just two steps away. First off, we are going to create an XML file that includes our translations. In this XML file, we can store translations for multiple phrases and we can easily add more languages at a later stage. We shall save this file as languages.xml and save it in the same folder as our HTML file.
You will store all phrases you want to translate between the
<translations></translations>
tags. You will store each phrase in a<translation></translation>
tag. In order to identify which phrase is being translated, we need to add theid=”title”
. The name should match the name of the CSS class you assigned in the HTML. Finally, we can put the translations inside and surround them by tags defining the language. For instance, we need to put the Italian text in between<italian></italian>
tags. Keep in mind that you can easily change the names of these tags – For example, you may choose to use<eng></eng>
and<ita></ita>
instead.Step 3
With that complete, you just need to add jQuery to read the XML file and replace the contents of your DIVs based on the language selected. Here you go:
That’s all the code needed – Have a look at it again with comments this time:
And that’s it! Refresh your page and the Italian version should load, replacing the default English one. In the example above, we set the language manually:
We could just as easily set that via PHP:
Or by reading it from the URL – you can use this jQuery Plugin to do that.
Bonus Trick
As you add more languages, you will realize that phrases are longer in certain languages and shorter in others. We might want to have custom CSS for each language. Taking the example above, we would initially have the following:
What if we wanted the Italian to have a smaller font? Easy! We need to make a slight modification to our jQuery:
Now that we’ve added that line, we can just add the following CSS:
Your Italian text should now be smaller. Note: In order for this to work, you must put the new language CSS definitions underneath the default one. Switching those two lines around will not work.
很抱歉这么晚才回答,但迟到总比不好......:-)
弗朗索瓦的回答是一个简单而快速的解决方案的好解决方案。
如需更完整、更灵活的解决方案(例如,复数形式处理...),请查看:i18next。
它们提供:
我自己使用 i18next 解决方案,尽管我个人更喜欢服务器端解决方案以避免客户端的任何额外负担...:-)
NB 我与i18next.com...:-)
Sorry for the late answer, but better late than never... :-)
Francois answer is a good solution for a simple and quick solution.
For a more complete and flexible solution (with plural forms handling, for example...), please have a look at: i18next.
They provide:
I'm using i18next solution myself, though I would personally prefer a server-side solution to avoid any additional burden on client side... :-)
N.B. I have no relation at all with i18next.com... :-)