|
| 1 | +# Glossary |
| 2 | + |
| 3 | +**Minimalistic semantic translation system for Elixir apps.** |
| 4 | + |
| 5 | +Glossary is a lightweight and expressive alternative to gettext for modern Elixir applications — especially Phoenix LiveView. |
| 6 | +It embraces semantic lexemes, YAML lexicons, and compile-time localization with a simple and explicit API. |
| 7 | + |
| 8 | +Each YAML file acts as a lexicon — a mapping of semantic keys (lexemes) to localized values (expressions) for a given language. |
| 9 | +All lexicons are compiled into the module at build time, enabling fast and predictable lookups at runtime. |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## 🧱 Concept |
| 14 | + |
| 15 | +A **lexeme** is a minimal semantic unit — a key like "game.won". |
| 16 | +An **expression** is its localized realization — a string like "You won!". |
| 17 | +A **lexicon** is a YAML file that maps lexemes to expressions for a specific language. |
| 18 | + |
| 19 | +Together, lexicons form a **glossary** — a complete set of localized meanings. |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## ✨ Features |
| 24 | + |
| 25 | +- 🧠 **Semantic keys** — Use lexemes like `"game.score"`, not literal strings. |
| 26 | +- 🔄 **Runtime interpolation** — Simple bindings with `{{key}}` syntax. |
| 27 | +- ⚡ **Live reloadable** — Load translations dynamically, perfect for LiveView. |
| 28 | +- 📄 **YAML-first** — Intuitive, version-friendly format. |
| 29 | +- 🧪 **No hidden magic** — Only explicit macros for setup, no DSLs, no runtime surprises. |
| 30 | + |
| 31 | +--- |
| 32 | + |
| 33 | +## Lexicons (YAML) |
| 34 | + |
| 35 | +Each YAML file represents a **lexicon** — a set of localized expressions. |
| 36 | + |
| 37 | +Lexicons are merged into a single lookup table keyed by `"language.lexeme"`. |
| 38 | + |
| 39 | +Example structure of lexicon: |
| 40 | + |
| 41 | +```yaml |
| 42 | +# game.en.yml |
| 43 | + game: |
| 44 | + won: "You won!" |
| 45 | + lost: "Game over." |
| 46 | + score: "Your score: {{score}}" |
| 47 | + |
| 48 | +# game.ru.yml: |
| 49 | + game: |
| 50 | + won: "Вы победили!" |
| 51 | + lost: "Игра окончена." |
| 52 | + score: "Ваш счёт: {{score}}" |
| 53 | + |
| 54 | +# user.en.yml: |
| 55 | + user: |
| 56 | + greeting: "Hello, {{name}}!" |
| 57 | + |
| 58 | +# user.ru.yml: |
| 59 | + user: |
| 60 | + greeting: "Привет, {{name}}!" |
| 61 | +``` |
| 62 | +
|
| 63 | +--- |
| 64 | +
|
| 65 | +## 🚀 Quick Start |
| 66 | +
|
| 67 | +### 1. Add to your project |
| 68 | +
|
| 69 | +[available in Hex](https://hex.pm/packages/glossary), the package can be installed |
| 70 | +by adding `glossary` to your list of dependencies in `mix.exs`: |
| 71 | + |
| 72 | +```elixir |
| 73 | +# mix.exs |
| 74 | +def deps do |
| 75 | + [ |
| 76 | + {:glossary, "~> 0.1"} |
| 77 | + ] |
| 78 | +end |
| 79 | +``` |
| 80 | + |
| 81 | +### 2. Define a module with glossary, specify a lexicons list |
| 82 | + |
| 83 | +```elixir |
| 84 | +# my_app_web/live/game/show.ex |
| 85 | +defmodule MyAppWeb.Live.Game.Show do |
| 86 | + use Glossary, ["game", "../users/user", "../common"] |
| 87 | +end |
| 88 | +``` |
| 89 | + |
| 90 | +This loads: |
| 91 | + |
| 92 | +- `my_app_web/live/common.en.yml`, `my_app_web/live/common.ru.yml` |
| 93 | +- `my_app_web/live/game/game.en.yml`, `my_app_web/live/game/game.ru.yml` |
| 94 | +- `my_app_web/live/users/user.en.yml`, `my_app_web/live/users/user.ru.yml` |
| 95 | + |
| 96 | +### 3. Use in LiveView templates by lexeme |
| 97 | + |
| 98 | +```elixir |
| 99 | +<%= MyAppWeb.Live.Game.Show.t("game.score", @locale, score: 42) %> |
| 100 | +``` |
| 101 | + |
| 102 | +### 4. You’ll see the full key on the page, e.g., `en.game.score` |
| 103 | + And a warning in your logs: |
| 104 | + |
| 105 | + ```text |
| 106 | + [warning] [Glossary] Missing: en.game.score |
| 107 | + ``` |
| 108 | + |
| 109 | +### 5. Add the translation to your YAML lexicon |
| 110 | + |
| 111 | + ```yaml |
| 112 | + # game.en.yml: |
| 113 | + game: |
| 114 | + score: "Your score: {{score}}" |
| 115 | + ``` |
| 116 | + |
| 117 | +### 6. Reload the page — warning disappears, and translated text is shown. |
| 118 | + |
| 119 | +### 7. Repeat until all template keys are covered and logs are clean. |
| 120 | + |
| 121 | +### 8. Only after the primary language is complete, translate the rest by following the structure. |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +## 💡 API |
| 126 | + |
| 127 | +```elixir |
| 128 | +t(lexeme, locale) |
| 129 | +t(lexeme, locale, bindings) |
| 130 | +``` |
| 131 | + |
| 132 | +- Falls back to `lexeme` if no translation is found. |
| 133 | +- Interpolates placeholders like `{{score}}` with values from `bindings`. |
| 134 | + |
| 135 | +--- |
| 136 | + |
| 137 | +## 📚 Best Practices |
| 138 | + |
| 139 | +- ✅ Use **semantic keys**: `"user.greeting"` > `"welcome_text_1"` |
| 140 | +- 📁 Group by domain: `user`, `game`, `import`, etc. |
| 141 | +- 🧩 Prefer flat 2-level keys: `domain.key` |
| 142 | +- 🔑 Avoid file-based logic — only lexemes and language matter |
| 143 | +- 🪄 Use `{{key}}` placeholders for dynamic values |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +## 🏛️ Philosophy |
| 148 | + |
| 149 | +Glossary was built for **dynamic apps** — like those using Phoenix LiveView — where UI, state, and translations often evolve together. |
| 150 | + |
| 151 | +### 🔍 Comparison with Gettext |
| 152 | +Glossary is built for interactive, reactive, hot-reloaded systems. |
| 153 | +Gettext was designed for monolithic, statically compiled apps in the 1990s. |
| 154 | +Phoenix LiveView is dynamic, reactive, and often developer-translated. |
| 155 | +Glossary brings translation into the runtime flow of development. |
| 156 | + |
| 157 | +| Feature | Glossary | Gettext | |
| 158 | +|----------------------|---------------------|---------------------| |
| 159 | +| ✅ Semantic keys | Yes (`"home.title"`) | No (uses msgid) | |
| 160 | +| ✏️ YAML format | Yes | No (.po files) | |
| 161 | +| ♻️ Live reload | Easy | Needs recompilation | |
| 162 | +| 📦 Runtime API | Simple `t/2`, `t/3` | Macro-based | |
| 163 | +| 🧪 Dev experience | Transparent | Magic/macros | |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +### Why move beyond gettext? |
| 168 | + |
| 169 | +- You want **declarative keys** and **semantic structure** |
| 170 | +- You want to **edit translations live** |
| 171 | +- You don’t want to manage `.po` files or run compilers |
| 172 | +- You want your **UI and language logic to stay in sync** |
| 173 | + |
| 174 | +--- |
| 175 | + |
| 176 | +## 🧠 Acknowledgments |
| 177 | + |
| 178 | +Inspired by the real-world needs of building modern Phoenix LiveView apps with: |
| 179 | + |
| 180 | +- ✨ Declarative UIs |
| 181 | +- 🔁 Dynamic state |
| 182 | +- 🛠️ Developer-driven i18n |
| 183 | + |
| 184 | +--- |
| 185 | + |
| 186 | +## 📬 Feedback |
| 187 | + |
| 188 | +Glossary is small, hackable, and stable — and we're open to ideas. |
| 189 | +Raise an issue, suggest a feature, or just use it and tell us how it goes. |
| 190 | + |
| 191 | +> Let your translations be as clean as your code. |
0 commit comments