- Notifications
You must be signed in to change notification settings - Fork5
A blazing fast internationalization (i18n) library for Crystal with compile-time key lookup.
License
wout/rosetta
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A blazing fast internationalization (i18n) library for Crystal with compile-timekey lookup. You'll never have amissing translation
in your app, ever again.
Rosetta is different from other internationalization libraries because ithandles key lookup at compile-time rather than runtime. The significantadvantage is that you'll be able to find missing translations - or typos inyour locale keys - during development rather than after you've deployed yourapp. This is also true for translation keys in all additional locales.
In Rosetta, interpolation keys are arguments to the translation method. So ifyou're missing an argument, the compiler will complain. The parser will alsocompare interpolation keys in additional locales to the ones found in thedefault locale, and complain if some are missing.
Benchmarking against other libraries which also use YAML or JSON backends,Rosetta is 12x to 700x faster than any other one.
For simple translations:
crimson-knight/i18n.cr translation 303.57k ( 3.29µs) (± 4.62%) 801B/op 702.21× slower crystal-i18n/i18n translation 18.07M ( 55.35ns) (± 7.28%) 48.0B/op 12.39× slower syeopite/lens translation 5.09M (196.47ns) (± 4.60%) 176B/op 43.98× slower wout/rosetta translation 223.86M ( 4.47ns) (± 2.20%) 0.0B/op fastest
For translations with interpolations:
crimson-knight/i18n.cr interpolation 318.12k ( 3.14µs) (± 0.85%) 801B/op 108.51× slower crystal-i18n/i18n interpolation 65.55k ( 15.26µs) (± 1.01%) 28.2kB/op 664.37× slower syeopite/lens interpolation 2.04M (490.17ns) (± 1.35%) 565B/op 21.35× slower wout/rosetta interpolation 43.55M ( 22.96ns) (± 4.81%) 80.0B/op fastest
Rosetta is that much faster because a lot of the hard work happens atcompile-time. And because the majority of the data is stored on thestackrather than theheap,out of the scope of garbage collector.
Read more onthe official docs page.
- Add the dependency to your
shard.yml
:
dependencies:rosetta:github:wout/rosetta
Run
shards install
Run
bin/rosetta --init
Require the generated config file:
# src/app_name.crrequire"../config/rosetta"
- Include the
Rosetta::Translatable
mixin:
# src/pages/main_layout.crincludeRosetta::Translatable
- Localize your app
Rosetta.locale=:esclassHello::ShowPage <MainLayoutdefcontent h1 r("welcome_message").t(name:"Brian")# => "¡Hola Brian!"endend
Read more onthe official docs page.
Make sure you haveGuardian.cr installed. Thenrun:
$ guardian
This will automatically:
- run ameba for src and spec files
- run the relevant spec for any file in src
- run spec file whenever they are saved
- install shards whenever you save shard.yml
- Fork it (https://github.com/wout/rosetta/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Check out thedocs
branch and run the following command to launch the docs locally:
docker run --rm -it -p 8000:8000 -v ${PWD}:/docs squidfunk/mkdocs-material
- wout - creator and maintainer
This shard pulls inspiration from the following projects:
About
A blazing fast internationalization (i18n) library for Crystal with compile-time key lookup.