Skip to content

Commit 395432a

Browse files
authored
feat: separe linguify into linguify and linguify-raw (#32)
* feat: separe linguify into linguify and linguify-raw * fix: formatting * fix: add arguments to the linguify functions and fix tests accordingly * formatting: remove ) and fix fluent tests formatting --------- Co-authored-by: ImTrisha <imtrisha@randomdevs.org>
1 parent 2c70ae7 commit 395432a

File tree

8 files changed

+58
-41
lines changed

8 files changed

+58
-41
lines changed

manual.pdf

3.92 KB
Binary file not shown.

src/lib.typ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
#import "linguify.typ" as lflib: set-database, reset-database, database-at, linguify
1+
#import "linguify.typ" as lflib: database-at, linguify, linguify-raw, reset-database, set-database
22
#import "fluent.typ": load-ftl-data
33
#import "utils.typ" as utils: if-auto-then

src/linguify.typ

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -182,22 +182,20 @@
182182

183183
// if the key is not found in the fallback language
184184

185-
error-message = error-message + " Also, the fallback language `" + fallback-lang + "` does not contain the key `" + key + "`."
186-
185+
error-message += " Also, the fallback language `" + fallback-lang + "` does not contain the key `" + key + "`."
187186
} else {
188187
// if no fallback language is set
189-
error-message = error-message + " Also, no fallback language is set."
188+
error-message += " Also, no fallback language is set."
190189
}
191190

192191
return error(error-message)
193192
}
194193

195-
196194
/// fetch a string in the required language.
197-
/// provides context for `_linguify` function which implements the logic part.
195+
/// must have a context beforehand to access the global database/lang
198196
///
199197
/// -> content
200-
#let linguify(
198+
#let linguify-raw(
201199
/// The key at which to retrieve the item.
202200
/// -> string
203201
key,
@@ -212,19 +210,32 @@
212210
default: auto,
213211
args: auto,
214212
) = {
215-
let impl() = {
216-
let result = _linguify(key, from: from, lang: lang, args: args)
217-
if is-ok(result) {
218-
result.ok
219-
} else {
220-
if-auto-then(default, () => panic(result.error))
221-
}
222-
}
223-
224-
if from == auto or lang == auto {
225-
// context is needed to use the default database or current language
226-
context impl()
213+
let result = _linguify(key, from: from, lang: lang, args: args)
214+
if is-ok(result) {
215+
result.ok
227216
} else {
228-
impl()
217+
if-auto-then(default, () => panic(result.error))
229218
}
230219
}
220+
221+
/// fetch a string in the required language.
222+
/// provides context for `linguify-raw` function.
223+
///
224+
/// -> content
225+
#let linguify(
226+
/// The key at which to retrieve the item.
227+
/// -> string
228+
key,
229+
/// database to fetch the item from. If auto linguify's global database will used.
230+
/// -> dictionary
231+
from: auto,
232+
/// the language to look for, if auto use `context text.lang` (default)
233+
/// -> string
234+
lang: auto,
235+
/// A default value to return if the key is not part of the database.
236+
/// -> any
237+
default: auto,
238+
args: auto,
239+
) = {
240+
context linguify-raw(key, from: from, lang: lang, default: default, args: args)
241+
}

tests/context-free/test.typ

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44

55
#set-database(data)
66

7-
// Database and language are known: string
8-
#assert.eq(type(linguify("apple", from: data, lang: "en")), str)
7+
// Database and language are known, so we dont have to provide the context
8+
#assert.eq(type(linguify-raw("apple", from: data, lang: "en")), str)
99

10-
// Only database known: content (context)
11-
#assert.eq(type(linguify("apple", from: data)), content)
10+
// Only database known, so we have to provide context
11+
#context assert.eq(type(linguify-raw("apple", from: data)), str)
1212

13-
// Only language known: content (context)
14-
#assert.eq(type(linguify("apple", lang: "en")), content)
13+
// Only language known, so we have to provide context
14+
#context assert.eq(type(linguify-raw("apple", lang: "en")), str)
1515

