如何在 Clojure 中编写多语言应用程序?

发布于 2024-10-07 22:49:36 字数 65 浏览 5 评论 0原文

我正在尝试弄清楚如何创建一个基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦幻的味道 2024-10-14 22:49:36

最简单的方法是用函数调用替换所有本地化字符串,例如:

(i18n lang "help")

并实现该函数以从 .properties 文件读取本地化字符串由lang参数确定。

为此,您不需要任何库。这是一个简单的功能。

为了避免一直读取文件,您可以在应用程序期间在内存中读取它们,以 def 开始,将其放入名为 loaded-property-files 的映射中,其中 lang< /code> 是键,值是消息键和适当的本地化消息的映射。

可以这样完成:

(defn load-property-files [langs]
  (let [default (into {} (read-properties "locale.properties"))]
      (apply merge 
       (for [lang langs] 
        (assoc {} lang
         (merge default 
          (into {} (read-properties (str "locale_" lang ".properties")))))))))

(def loaded-property-files 
      (load-property-files ["en" "es" "de"]))

如果文件加载性能不是问题,但您希望能够在运行时更轻松地更改文件,只需将 def 更改为函数即可。

函数read-properties(最初来自旧的clojure.contrib)如下所示:

(defn read-properties
  "Read properties from file-able."
  ([fileable]
   (into {} (map #(vector (keyword (key %)) (val %))
    (try
      (with-open [f (java.io.FileInputStream. (new java.io.File fileable))]
        (doto (new java.util.Properties)
          (.load f)))
    (catch java.io.FileNotFoundException e {})))))
  ([fileable defaults] (merge (read-properties fileable) defaults)))

只要该键,就会使用default文件中的本地化字符串在指定的地图中找不到,即刚刚添加的新字符串,并且尚未有人将其翻译成西班牙语,将以默认 locale.properties 中的语言显示,

然后您的 i18n 函数如下所示:

(defn i18n [lang code]
  ((loaded-property-files lang) code))

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 by lang 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 named loaded-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:

(defn load-property-files [langs]
  (let [default (into {} (read-properties "locale.properties"))]
      (apply merge 
       (for [lang langs] 
        (assoc {} lang
         (merge default 
          (into {} (read-properties (str "locale_" lang ".properties")))))))))

(def loaded-property-files 
      (load-property-files ["en" "es" "de"]))

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 old clojure.contrib) looks like this:

(defn read-properties
  "Read properties from file-able."
  ([fileable]
   (into {} (map #(vector (keyword (key %)) (val %))
    (try
      (with-open [f (java.io.FileInputStream. (new java.io.File fileable))]
        (doto (new java.util.Properties)
          (.load f)))
    (catch java.io.FileNotFoundException e {})))))
  ([fileable defaults] (merge (read-properties fileable) defaults)))

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 default locale.properties

Then your i18n function looks like this:

(defn i18n [lang code]
  ((loaded-property-files lang) code))
∝单色的世界 2024-10-14 22:49:36

有一个新的 i18n 库: https://github.com/ptaoussanis/tower 其原理如下:

Tower 试图为 Clojure 呈现一个简单、惯用的国际化和本地化故事。它尽可能地包装了标准 Java 功能,但当有充分理由时,它也不怕脱离 Java 约定。

There is a new i18n library: https://github.com/ptaoussanis/tower with this rationale:

Tower is an attempt to present a simple, idiomatic internationalization and localization story for Clojure. It wraps standard Java functionality where possible, but it isn't afraid to step away from Java conventions when there's a good reason to.

因为看清所以看轻 2024-10-14 22:49:36

我为此创建了 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.

星星的軌跡 2024-10-14 22:49:36

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文