Giter Site home page Giter Site logo

i18n_backend_database's Introduction

A Database Backend For Rails I18N

Stores your translations in the database, rather than yaml files. As you tag items with i18n.t() throughout your code base, all untranslated items are marked and added to the database. An admin panel is provided so translators can quickly translate untranslated text. All lookups occur in a cache store of your choice prior to hitting the database.

DISCLAIMER!

  • In implementing this into another project, I realized that the currency support is currently broken. It basically boils down to the i18n gem accessing some of the Rails i18n yaml’s in a different way than others. In this case, it’s requesting a parent key, and expects a hash to be returned containing the children. This is not baked in the plugin at the moment, and any contributions toward this would be well received. Thank you wlorentson!
  • The translations_controller is unprotected, and you’ll probably want to add some kind of authorization filter to it to make sure the outside world can’t access it.

Installation

  
    script/generate i18n_backend_database       # add migration
    rake db:migrate                             # migrate

    rake i18n:populate:load_default_locales     # populate default locales
    rake i18n:populate:from_rails               # populate the locales and translations tables from all Rails Locale YAML files.
    rake i18n:populate:from_application         # populate the translation tables from translation calls within the application.
    rake i18n:populate:synchronize_translations # create non-default locale translation records from default locale translations.
    rake i18n:populate:all                      # run all populate tasks.
    rake i18n:translate:google                  # translate all untranslated string values using Google Language Translation API.
  

In config/initialisers/i18n.rb

  
    I18n.backend = I18n::Backend::Database.new # registers the backend
    I18n.backend.cache_store = :memory_store   # optional: specify an alternate cache store
    I18n.backend.localize_text_tag = '##'      # optional: specify an alternate localize text tag, the default is ^^
  

In config/routes.rb to register admin panel routes

  
    map.from_plugin 'i18n_backend_database'
  

Use

All non-user generated text provided by the application needs to be wrapped in a call to I18n.t().


I18n.t("Hello there!")

Interpolation is handled by passing in key/value pairs as a value to an interpolation tag ( {{ }} ).


I18n.t("Hello there {{name}}!", :name => "Dylan")

I18n.t("Click {{here}} or {{there}}", :here => "link_to(I18n.t('midnite'), croix_path)", :there => "link_to(I18n.t('staten_island'), wu_path)")

Pluralization is handled by passing in a “count” key value pair, which is a unique interpolation value.


I18n.t("You are {{count}} years old", :count => 100)

Links to external documents that need to be translated should be tagged as well.


I18n.t('http://www.elctech.com/core')

All fragment cache view blocks need to have their keys prepended with the current locale.


cache("#{I18n.locale}-footer_#{controller.action_name}")

Date/Time localization is handled by using the I18n.l method. The format used will be :default (see next item for explanation).


I18n.l(@user.joined_at)

Date/Time localization can take a format parameter that corresponds to a key in the translations table (the Rails defaults :default, :short, and :long are available). We could in theory create our own like en.date.formats.espn_default.


I18n.l(@user.joined_at, :format => :default) I18n.l(@user.joined_at, :format => :short) I18n.l(@user.joined_at, :format => :espn_default)

Date/Time localization can take a custom format string as well.


I18n.l(@user.joined_at, :format => "%B %e, %Y")

Text stored in a database can be localized by tagging the text being stored and then localizing in the view etc.


I18n.tlt("is now friends with") => "^^is now friends with^^" I18n.lt("shane ^^is now friends with^^ dylan") => "shane ahora es con amigos dylan"

Images can be translated with the I18n.ta tag


<%= image_tag(I18n.ta("logos/elc.gif"), :size => "134x75") %>

In this example, for a locale es, there should be an image: public/es/images/logos/elc.gif

i18n_backend_database's People

Contributors

dylanz avatar smingins avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

i18n_backend_database's Issues

tlt shouldn't allow blank entries

When we parse tlt tags, we translate anything in-between the set tag, ie: ^^this will be translated^^. However, we will incorrectly try and parse this ^^^^. Our regex should account for these cases, and require included content.

Error at "rails g i18n_backend_database" (Rails 3.1)

terminal output:

