### Pure Ruby Project Setup Source: https://github.com/translation/rails/blob/master/README.md Configure Translation.io for use in a pure Ruby project, including necessary requires and setup. ```ruby require 'rubygems' require 'active_support/all' require 'yaml' class FakeConfig def after_initialize end def development? false end end module Rails class Railtie def self.rake_tasks yield end def self.initializer(*args) end def self.config ::FakeConfig.new end end def self.env ::FakeConfig.new end end task :environment do end require 'translation' I18n.load_path += Dir[File.join('i18n', '**', '*.{yml,yaml}')] # Put your configuration here: TranslationIO.configure do |config| config.yaml_locales_path = 'i18n' config.api_key = '' config.source_locale = 'en' config.target_locales = ['nl', 'de'] config.metadata_path = 'i18n/.translation_io' end ``` -------------------------------- ### Custom Language Code Example Source: https://github.com/translation/rails/blob/master/README.md Define custom language codes by appending text to an existing language code, separated by a hyphen. Custom text can only contain alphabetic characters and hyphens. ```ruby "#{existing_language_code}-#{custom_text}" ``` -------------------------------- ### Initialize Translation Project Source: https://github.com/translation/rails/blob/master/README.md Run this rake task after configuring the gem to initialize your project and push existing translations to Translation.io. ```bash bundle exec rake translation:init ``` -------------------------------- ### Configure Translation.io Initializer Source: https://github.com/translation/rails/blob/master/README.md Configure the Translation.io gem by setting your API key, source locale, and target locales in the initializer file. ```ruby TranslationIO.configure do |config| config.api_key = 'abcdefghijklmnopqrstuvwxyz012345' config.source_locale = 'en' config.target_locales = ['fr', 'nl', 'de', 'es'] end ``` -------------------------------- ### Adding Custom Source File Formats Source: https://github.com/translation/rails/blob/master/README.md Extend the list of source file formats for GetText by adding new extensions. ```ruby TranslationIO.configure do |config| ... config.source_formats << 'rb2' config.erb_source_formats << 'erb2' config.haml_source_formats << 'haml2' config.slim_source_formats << 'slim2' ... end ``` -------------------------------- ### Running RSpec Tests Source: https://github.com/translation/rails/blob/master/README.md Execute the project's specifications using RSpec. ```bash bundle exec rspec ``` -------------------------------- ### Generating Coverage Badge Source: https://github.com/translation/rails/blob/master/README.md Generate a coverage badge for the project using the provided script. ```bash ruby script/coverage-badge ``` -------------------------------- ### GetText Localization Syntax Source: https://github.com/translation/rails/blob/master/README.md Showcases GetText syntax for translations, including pluralization and context. This method automatically scans code for translatable strings, eliminating the need for separate YAML files. ```ruby _("Text to be translated") n_("Singular text", "Plural text", number) p_("context", "Text to be translated") np_("context", "Singular text", "Plural text", number) _('%{city1} is bigger than %{city2}') % { city1: "NYC", city2: "BXL" } ``` -------------------------------- ### I18n YAML Translations Source: https://github.com/translation/rails/blob/master/README.md Demonstrates standard Rails I18n syntax for regular translations, pluralization, and interpolation. Requires corresponding YAML files for translation strings. ```ruby t('inbox.title') t('inbox.message', count: n) t('inbox.hello', name: @user.name) ``` ```yaml en: inbox: title: 'Title to be translated' message: zero: 'no messages' one: 'one message' other: '%{count} messages' hello: 'Hello %{name}' ``` -------------------------------- ### Manually Including GetText Methods Source: https://github.com/translation/rails/blob/master/README.md Manually include GetText methods using TranslationIO::Proxy when object delegation is disabled. ```ruby class Contact < ApplicationRecord extend TranslationIO::Proxy end ``` -------------------------------- ### Sync Translations Source: https://github.com/translation/rails/blob/master/README.md Execute this rake task to send new translatable keys and retrieve new translations from Translation.io. ```bash bundle exec rake translation:sync ``` -------------------------------- ### Customizing Locale Paths Source: https://github.com/translation/rails/blob/master/README.md Define custom paths for GetText and YAML locale files. ```ruby TranslationIO.configure do |config| ... config.locales_path = 'some/path' # defaults to config/locales/gettext config.yaml_locales_path = 'some/path' # defaults to config/locales ... end ``` -------------------------------- ### Run Readonly Translation Sync in CI Source: https://github.com/translation/rails/blob/master/README.md Execute a thread-safe, readonly sync task to prevent CI failures while fetching translations. ```bash bundle exec rake translation:sync_readonly ``` -------------------------------- ### Sync and Show Purgeable Keys Source: https://github.com/translation/rails/blob/master/README.md This rake task synchronizes translations and identifies unused keys by comparing against the current branch. Use with caution as it also performs a sync. ```bash bundle exec rake translation:sync_and_show_purgeable ``` -------------------------------- ### Custom Localization Key Prefixes Source: https://github.com/translation/rails/blob/master/README.md Add custom prefixes for localization keys to separate them from translation strings. ```ruby TranslationIO.configure do |config| ... config.localization_key_prefixes = ['my_gem.date.formats'] ... end ``` -------------------------------- ### Configure Translation.io to Disable GetText Source: https://github.com/translation/rails/blob/master/README.md Modify the `TranslationIO.configure` block to exclusively use YAML files for translations. ```ruby TranslationIO.configure do |config| ... config.disable_gettext = true ... end ``` -------------------------------- ### Configure Translation.io to Disable YAML Source: https://github.com/translation/rails/blob/master/README.md Modify the `TranslationIO.configure` block to exclusively synchronize GetText files and leave YAML files unchanged. ```ruby TranslationIO.configure do |config| ... config.disable_yaml = true ... end ``` -------------------------------- ### Add Translation Gem to Gemfile Source: https://github.com/translation/rails/blob/master/README.md Add this line to your project's Gemfile to include the Translation.io gem. ```ruby gem 'translation' ``` -------------------------------- ### Route Scoping for Locale Source: https://github.com/translation/rails/blob/master/README.md Define scoped routes to handle locale prefixes in URLs, such as `/fr`. ```ruby scope "/:locale", :constraints => { locale: /[a-z]{2}/ } do resources :pages end ``` -------------------------------- ### Ignoring YAML Keys Source: https://github.com/translation/rails/blob/master/README.md Configure prefixes to ignore specific YAML keys, useful for gem-specific or common keys. ```ruby TranslationIO.configure do |config| ... config.ignored_key_prefixes = [ 'number.human', 'admin', 'errors.messages', 'activerecord.errors.messages', 'will_paginate', 'helpers.page_entries_info', 'views.pagination', 'enumerize.visibility', /\.code$/ ] ... end ``` -------------------------------- ### Define Component Translations in YAML Source: https://github.com/translation/rails/blob/master/README.md Structure component-specific translations within your YAML locale files. ```yaml en: my_component: your_name: Your name title: Title ``` -------------------------------- ### Including Gems with GetText Strings Source: https://github.com/translation/rails/blob/master/README.md Specify gems that contain GetText strings to ensure they are synchronized. ```ruby TranslationIO.configure do |config| ... config.parsed_gems = ['your_gem_name'] ... end ``` -------------------------------- ### Set Locale Locally in Rails Source: https://github.com/translation/rails/blob/master/README.md Directly set the I18n locale to a specific language code. ```ruby I18n.locale = 'fr' ``` -------------------------------- ### Sync and Purge Unused Keys Source: https://github.com/translation/rails/blob/master/README.md This rake task synchronizes translations and permanently deletes unused keys from Translation.io based on the current branch. Use with extreme caution. ```bash bundle exec rake translation:sync_and_purge ``` -------------------------------- ### Enable I18n Fallbacks Source: https://github.com/translation/rails/blob/master/README.md Enable locale fallbacks in your Rails application by setting `config.i18n.fallbacks = true` in `config/application.rb`. This allows regional or custom languages to fallback to their main language if a translation is missing. ```ruby config.i18n.fallbacks = true ``` -------------------------------- ### Pass GetText Translations to React Component Source: https://github.com/translation/rails/blob/master/README.md Serialize GetText translations as props for a React component. ```erb <%= react_component("MyComponent", { :user_id => current_user.id, :i18n => { :your_name => _('Your name'), :title => _('Title') } }) %> ``` -------------------------------- ### Set Locale Globally in Rails Controller Source: https://github.com/translation/rails/blob/master/README.md Use `set_locale` as a before_action in `ApplicationController` to manage the application's locale. ```ruby class ApplicationController < ActionController::Base before_action :set_locale [...] end ``` -------------------------------- ### Pass I18n Translations to React Component Source: https://github.com/translation/rails/blob/master/README.md Serialize I18n translations from YAML files as props for a React component using `react-rails`. ```erb <%= react_component("MyComponent", { :user_id => current_user.id, :i18n => YAML.load_file("config/locales/#{I18n.locale}.yml")[I18n.locale.to_s]["my_component"] }) %> ``` -------------------------------- ### Disabling GetText Object Monkey-Patching Source: https://github.com/translation/rails/blob/master/README.md Disable the default GetText method availability on the Object class. ```ruby TranslationIO.configure do |config| ... config.gettext_object_delegate = false ... end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.