GithubHelp home page GithubHelp logo

leadformance / hstore_translate Goto Github PK

View Code? Open in Web Editor NEW
137.0 137.0 39.0 79 KB

Rails I18n library for ActiveRecord model/data translation using PostgreSQL's hstore datatype

License: MIT License

Ruby 95.19% Logos 4.81%

hstore_translate's People

Contributors

etiennedepaulis avatar lminaudier avatar mfilej avatar robworley avatar route avatar rsempe avatar tompesman 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hstore_translate's Issues

Gem Release needed

Heyhey,

could you push out a new gem version? Using JRuby, Bundler throws an error at installing the pg gem.

Thanks!

Can't assign translations from controller

Hi, I don't know if I'm missing something, but I can't make this work when assigning translations from a controller.

Here I have a simple example with a Posts scaffold:

https://github.com/jaimeiniesta/active_admin_hstore

I'm trying to create posts assigning its titles for English and Spanish, like:

https://github.com/jaimeiniesta/active_admin_hstore/blob/master/app/views/posts/_form.html.erb

But I get this error:

ActiveModel::MassAssignmentSecurity::Error in PostsController#create
Can't mass-assign protected attributes: title_en, title_es

Then, if I make them accesible like this:

class Post < ActiveRecord::Base
  translates :title
  attr_accessible :title_en, :title_es
end

I get this other error:

ActiveRecord::UnknownAttributeError in PostsController#create
unknown attribute: title_en

What's wrong? How can I do this?

Region specific translations with fallback to common language

I've had a look at the code and based on the doc and tests, I figure out how these cases are handled (expressed as JSON)?

{
 "nl": "color",
 "en": "color", 
 "en-gb": "colour",
 "zh": "颜色",
 "zh-tw": "顏色"
}

(all these translations are different, except nil which matches en which also matches implicitly en-US).

Could you comment if this case is possible with hstore_translate at the moment?

Querying multiple records

Hi,

Is there any way to query for multiple objects?

Something like this:
Post.with_title_translations(["First title", "Second title"]) ?

Empty strings inserted from forms break translation fallbacks

I have forms where I use title_en and title_de to allow editing of both languages in the admin backend. When these are posted even if the user didn't change anything they are serialized as empty strings, even if they used to be nil.

The translation fallbacks only work for nil values. I now solved this with a callback that deletes all blank values and this is of course an ok approach. I just wanted to run this by you and see if you think this should be solved in the library itself. Maybe with an option to translate or maybe just documented somewhere.

If you want me to proceed in any direction just drop me a line here, otherwise feel free to just close the issue.

Dynamical attr_accessible attributes

Instead of defining each several language for attr_accessible

class Product < ActiveRecord::Base
  attr_accessible :name_en, :name_de, :name_fr
  translates :name
end

it would be nice to set only one central attribute.

class Product < ActiveRecord::Base
  attr_accessible :name_translations
  translates :name
end

Is there a way to change this behavior?

Dynamic strong params list for Rails 4

I18n.available_locales.each { |locale| attr_accessible "name_#{locale}" }

Is there a nice way to do this in Rails 4? I understand I have to list all fields as strong params, but I've been unable to find any nice looking solution if there are many locales and a lot of translated fields.
#18

"Random" problem on fallback heroku

We are facing kind of a funny problem on heroku, here is the context:

We putted the site in staging mode with i18n.fallbacks = true.
then when we tried to print a translated field in the view, only the local.to_s was printed (in our case: en-US).

The problem solved itself when we finally tried to switch the i18n.fallbacks to false.

Here is the funny part, the problem happened today, (no config changed) and the only "fix" that we were able to do was to switch again i18n.fallbacks' value (now to true), after that, we could change the i18n.fallbacks value without problem.

I know its a long shot but we honnestly don't know what to do anymore :/

we are using RAILS 4 on heroku without any cache.

Column for the translated attribute is not populated

I've just found out something rather surprising.
Unlike Globalize3, when you create a new record, the column for the attribute that's being translated is not populated

class Country < ActiveRecord::Base
  translates :name
end

Then, when creating a new Country, the name_translations column is populated with en => 'my country' but name is NULL

The most annoying part is that if then I do Country.find_by_name('my country') I get a nil.

Is there any way to go around that?

thanks, P.

Is there a way to do assignment with culture

I'm maintaining an app that uses this gem. I'd like to seed some data but I don't want to iterate through cultures to do it.