rails g i18n_backend_database
$HOME/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/base.rb:1088:in `method_missing': undefined method `named_scope' for #<Class:0xa050d5c> (NoMethodError)ax
  from $HOME/.rvm/gems/ruby-1.9.2-p290/gems/i18n_backend_database-0.0.1/lib/models/locale.rb:6:in `<class:Locale>'
  from $HOME/.rvm/gems/ruby-1.9.2-p290/gems/i18n_backend_database-0.0.1/lib/models/locale.rb:1:in `<top (required)>'
  from $HOME/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `require'
  from $HOME/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `block in require'
  from $HOME/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:223:in `block in load_dependency'
  from $HOME/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:640:in `new_constants_in'
  from $HOME/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:223:in `load_dependency'
  from $HOME/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `require'
  from $HOME/.rvm/gems/ruby-1.9.2-p290/gems/i18n_backend_database-0.0.1/lib/i18n_backend_database.rb:1:in `<top (required)>'
  from $HOME/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.21/lib/bundler/runtime.rb:68:in `require'
  from $HOME/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.21/lib/bundler/runtime.rb:68:in `block (2 levels) in require'
  from $HOME/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.21/lib/bundler/runtime.rb:66:in `each'
  from $HOME/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.21/lib/bundler/runtime.rb:66:in `block in require'
  from $HOME/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.21/lib/bundler/runtime.rb:55:in `each'
  from $HOME/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.21/lib/bundler/runtime.rb:55:in `require'
  from $HOME/.rvm/gems/ruby-1.9.2-p290/gems/bundler-1.0.21/lib/bundler.rb:122:in `require'
  from /media/PORT/work/couture/config/application.rb:13:in `<top (required)>'
  from $HOME/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/commands.rb:21:in `require'
  from $HOME/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/commands.rb:21:in `<top (required)>'
  from script/rails:6:in `require'
  from script/rails:6:in `<main>'

rails 2.3.8 bug

I18n defines a Locale module,
This messes up the access to the plugin's Locale Class...
All the methods the are called from the Database instance like available_locales fails because they are trying to reach Locale module instead of the Locale class

seed_application_translations not respecting options

To replicate:

Place this somewhere in the /app directory:
I18n.t('activerecord.errors.messages.record_invalid', :errors => "test test test")

Run the "I18n:populate:from_rails" rake task. The regular expressions grabs the key, but not the interpolation arguments, which it should.

Please add a license file

We are interested in using this software but we need to know what the license is. MIT? Pretty much any non GPLish would be great.

Geoff

"Interpolation argument * missing in *" error in rake task

Hello,
I am trying to import my translations into database with rake i18n:populate:from_application but the task is always interrupted by an interpolation argument error:

interpolation argument product missing in "{{product}} {{version}} released!"

In application, the translation is used several times:
text = I18n.t('txt.home.new_release', :product => product_name, :version => version)
xml.title t('txt.home.new_release', :product => post.product_name, :version => post.version)
<%= t('txt.home.new_release', :product => latest.product_name, :version => latest.version) %>

All the interpolation arguments are correct and a whole bunch of other translations with interpolation arguments is being imported without a problem. Am I missing something?

Thanks!

Rails 3.x support

Hi there,

Awesome gem. Is there any rails 3 support planned for it?
Or do you know a way to get it to work?

Many thanks

Wijnand

use text instead of strings for raw_key, value

'string' ends up being 255 chars, which is not enough for longer bits of text.

 create_table :translations do |t|
   t.string   :key
-      t.string   :raw_key
-      t.string   :value
+      t.text     :raw_key
+      t.text     :value
   t.integer  :pluralization_index, :default => 1
   t.integer  :locale_id
 end

There should also be a little bit more room in the translation workspace:

--- a/lib/views/translations/translations.html.erb
+++ b/lib/views/translations/translations.html.erb
@@ -19,7 +19,7 @@
   <% remote_form_for([@locale, translation]) do |f| %>
 <p><%=h translation.default_locale_value || translation.raw_key %></p>
 <p>
-      <%= f.text_field :value %>
+      <%= f.text_field :value, :size => 128 %>
   <%= f.submit "Update" %>
 </p>
   <% end %>

That could also be made to be a bit 'clever', and use a text area for larger bits of text...

rake i18n:populate:all causes interpolation argument errors missing in "Validation failed: {{errors}}"

We're running Rails 2.3.5, not sure if something changed in there. The problem has to do with some of the stuff in this commit http://github.com/dylanz/i18n_backend_database/commit/e1faa3f384daf26b2809aadfdb4f86569615497f

What I was seeing is the object that was being scanned for interpolation arguments looking something like this:
"'activerecord.errors.messages.record_invalid', :errors=>errors"

and not being able to parse out the fact that it needs an errors option. My regex_fu is limited so I haven't been able to come up with something better.

License issue

Why did you release this under such a strange license?

I'd use this, but requiring all companies to display "This product includes software developed by ELC Technologies" seems excessive.

Would you reconsider releasing this under a more standard open license (MIT, BSD, GPL)?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.