### Tongue Datetime Formatting Options Source: https://github.com/tonsky/tongue/blob/master/README.md Provides a comprehensive reference for all available formatting codes that can be used within `inst-formatter` strings. Each code is explained with an example and its corresponding meaning in date and time representation. ```APIDOC Full list of formatting options: | Code | Example | Meaning | | -------------------- | -------------- | -------------------- | | `{hour24-padded}` | 00, 09, 12, 23 | Hour of day (00-23), 0-padded | | `{hour24}` | 0, 9, 12, 23 | Hour of day (0-23) | | `{hour12-padded}` | 12, 09, 12, 11 | Hour of day (01-12), 0-padded | | `{hour12}` | 12, 9, 12, 11 | Hour of day (1-12) | | `{dayperiod}` | AM, PM | AM/PM from `:dayperiods` | | `{minutes-padded}` | 00, 30, 59 | Minutes (00-59), 0-padded | | `{minutes}` | 0, 30, 59 | Minutes (0-59) | | `{seconds-padded}` | 0, 30, 59 | Seconds (00-60), 0-padded | | `{seconds}` | 00, 30, 59 | Seconds (0-60) | | `{milliseconds}` | 000, 123, 999 | Milliseconds (000-999), always 0-padded | | `{weekday-long}` | Wednesday | Weekday from `:weekdays-long` | | `{weekday-short}` | Wed, Thu | Weekday from `:weekdays-short` | | `{weekday-narrow}` | W, T | Weekday from `:weekdays-narrow` | | `{weekday-numeric}` | 1, 4, 5, 7 | Weekday number (1-7, Sunday = 1) | | `{day-padded}` | 01, 15, 29 | Day of month (01-31), 0-padded | | `{day}` | 1, 15, 29 | Day of month (1-31) | | `{month-long}` | January | Month from `:months-long` | | `{month-short}` | Jan, Feb | Month from `:months-short` | | `{month-narrow}` | J, F | Month from `:months-narrow` | | `{month-numeric-padded}` | 01, 02, 12 | Month number (01-12, January = 01), 0-padded | | `{month-numeric}` | 1, 2, 12 | Month number (1-12, January = 1) | | `{year}` | 1999, 2016 | Full year (0-9999) | ``` -------------------------------- ### Using Tongue Translations Source: https://github.com/tonsky/tongue/blob/master/README.md Illustrates various ways to use the `translate` function built by Tongue. This includes simple key lookups, namespaced keys, substitutions with strings and maps, using arbitrary functions for dynamic translations, and handling locale fallbacks and missing keys. ```clojure (translate :en :color) ;; => "Color" ;; namespaced keys (translate :en :animals/dog) ;; => "Dog", taken from { :en { :animals { :dog "Dog }}} ;; substitutions (translate :en :welcome "Nikita") ;; => "Hello, Nikita!" (translate :en :between 0 100) ;; => "Value must be between 0 and 100" (translate :en :mail-title {:user "Tom" :title "New message"}) ;; => "Tom, New message - Message received." ;; if key resolves to fn, it will be called with provided arguments (translate :en :count 0) ;; => "No items" (translate :en :count 1) ;; => "1 item" (translate :en :count 2) ;; => "2 items" ;; multi-tag locales will fall back to more generic versions ;; :zh-Hans-CN will look in :zh-Hans-CN first, then :zh-Hans, then :zh, then fallback locale (translate :en-GB :color) ;; => "Colour", taken from :en-GB (translate :en-GB :flower) ;; => "Flower", taken from :en ;; if there’s no locale or no key in locale, fallback locale is used (translate :ru :color) ;; => "Color", taken from :en as a fallback locale ;; if nothing can be found at all (translate :en :unknown) ;; => "|Missing key :unknown|" ``` -------------------------------- ### Format Instants with Timezone Source: https://github.com/tonsky/tongue/blob/master/README.md Demonstrates formatting a Clojure instant using a pre-built formatter. It shows how to format an instant with the default UTC timezone and how to specify a particular timezone for accurate representation. ```clojure (format-inst #inst "2016-07-11T22:31:00+06:00") ;; => "Jul 11, 2016 at 4:31 PM" (format-inst #inst "2016-07-11T22:31:00+06:00" (java.util.TimeZone/getTimeZone "Asia/Novosibirsk")) ;; => "Jul 11, 2016 at 10:31 PM" ``` -------------------------------- ### Create and Use English Number Formatter Source: https://github.com/tonsky/tongue/blob/master/README.md Demonstrates how to create a number formatter for English locale with specific grouping and decimal separators, and then use it to format a number. ```clojure (def format-number-en ;; [number] => string (tongue/number-formatter { :group "," :decimal "." })) (format-number-en 9999.9) ;; => "9,999.9" ``` -------------------------------- ### Basic String Interpolation Source: https://github.com/tonsky/tongue/blob/master/README.md Demonstrates how to use Tongue for basic string interpolation with positional and named arguments. It shows how to define dictionaries and use the `build-translate` function to create a translation function. ```clojure (require '[tongue.core :as tongue]) (def dicts { :en { :welcome "Hello, {1}!" :mail-title "{user}, {title} - Message received." }}) (def tr (tongue/build-translate dicts)) (tr :en :welcome "Nikita") ;; => "Hello, Nikita!" (tr :en :mail-title {:user "Tom" :title "New message"}) ;; => "Tom, New message - Message received." ``` -------------------------------- ### Integrate Datetime Formatting into Translation Dictionaries Source: https://github.com/tonsky/tongue/blob/master/README.md Shows how to include a default datetime formatter within a translation dictionary using the `:tongue/format-inst` key. This allows substituted instants in translated strings to be automatically formatted. ```clojure (def dicts { :en { :tongue/format-inst (tongue/inst-formatter "{month-short} {day}, {year}" inst-strings-en) :published "Published at {1}" } }) (def translate (tongue/build-translate dicts)) ;; if locale has :tongue/format-inst key, substituted instants will be formatted using it (translate :en :published #inst "2016-01-01") ;; => "Published at January 1, 2016" ``` -------------------------------- ### Build a Datetime Formatter Source: https://github.com/tonsky/tongue/blob/master/README.md Creates a reusable function for formatting Clojure instants into human-readable date and time strings. It takes a format string and a map of locale strings as arguments. The resulting formatter can accept an instant or an instant and a timezone. ```clojure (def format-inst ;; [inst] | [inst tz] => string (tongue/inst-formatter "{month-short} {day}, {year} at {hour12}:{minutes-padded} {dayperiod}" inst-strings-en)) ``` -------------------------------- ### Multiple Datetime Formatters in Dictionary Source: https://github.com/tonsky/tongue/blob/master/README.md Illustrates how to define multiple distinct datetime formatters within a translation dictionary, each associated with a different key. This enables flexible formatting options for various localization needs. ```clojure (def dicts { :en { :date-full (tongue/inst-formatter "{month-long} {day}, {year}" inst-strings-en) :date-short (tongue/inst-formatter "{month-numeric}/{day}/{year-2digit}" inst-strings-en) :time-military (tongue/inst-formatter "{hour24-padded}{minutes-padded}")}}) (def translate (tongue/build-translate dicts)) (translate :en :date-full #inst "2016-01-01T15:00:00") ;; => "January 1, 2016" (translate :en :date-short #inst "2016-01-01T15:00:00") ;; => "1/1/16" (translate :en :time-military #inst "2016-01-01T15:00:00") ;; => "1500" ;; You can use timezones too (def tz (java.util.TimeZone/getTimeZone "Asia/Novosibirsk")) ;; GMT+6 (translate :en :time-military #inst "2016-01-01T15:00:00" tz) ;; => "2100" ``` -------------------------------- ### Integrate Number Formatting into Locale Dictionaries Source: https://github.com/tonsky/tongue/blob/master/README.md Shows how to define locale dictionaries for English and Russian, incorporating custom number formatters using the `:tongue/format-number` key. It also demonstrates how to build a translator and use it to format numbers within translated strings. ```clojure (def dicts { :en { :tongue/format-number format-number-en :count "{1} items" } :ru { :tongue/format-number (tongue/number-formatter { :group " " :decimal "," }) :count "{1} штук" }}) (def translate (tongue/build-translate dicts)) ;; if locale has :tongue/format-number key, substituted numbers will be formatted (translate :en :count 9999.9) ;; => "9,999.9 items" (translate :ru :count 9999.9) ;; => "9 999,9 штук" ;; hint: if you only need a number, use :tongue/format-number key directly (translate :en :tongue/format-number 9999.9) ;; => "9,999.9" ``` -------------------------------- ### Define Dictionaries in Tongue Source: https://github.com/tonsky/tongue/blob/master/README.md Demonstrates how to define translation dictionaries using Clojure maps for different locales. Supports simple keys, namespaced keys, nested maps, substitutions, aliases, arbitrary functions for translations, and custom missing key messages. ```clojure (require '[tongue.core :as tongue]) (def dicts { :en { ;; simple keys :color "Color" :flower "Flower" ;; namespaced keys :weather/rain "Rain" :weather/clouds "Clouds" ;; nested maps will be unpacked into namespaced keys ;; this is purely for ease of dictionary writing :animals { :dog "Dog" ;; => :animals/dog :cat "Cat" } ;; => :animals/cat ;; substitutions :welcome "Hello, {1}!" :between "Value must be between {1} and {2}" ;; For using a map :mail-title "{user}, {title} - Message received." ;; aliases, to share common strings but still use specific i18n keys :frontpage-greeting :welcome ;; arbitrary functions :count (fn [x] (cond (zero? x) "No items" (= 1 x) "1 item" :else "{1} items")) ;; you can return string with substitutions ;; optional -- override “Missing key” message :tongue/missing-key "Missing key {1}" } :en-GB { :color "colour" } ;; sublang overrides :tongue/fallback :en } ;; fallback locale key ) ``` -------------------------------- ### Build Translation Function in Tongue Source: https://github.com/tonsky/tongue/blob/master/README.md Shows how to build a translation function using the `build-translate` function from the Tongue library, taking the defined dictionaries as input. This function is then used to perform translations. ```clojure (def translate ;; [locale key & args] => string (tongue/build-translate dicts)) ``` -------------------------------- ### Define Locale Strings for Date Localization Source: https://github.com/tonsky/tongue/blob/master/README.md Defines a map of locale-specific strings for various date and time components like weekdays, months, and day periods. These are used by `inst-formatter` to create localized date strings. ```clojure (def inst-strings-en { :weekdays-narrow ["S" "M" "T" "W" "T" "F" "S"] :weekdays-short ["Sun" "Mon" "Tue" "Wed" "Thu" "Fri" "Sat"] :weekdays-long ["Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"] :months-narrow ["J" "F" "M" "A" "M" "J" "J" "A" "S" "O" "N" "D"] :months-short ["Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"] :months-long ["January" "February" "March" "April" "May" "June" "July" "August" "September" "October" "November" "December"] :dayperiods ["AM" "PM"] :eras-short ["BC" "AD"] :eras-long ["Before Christ" "Anno Domini"] }) ``` -------------------------------- ### Custom Interpolation with Vectors Source: https://github.com/tonsky/tongue/blob/master/README.md Illustrates how to extend Tongue to handle custom data types for interpolation, specifically using Clojure vectors. This involves implementing the `tongue.core/IInterpolate` interface to define how vectors are processed for named and positional interpolations. ```clojure (require '[tongue.core :as tongue]) (extend-type clojure.lang.PersistentVector tongue/IInterpolate (interpolate-named [v dicts locale interpolations] (mapv (fn [x] (if (and (keyword? x) (= "arg" (namespace x))) (get interpolations x) x)) v)) (interpolate-positional [v dicts locale interpolations] (mapv (fn [x] (if (and (vector? x) (= :arg (first x))) (nth interpolations (second x)) x)) v))) (def dicts { :en { :welcome [:div {} "Hello, " [:arg 0]] :mail-title [:arg/user ", Message received."] }}) (def tr (tongue/build-translate dicts)) (tr :en :welcome "Nikita") ;; => [:div {} "Hello, " "Nikita"] (tr :en :mail-title {:arg/user "Tom"}) ;; => ["Tom" ", Message received."] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.