I found I could do this:

model.send('write_hstore_translation', 'name', translation.text, culture)

But that is a protected method. Is there another assignment function call that I can make were I can pass in the culture?

Thanks,
John

Make translations column name configurable

It would be nice to have an option to define the name of the column used to store the translations. E.g. I'd like to use a column without the _translations extension. Why not use something like title directly?

Fallbacks

Hi, guys!

Example:
body_translations: { ru: "" }
ru - default locale
When you select en locale, fallback returns nil.

I think, that correct behavior is to return empty string, not nil.
What do you think?

Deprecation warning in Rails 5

DEPRECATION WARNING: alias_method_chain is deprecated. Please, use Module#prepend instead. From module, you can access the original method using super
(called from translates at ../gems/hstore_translate-bf7541fbc1c2/lib/hstore_translate/translates.rb:30

Working with extended locale names

I'm having issues working with locale names like "zh-Hans" because title_zh-Hans won't work obviously. parse_translated_attribute_accessor seems to expect 2 character language codes.

How about translation array attributes?

Lets say I have a field .facts on my model that is an array.

['fact1', 'fact2','fact3', ....]

Is it possible to use with the hstore translation?

best regards
denym_

Support for Rails 4.2.4? Getting undefined method title_translations

Hello,

I am using Rails 4.2.4 which includes native support for hstore db type. In my model I've added the following line

class Category < ActiveRecord::Base
  translates :title, :description
end

When I access the title, I get the following error:

NoMethodError: undefined method `title_translations' for ...

Thanks!

How to retrieve a list of translated names?

See the following question in Stack Overflow

http://stackoverflow.com/questions/26921553/returning-and-sorting-by-value-extracted-from-an-hstore-postgres-hstore-trans

I am using hstore_translate within a Rails4 project to handle
my I18n needs.

Assume I have the following model:

class Thingy < ActiveRecord::Base
  translates :name
end

with table defined in a migration as

create_table :thingies do |t|
  t.hstore name_translations
end
add_index ::thingies, :name_translations, using: :gin

In my Ruby code I wish to retrieve a list of the all the names and ids for Thingies with a name in a specific locale.

Previously, before my Thingy had a localised name I could just do

thingies = Thingy.order(:name).pluck(:id, :name)

Now I am doing

thingies = Thingy.where("name_translations ? 'en'").to_a.map do |t|
  {id: t.id, name: t.name}
end
thingies.sort! { |a,b| a[:name] <=> b[:name] }

But I can't help feeling there's a way I can better leverage Postgres to do this all in one line of code.

License missing from gemspec

RubyGems.org doesn't report a license for your gem. This is because it is not specified in the gemspec of your last release.

via e.g.

spec.license = 'MIT'
# or
spec.licenses = ['MIT', 'GPL-2']

Including a license in your gemspec is an easy way for rubygems.org and other tools to check how your gem is licensed. As you can imagine, scanning your repository for a LICENSE file or parsing the README, and then attempting to identify the license or licenses is much more difficult and more error prone. So, even for projects that already specify a license, including a license in your gemspec is a good practice. See, for example, how rubygems.org uses the gemspec to display the rails gem license.

There is even a License Finder gem to help companies/individuals ensure all gems they use meet their licensing needs. This tool depends on license information being available in the gemspec. This is an important enough issue that even Bundler now generates gems with a default 'MIT' license.

I hope you'll consider specifying a license in your gemspec. If not, please just close the issue with a nice message. In either case, I'll follow up. Thanks for your time!

Appendix:

If you need help choosing a license (sorry, I haven't checked your readme or looked for a license file), GitHub has created a license picker tool. Code without a license specified defaults to 'All rights reserved'-- denying others all rights to use of the code.
Here's a list of the license names I've found and their frequencies

p.s. In case you're wondering how I found you and why I made this issue, it's because I'm collecting stats on gems (I was originally looking for download data) and decided to collect license metadata,too, and make issues for gemspecs not specifying a license as a public service :). See the previous link or my blog post about this project for more information.

Mark the translation field as dirty, to enable lifecycle callbacks

Like,

class MyModel < ActiveRecord::Base
  translates :name
  after_save :do_something, :if => :name_changed?

  def do_something
  # ...
  end
end

This can be achieved by just adding

send "#{attr_name}_will_change!" unless translations[locale.to_s] == value

in the write_hstore_translation method.

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.