{"id":16987849,"url":"https://github.com/timfjord/i18n-transformers","last_synced_at":"2026-04-17T09:31:31.995Z","repository":{"id":56877159,"uuid":"107685995","full_name":"timfjord/i18n-transformers","owner":"timfjord","description":"Transformers for I18n ruby library","archived":false,"fork":false,"pushed_at":"2023-06-28T08:11:26.000Z","size":16,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-03-12T03:29:06.413Z","etag":null,"topics":["i18n","markdown","ruby","transformations"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/timfjord.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2017-10-20T14:10:52.000Z","updated_at":"2025-07-17T19:15:22.000Z","dependencies_parsed_at":"2023-07-14T11:15:46.207Z","dependency_job_id":null,"html_url":"https://github.com/timfjord/i18n-transformers","commit_stats":{"total_commits":7,"total_committers":1,"mean_commits":7.0,"dds":0.0,"last_synced_commit":"c735ee45535e459b97e08aa95ba9374b764f1ff5"},"previous_names":["timsly/i18n-transformers"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/timfjord/i18n-transformers","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timfjord%2Fi18n-transformers","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timfjord%2Fi18n-transformers/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timfjord%2Fi18n-transformers/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timfjord%2Fi18n-transformers/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/timfjord","download_url":"https://codeload.github.com/timfjord/i18n-transformers/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timfjord%2Fi18n-transformers/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31923082,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-17T09:10:15.403Z","status":"ssl_error","status_checked_at":"2026-04-17T09:10:14.455Z","response_time":62,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["i18n","markdown","ruby","transformations"],"created_at":"2024-10-14T02:50:54.631Z","updated_at":"2026-04-17T09:31:31.973Z","avatar_url":"https://github.com/timfjord.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# I18n::Transformers\n\n[![Build Status](https://travis-ci.org/timsly/i18n-transformers.svg?branch=master)](https://travis-ci.org/timsly/i18n-transformers)\n\nTransformers for `I18n` ruby library\n\n`I18n::Transformers` is a `I18n` ruby library plugin that allows transform translations.\n\n`I18n::Transformers` can be used if some or even all translations keys require some transformation.\nFor example parsing markdown or replacing some symbol.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'i18n-transformers'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install i18n-transformers\n\n## Usage\n\nThe core of this library is `I18n.transformers.register` method.\nIt can be used to register various transformers or reset all available transformers.\nIt is designed to register transformers globally.\nIt returns transformer instance that can be later used with `before`/`after` options.\n\nIf some transformers have to be applied only for some set of keys\nthen this logic has to implemented in the transformer class itself.\nAlso, it is very important to return original value in this case(see [Transformers chain section](#transformers-chain))\n\nThe gem comes with a few built-in transformers:\n\n* generic transformer\n* markdown transformer\n\nIf symbol was passed as the first argument to `.register` call\nit will search thought built-in transformers and try to register it.\nIf there is no match generic adapter will be used.\n\n### Generic transformer\n\nGeneric transformer is designed to handle custom transformers defined as a block.\nIt is useful for simple transformations.\n\nTo register generic transformer call:\n\n```ruby\nI18n.transformers.register do |key, value|\n  \"transformered-#{value}\"\nend\n```\n\n`I18n.translate` key and translation value will be passed to the block.\n\nEach transformer has its name that helps to register transformer after or before some specific transformer.\nBy default generic transformer generates unique name for each transformer,\nbut it can be also explicitly specified by passing name as the first argument.\n\n```ruby\nI18n.transformers.register 'my_transformer' do |key, value|\n  \"transformered-#{value}\"\nend\n```\n\nAll transformers can be registered before, after or at specific position.\n`before`, `after` and `at` options are used for that.\n\n```ruby\nI18n.transformers.register 'my_transformer' do |key, value|\n  \"transformered1-#{value}\"\nend\n\nanother_transformer = I18n.transformers.register after: 'my_transformer' do |key, value|\n  \"transformered2-#{value}\"\nend\n\nI18n.transformers.register before: another_transformer, do |key, value|\n  \"transformered3-#{value}\"\nend\n\nI18n.transformers.register at: 1, do |key, value|\n  \"transformered4-#{value}\"\nend\n```\n\n### Markdown transformer\n\nTo register built-in markdown transformer use:\n\n```ruby\nI18n.transformers.register :markdown\n```\n\nCurrently it supports [`redcarpet`](https://github.com/vmg/redcarpet) and [`kramdown`](https://github.com/gettalong/kramdown) markdown parsers.\n\nIt uses first available adapter so if both are available `redcarpet` will be picked.\nTo specify adapter use:\n\n```ruby\nI18n.transformers.register :markdown, adapter: :redcarpet\n```\n\nMarkdown transformer is designed to be triggered only for some set of keys.\nBy default it triggers only for keys ended with `_md` and `.md`.\n\nSo specify custom key pattern use:\n\n```ruby\nI18n.transformers.register :markdown, key_pattern: /_markdown$/\n```\n\nIn the example above markdown transformer will be triggered only if key ended with `_markdown`.\n\nIf block was specified it will be used instead.\nIt can be useful to override default transformation behavior.\n\n```ruby\nI18n.transformers.register :markdown do |key, value|\n  MyMarkdownParser.parse(value)\nend\n```\n\nPlease note that it will be still running only if key matches `key_pattern` option.\n\nMarkdown transformer always uses the same name - `'markdown'` - so to register some transformer after/before it use\n\n```ruby\nI18n.transformers.register :markdown\n\nI18n.transformers.register 'my_custom_transformer', after: 'markdown' do |key, value|\n  Smth.transform(key, value)\nend\n```\n\nIt is designed to be registered only once that's why `name` is always `markdown`.\n\n### Custom transformers\n\nTo register custom transformer use:\n\n```ruby\nI18n.transformers.register MyCustomTransformer\n```\n\n`MyCustomTransformer` has to be inherited from `I18n::Transformers::Collection::Base` class.\nAlso, to not break transformers chain(see [Transformers chain section](#transformers-chain)),\nit is responsible for returning correct value if it needs to be applied only for some set of keys\n\nAll options except `before`, `after` and `at` will be passed as the first argument to `.new` call.\n\nTransformer instance can be passed too:\n\n```ruby\nI18n.transformers.register MyCustomTransformer.new(:val1, :val2)\n```\n\n### Transformers chain\n\nIt is very important to return some value from either block or `#transform` method from the custom transformer class\notherwise transformers chain will be broken.\n\nHere is the bad example of custom transformer:\n\n```ruby\nI18n.transformers.register do |key, value|\n  if key.to_s.start_with? 'key_prefix'\n    value.gsub('^symbol^', 'REPLACED_SYMBOL')\n  end\nend\n```\n\nThe code above most likely will be broken for all keys that don't start with `key_prefix`.\nTo fix that we should always fallback to the original value\n\n```ruby\nI18n.transformers.register do |key, value|\n  if key.to_s.start_with? 'key_prefix'\n    value.gsub('^symbol^', 'REPLACED_SYMBOL')\n  else\n    value\n  end\nend\n```\n\n### Reseting transformers\n\nThis library is designed to register all transformers globally.\nIn a rails app it can be done in initializer.\nBut if there is a need to reset all transformers `.reset` method can be used.\n\n```ruby\nI18n.transformers.reset\n```\n\n## Example\n\nTo illustrate how this gem works let's assume the following setup\n\n```yaml\nen:\n  key: my key1 value ^symbol^\n  key_md: '**Text with ^symbol^**'\n  key_markdown: '*^symbol^*'\n```\n\n```ruby\nI18n.transformers.register :markdown # let's assume redcarpet gem is available\n\nI18n.transformers.register do |key, value|\n  value.gsub('^symbol^', 'REPLACED_SYMBOL')\nend\n```\n\nThen\n```ruby\nI18n.translate :key # =\u003e my key1 value REPLACED_SYMBOL\n\nI18n.translate :key_md # =\u003e \u003cp\u003e\u003cstrong\u003eText with REPLACED_SYMBOL\u003c/strong\u003e\u003c/p\u003e\n\nI18n.translate :key_markdown # =\u003e *REPLACED_SYMBOL*\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## TODO\n\n* Think more about missing translations and find a nice way to handle them\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/timsly/i18n-transformers.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimfjord%2Fi18n-transformers","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimfjord%2Fi18n-transformers","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimfjord%2Fi18n-transformers/lists"}