tinymce_如何动态定位TinyMCE

tinymce

This article will teach you how to dynamically localize your TinyMCE editor. If you aren't familiar with what TinyMCE is, it's a what-you-see-is-what-you-get rich text editor for the web, built in JavaScript.

本文将教您如何动态本地化TinyMCE编辑器。 如果您不熟悉TinyMCE是什么,它将是您所见即所得的,内置JavaScript的Web富文本编辑器。

问题 (The problem)

This tutorial assumes you have built a PHP multilingual site/framework and you, or whoever the admin is, would like TinyMCE to automatically switch to the current language of your site. A practical example would be: you've just switched your site's interface to Spanish, but you realize the interface of the editor is still in English and you would like it to be in Spanish instead.

本教程假定您已经构建了PHP多语言站点/框架,并且您(无论管理员是谁)都希望TinyMCE自动切换到站点的当前语言。 一个实际的例子是:您刚刚将站点的界面切换为西班牙语,但是您意识到编辑器的界面仍然是英语,而您希望它使用西班牙语。

Although TinyMCE is a great wysiwyg editor that provides localized files for almost every known language, it still does not come with an automatic language-switching feature. It's very easy to roll your own solution to this problem, though, so that's what we're going to do now.

尽管TinyMCE是一款出色的所见即所得编辑器,几乎可以为每种已知语言提供本地化文件,但它仍然没有自动语言切换功能。 但是,将自己的解决方案推出该问题非常容易,所以这就是我们现在要做的。

解决方案 (The solution)

First, grab the extra language files you need from here and upload them to your TinyMCE directory accordingly.

首先,从此处获取您需要的其他语言文件,并将它们相应地上传到TinyMCE目录。

Let's assume that on your site/framework, the variable that holds the current language is called $current_lang. You need to check that your $current_lang variable holds values that match TinyMCE's language codes. In simple terms, TinyMCE's code for Spanish is 'es' and if $current_lang returns 'sp' instead of 'es' when needed, it just won't work. So in this case you need to add an extra line or two:

假设在您的站点/框架上,保存当前语言的变量称为$current_lang 。 您需要检查$current_lang变量是否包含与TinyMCE的语言代码匹配的值。 简单来说,TinyMCE的西班牙语代码是'es',如果$current_lang在需要时返回'sp'而不是'es',它将无法正常工作。 因此,在这种情况下,您需要添加一两行:

<?php
...
if($current_lang == 'sp') {
    $current_lang = 'es';
} else {
    $current_lang = 'en'; //or whatever the default lang you wish
} 
...

You can look the codes up here and see if they correspond to what you already use on your site/framework.

您可以在此处查找代码,查看它们是否与您在站点/框架上已使用的代码相对应。

Now let's get to the header section of your site/framework where you have previously initialized TinyMCE. We are going to import our php $current_lang variable into our JS code, just above the place where you have initialized tinyMCE.js, as shown below:

现在,让我们转到您先前初始化TinyMCE的站点/框架的标题部分。 我们将把php $current_lang变量导入到我们的JS代码中,就在您初始化tinyMCE.js的位置的上方,如下所示:

<script type="text/javascript">

var cur_lang = "<?php echo $current_lang; ?>"; // do not forget the double quotes

tinyMCE.init({
...

Finally, we add a language parameter/value to our general options section, if that option wasn't there already.

最后,如果该选项尚不存在,则将其添加到常规选项部分。

<script type="text/javascript">

var cur_lang = "<?php echo $current_lang; ?>"; // do not forget the double quotes

tinyMCE.init({
        // General options
        width : "480",
        height : "680",
        mode : "textareas",
        theme : "advanced",
        language : cur_lang,  // Here we have added our language parameter, the value of which corresponds to our current language on the site.
        plugins : 
        ...

Note: Do not forget to modify the Javascript initialization snippet of your TinyMCE compressor too if you have installed one.

注意:如果已经安装了TinyMCE压缩程序, 不要忘记对其进行修改。

That's it, from now on, your TinyMCE editor's interface will appear in the language your site is currently in.

就是这样,从现在开始,您的TinyMCE编辑器的界面将以您网站当前使用的语言显示。

结论 (Conclusion)

TinyMCE is a fantastic tool, but people often jump through various inefficient hoops to make it support localization. In this tutorial, we've implemented one common way to do this in just a few short lines of code. Did you use another approach? Let us know!

TinyMCE是一个很棒的工具,但是人们经常跳过各种效率低下的圈,以使其支持本地化。 在本教程中,我们仅用几行代码就实现了一种通用的方法。 您是否使用其他方法? 让我们知道!

翻译自: https://www.sitepoint.com/dynamically-localize-tinymce/

tinymce

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值