如何在 Clojure 中编写多语言应用程序?
我正在尝试弄清楚如何创建一个基于 Compojure 并具有多语言支持的网站。有没有类似 i18n 之类的解决方案?
I'm trying to figure out how to create a Compojure-based web-site with multilingual support. Is there any solutions like i18n or something like that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最简单的方法是用函数调用替换所有本地化字符串,例如:
(i18n lang "help")
并实现该函数以从
.properties
文件读取本地化字符串由lang
参数确定。为此,您不需要任何库。这是一个简单的功能。
为了避免一直读取文件,您可以在应用程序期间在内存中读取它们,以
def
开始,将其放入名为loaded-property-files
的映射中,其中lang< /code> 是键,值是消息键和适当的本地化消息的映射。
可以这样完成:
如果文件加载性能不是问题,但您希望能够在运行时更轻松地更改文件,只需将
def
更改为函数即可。函数
read-properties
(最初来自旧的clojure.contrib
)如下所示:只要该键,就会使用
default
文件中的本地化字符串在指定的地图中找不到,即刚刚添加的新字符串,并且尚未有人将其翻译成西班牙语,将以默认locale.properties
中的语言显示,然后您的
i18n
函数如下所示:The easiest way is to replace all your localized strings with a function calls like:
(i18n lang "help")
And implement that function to read localized string from a
.properties
file determined bylang
parameter.For that you don't need any libraries. It's a simple function.
To avoid reading files all the time you could read them in memory during your applications start with a
def
into a map namedloaded-property-files
where,lang
is the key and the value is a map of message keys and appropriate localized messages.This can be done like this:
If file loading performance is not a problem, but you'd like to be able to change the files more easily during runtime, just change the
def
to a function.The function
read-properties
(originally from oldclojure.contrib
) looks like this:The localization string from
default
file would be used whenever that key isn't found in the specified map, i.e. new string that has just been added, and no one translated it in Spanish yet, would be shown in language from defaultlocale.properties
Then your
i18n
function looks like this:有一个新的 i18n 库: https://github.com/ptaoussanis/tower 其原理如下:
There is a new i18n library: https://github.com/ptaoussanis/tower with this rationale:
我为此创建了 clji18n ,但在完成之前我必须切换到其他项目。它“几乎”可用,你可以尝试一下。
I created clji18n for this, but I had to switch to other project before completing it. It's "almost" usable, you can give it a try.
kotarak 的 j18n (请注意,还有另一个用于 Java 的 j18n 库,但它们是不同的)似乎不错。
https://bitbucket.org/kotarak/j18n
kotarak's j18n (note that there is another j18n library for Java but they are different ones) seems good.
https://bitbucket.org/kotarak/j18n