Skip to content

Commit

Permalink
Merge branch 'typst:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
pauladam94 authored Jan 31, 2024
2 parents 8627264 + 9c9abbe commit 89fbbef
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/preview/linguify/0.1.0/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 jomaway

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
36 changes: 36 additions & 0 deletions packages/preview/linguify/0.1.0/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# [Typst-linguify](https://github.com/jomaway/typst-linguify)

Load strings for different languages easily. This can be useful if you create a package or template for multilingual usage. See the [gentle-clues package](https://github.com/jomaway/typst-gentle-clues) as an example.

## Usage

```typst
#import "@local/linguify:0.1.0": *
#let lang_data = toml("lang.toml")
#show: linguify_config.with(data: lang_data, lang: "en");
#linguify("abstract") // Shows Abstract in the document.
```

The `lang.toml` must look like this:

```toml
default-lang = "en"

[en]
title = "A simple linguify example"
abstract = "Abstract"

[de]
title = "Ein einfaches Linguify Beispiel"
abstract = "Zusammenfassung"
```

## Features

- Use a `toml` or other file to load strings for different languages. You need to pass a typst dictionary whichs follows the structure of the shown toml file.
- Specify a **default-lang**. If none is specified it will default to `en`
- **Fallback** to the default-lang if a key is not found for a certain language.
41 changes: 41 additions & 0 deletions packages/preview/linguify/0.1.0/linguify.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// linguify

#let __lang_setting = state("lang-setting", "en");
#let __lang_dict = state("lang-dict", (default-lang: "en"));
#let __lang_fallback = state("lang-fallback", true);


#let linguify_config(data: auto, lang: "en", fallback: true, content) = {
assert.eq(type(lang), str, message: "The lang parameter needs to be a string")
__lang_setting.update(lang);
if data != auto {
assert.eq(type(data), dictionary, message: "The data parameter needs to be of type dict.")
__lang_dict.update(data);
}
assert.eq(type(fallback), bool, message: "fallback can only be [true] or [false]")
__lang_fallback.update(fallback)
content
}

#let _get_default_lang(data) = {
let default_lang = data.at("default-lang", default: "en")
assert(default_lang in data, message: "No entry for the `default-lang` (" + default_lang + ") could be found in the lang data.")
default_lang
}

#let linguify(key) = {
locate(loc => {
// get current state.
let selected_lang = __lang_setting.at(loc)
let data = __lang_dict.at(loc)
let fallback = __lang_fallback.at(loc)
// process.
if (fallback) {
let default_lang = _get_default_lang(data);
if (not selected_lang in data) {selected_lang = default_lang}
return data.at(selected_lang).at(key, default: data.at(default_lang).at(key))
} else {
return data.at(selected_lang).at(key)
}
})
}
11 changes: 11 additions & 0 deletions packages/preview/linguify/0.1.0/typst.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "linguify"
version = "0.1.0"
entrypoint = "linguify.typ"
authors = ["Jomaway <https://github.com/jomaway>"]
license = "MIT"
description = "Load strings for different languages easily"
repository = "https://github.com/jomaway/typst-linguify"
keywords = ["i18n, language, internationalization"]
compiler = "0.10.0"
exclude = ["examples/*",]

0 comments on commit 89fbbef

Please sign in to comment.