Comments (4)
After thinking this through I think going with a proper parsing of BCP 47 tags (probably still supporting underscore for backwards compatibility?) is the only reasonable option. So I'll start to prepare a patch for that.
from tower.
Hi there,
I'm afraid I'm not really following. To clarify: Tower has two notions of "locale"-
- A JVM locale used for the JVM-dependent localization features
- A keyword locale used for translations
The second is a strict superset of the first. No JVM validation or structural requirements are imposed on the second type.
So you can use any kind of locale structure you like for translations: :sr-Latn-RS
and :zh-Hans
should work fine.
You can't use these for anything that requires a JVM locale (since the JVM wouldn't be able to recognize these); but you're free to use them for the translations API (which is pure Clojure/Script).
The only semantic requirement is that an :x-y-z
locale can sensibly fallback to :x-y
and then :x
.
Does that make sense?
from tower.
Hi,
Sorry for not being clear enough in my description, I'll try to be short and describe my use case better.
I have a system where I have a valid IETF BCP 47 locale in my database for each user. I want to use this locale for formatting both translations and JVM-dependent localization features. If we take zh-Hans-CN
as an example, I can use it just fine for translations, but if I try to use it for localization features the following happens:
=> (tower/fmt-str :zh-Hans-CN "%f" 5.5)
ExceptionInfo Invalid locale: :zh-Hans-CN clojure.core/ex-info (core.clj:4403)
This is not nice, because zh-Hans-CN
is a perfectly valid locale according to best current practices. (hence BCP)
What I would really like to have as a first step would be something like the following:
diff --git a/src/taoensso/tower.cljx b/src/taoensso/tower.cljx
index 2abb09d..933decd 100644
--- a/src/taoensso/tower.cljx
+++ b/src/taoensso/tower.cljx
@@ -7,7 +7,7 @@
[taoensso.encore :as encore]
[taoensso.timbre :as timbre]
[taoensso.tower.utils :as utils :refer (defmem- defmem-*)])
- #+clj (:import [java.util Date Locale TimeZone Formatter]
+ #+clj (:import [java.util Date Locale Locale$Builder TimeZone Formatter IllformedLocaleException]
[java.text Collator NumberFormat DateFormat])
#+cljs (:require-macros [taoensso.encore :as encore]
[taoensso.tower :as tower-macros])
@@ -67,11 +67,16 @@
(make-Locale (.getLanguage ^Locale loc)))
:else
- (let [loc-parts (str/split (name loc) #"[-_]")]
- (all-Locales
- (if-not lang-only?
- (apply make-Locale loc-parts)
- (make-Locale (first loc-parts))))))))
+ (try
+ (let [loc-obj (.build (.setLanguageTag (Locale$Builder.) (name loc)))]
+ (if-not lang-only? loc-obj
+ (make-Locale (.getLanguage ^Locale loc-obj))))
+ (catch IllformedLocaleException e
+ (let [loc-parts (str/split (name loc) #"[_]")]
+ (all-Locales
+ (if-not lang-only?
+ (apply make-Locale loc-parts)
+ (make-Locale (first loc-parts))))))))))
#+clj
(def jvm-locale
This would support all BCP 47 locales and try to fall back to the old legacy way of handling locales. Notice that I removed -
from the regex because I think it would be much nicer to have all locale names separated by dash to be well formed BCP 47 names.
What would be nice in the long run would be something like what .NET does as explained in https://msdn.microsoft.com/en-us/library/vstudio/dd997383(v=vs.100).aspx
The parent chain of the Chinese cultures now includes the root Chinese culture. The following examples show the complete parent chain for two of the Chinese specific cultures:
zh-CN → zh-CHS → zh-Hans → zh → Invariant
zh-TW → zh-CHT → zh-Hant → zh → Invariant
So the system should know that simplified Chinese is used in mainland China and fall back to zh-Hans if zh-CN is used. But it seems that the JVM doesn't support this either and leaves it to the implementation to handle correct mappings, so I think it's out of scope of tower.
Did this clarify my point of script support any better?
from tower.
Hi,
Did this clarify my point of script support any better?
It did, thank you for the detailed info! Not ready to reply just yet, need some time to go over this all.
Just leaving a reference here in the meantime: http://openjdk.java.net/jeps/128
from tower.
Related Issues (20)
- make-t fails for a dictionary containing language tag :id HOT 6
- What is correct way to specify deeply nested translation path? HOT 10
- Compilation of tower.cljs fails with ClojureScript 0.0-2913 HOT 16
- Temp: testing markdown
- AssertionError from make-t when map with :missing is loaded from external resource HOT 3
- Maximum text size (hint for translator) and entity property texts HOT 3
- Invalid locale exception when passing arguments to translation in sw locale HOT 6
- Tower 3.1.0-beta3 with ClojureScript: IllegalArgumentException with tower/dict-compile* HOT 6
- timbre/logp not found HOT 4
- Best way to make dynamic paths HOT 2
- Wrong Swedish currency output HOT 2
- using figwheel in translation HOT 3
- Dependency problem HOT 2
- Formatting time and timezone
- Warn about `dev-mode?` in readme and changelog? HOT 1
- encore.cljs: contains? not supported on type: clojure.lang.PersistentList HOT 2
- Assert failed: Invalid tconfig key: :dictionary HOT 1
- Customizable markdown
- Invalid locale exception with numeric region code
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tower.