16-
// Neither known: content (context)
17-
#assert.eq(type(linguify("apple")), content)
16+
// Assert that without context and without any of `lang` or `from`, the function panics.
17+
18+
#assert-panic(() => linguify-raw("apple"))
19+
20+
#assert-panic(() => linguify-raw("apple", from: data))
21+
22+
#assert-panic(() => linguify-raw("apple", lang: "en"))

tests/fluent/test.typ

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// #include "fluent-test/test.typ"
2+
23
#import "/src/lib.typ": *
34

45
#let de = smallcaps("DE:")

tests/outline-location/ref.typ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
#outline()
77

88
= red
9-
green
9+
green

tests/outline-location/test.typ

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
// This key is NOT found in the header!
2121
= #linguify("red")
2222
// This key IS found in regular text
23-
#linguify("green")
23+
#linguify("green")

tests/unit1/test.typ

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#let test-data = {
66
assert(db != none)
7-
assert(db.at("lang", default: none) != none )
7+
assert(db.at("lang", default: none) != none)
88

99
set-database(db)
1010
reset-database()
@@ -46,15 +46,15 @@
4646
set text(lang: "en")
4747
context {
4848
assert(_linguify("apple", from: db) == ok("Apple"))
49-
assert(_linguify("pear", from: db)== ok("Pear"))
50-
assert(_linguify("banana", from: db) == ok("Banana" ))
49+
assert(_linguify("pear", from: db) == ok("Pear"))
50+
assert(_linguify("banana", from: db) == ok("Banana"))
5151

5252
assert(_linguify("red", from: db) == ok("red"))
5353
assert(_linguify("green", from: db) == ok("green"))
5454
assert(_linguify("yellow", from: db) == ok("yellow"))
5555

5656
assert(is-error(_linguify("test", from: db)))
57-
}
57+
}
5858

5959
// German (de)
6060
set text(lang: "de")
@@ -91,26 +91,26 @@
9191

9292
#let test-_linguify-auto-db = {
9393
set-database(db)
94-
// English (en)
94+
// English (en)
9595
set text(lang: "en")
9696
context {
9797
assert(_linguify("apple") == ok("Apple"))
98-
assert(_linguify("pear")== ok("Pear"))
98+
assert(_linguify("pear") == ok("Pear"))
9999
assert(_linguify("banana") == ok("Banana"))
100100

101101
assert(_linguify("red") == ok("red"))
102102
assert(_linguify("green") == ok("green"))
103103
assert(_linguify("yellow") == ok("yellow"))
104104

105105
assert(is-error(_linguify("test")))
106-
}
106+
}
107107

108108
// German (de)
109109
set text(lang: "de")
110110
context {
111111
assert(_linguify("apple") == ok("Apfel"))
112112
assert(_linguify("pear") == ok("Birne"))
113-
assert(_linguify("banana") == ok("Banane") )
113+
assert(_linguify("banana") == ok("Banane"))
114114

115115
// keys not inside db - will fallback to en
116116
assert(_linguify("red") == ok("red"))
@@ -125,7 +125,7 @@
125125
context {
126126
assert(_linguify("apple") == ok("Apple"))
127127
assert(_linguify("pear") == ok("Pear"))
128-
assert(_linguify("banana") == ok("Banana") )
128+
assert(_linguify("banana") == ok("Banana"))
129129

130130
assert(_linguify("red") == ok("red"))
131131
assert(_linguify("green") == ok("green"))
@@ -139,7 +139,7 @@
139139

140140
#let test-args-in-dict-mode = {
141141
context {
142-
assert(_linguify("apple") == ok("Apple"))
142+
assert(_linguify("apple") == ok("Apple"))
143143
assert(is-error(_linguify("apple", args: (name: "test"))))
144144
assert(is-error(_linguify("apple", args: none)))
145145
assert(is-error(_linguify("apple", args: (:))))
@@ -151,7 +151,7 @@
151151
}
152152

153153
#let test-linguify-default = {
154-
assert.eq(linguify("test", from: db, lang: "en", default: "x"), "x")
154+
assert.eq(linguify-raw("test", from: db, lang: "en", default: "x"), "x")
155155

156156
[run `test-linguify-default` successfully]
157157
}

0 commit comments

Comments
 (0)