This repository has been archived by the owner on Mar 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathl10n.rkt
73 lines (59 loc) · 2.21 KB
/
l10n.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#lang racket/base
; Loads human-readable strings dynamically
(provide get-message-formatter
get-localized-string
run+print-subprogram)
(require racket/list
racket/runtime-path
racket/sequence
"codec.rkt"
"format.rkt"
"subprogram.rkt"
"printer.rkt")
(define-runtime-path here "l10n")
(define (run+print-subprogram subprogram-inst)
(parameterize ([current-message-formatter (get-message-formatter)])
(define-values (result messages) (run-subprogram subprogram-inst))
(write-message-log messages (current-message-formatter))
result))
(define (dynamic-require/localized key)
(let ([on-failure (americentric-fallback key)])
(with-handlers ([exn:fail:filesystem? on-failure])
(dynamic-require (get-module-path (system-language+country))
key
on-failure))))
(define (get-message-formatter)
(define f
(combine-message-formatters (dynamic-require/localized 'format-message/locale)
default-message-formatter))
(λ (m)
(parameterize ([current-message-formatter f])
(f m))))
(define (get-localized-string-lookup)
(dynamic-require/localized 'get-string))
(define (get-localized-string sym)
((get-localized-string-lookup) sym))
(define (get-module-path locale)
(path-replace-extension
(build-path here
(string-downcase
(coerce-string
(regexp-replace
#rx"_"
(regexp-replace #px"\\..+" locale "")
"-"))))
#".rkt"))
(define (americentric-fallback sym)
(λ _ (dynamic-require (get-module-path "en-us") sym)))
(module+ test
(require rackunit
"message.rkt")
(test-not-exn "Americentric fallback is always available"
(λ ()
(check-pred procedure? (americentric-fallback 'format-message/locale))
(check-pred procedure? (americentric-fallback 'get-string))))
(test-case "Can get formatter on current system"
(define formatter (get-message-formatter))
(check-pred procedure? formatter)
(check-eq? (procedure-arity formatter) 1)
(check-equal? (formatter ($show-string "a")) "a")))