https://github.com/oreshinya/purescript-simple-i18n
Type-safe internationalization utilities.
https://github.com/oreshinya/purescript-simple-i18n
i18n purescript
Last synced: 16 days ago
JSON representation
Type-safe internationalization utilities.
- Host: GitHub
- URL: https://github.com/oreshinya/purescript-simple-i18n
- Owner: oreshinya
- License: mit
- Created: 2019-09-22T14:07:00.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-11-17T11:11:02.000Z (over 2 years ago)
- Last Synced: 2025-03-12T09:41:18.461Z (about 1 month ago)
- Topics: i18n, purescript
- Language: PureScript
- Size: 146 KB
- Stars: 9
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# purescript-simple-i18n
[](https://github.com/oreshinya/purescript-simple-i18n/releases)
Type-safe internationalization utilities.
## Installation
### Bower
```
$ bower install purescript-simple-i18n
```### Spago
```
$ spago install simple-i18n
```## Usage
### Define labels
You can define labels **with typelevel string list** for translation.
This force you to translate all labels **without excess and deficiently**.
**NOTE: Labels should be ordered alphabetically.**
```purescript
import Record.Extra (type (:::), SNil)-- Symbols should be in alphabetic order.
type Labels =
( "apple"
::: "banana"
::: "grape"
::: SNil
)
```### Define translations
You can define translations with `Simple.I18n.Translation` module.
```purescript
import Simple.I18n.Translation (Translation, fromRecord)en :: Translation Labels
en = fromRecord
{ apple: "Apple"
, banana: "Banana"
, grape: "Grape"
}ja :: Translation Labels
ja = fromRecord
{ apple: "りんご"
, banana: "バナナ"
, grape: "ぶどう"
}
```### Create translator
Next step is creating translator with `Simple.I18n.Translator` module.
Pass fallback language and translations to `createTranslator`.
```purescript
import Simple.I18n.Translator (Translator, createTranslator, label, setLang, translate)
import Type.Proxy (Proxy(..))translator :: Translator Labels
translator =
createTranslator
(Proxy :: _ "en") -- Fallback language (and default language)
{ en, ja } -- Translations
```### Change language setting
You can set language with `setLang`.
```purescript
import Preludeimport Simple.I18n.Translator (Translator, createTranslator, label, setLang, translate)
main :: Effect Unit
main = do
let translator' = translator # setLang "ja"
-- some codes
```You might think "Why can `setLang` receive `String` instead of `Proxy`?".
The reason is that we get language setting from outside of PureScript like `navigator.language`, `localStorage`, `subdomain`, `path`, `query parameter`, or others in most cases.
So `String` is enough.
### Translate
You can get translation type-safely.
```purescript
import Preludeimport Simple.I18n.Translator (Translator, createTranslator, label, setLang, translate)
import Type.Proxy (Proxy(..))translator :: Translator Labels
translator =
createTranslator
(Proxy :: _ "en") -- Fallback language (and default language)
{ en, ja } -- Translationsmain :: Effect Unit
main = do
log $ translator # translate (label :: _ "apple") -- "Apple"
let translator' = translator # setLang "ja"
log $ translator' # translate (label :: _ "apple") -- "りんご"
-- some codes
```## Documentation
Module documentation is [published on Pursuit](http://pursuit.purescript.org/packages/purescript-simple-i18n).
## LICENSE
MIT