Adds localization support to re-named resources #1885

Closed
wants to merge 1 commit into
from

Projects

None yet

4 participants

Contributor

Fixes an issue I opened a few minutes before ( #1884 )

I made a new localization scope; I'm not sure if this is appropriate or not, but it works.

Contributor

+1 for that

Contributor

Can you provide a quick update to the internatlization documentation? Also, there's a spec that could use updating on this feature as well.

https://github.com/gregbell/active_admin/blob/master/spec/unit/resource/naming_spec.rb

Contributor

It would be better i think if you scope not [:active_admin, :custom_resources] but [:activerecord, :models] to look up for tanslation in active record yml file.

Contributor

Hmm, I suppose. I actually considered that option while writing it, but I decided to use the :custom_resources just in case there were any conflicts with existing models

Contributor

I added some tests and the documentation.. I wasn't sure where to put it, so I just put it under the "Customize The Resource" / "Rename the Resource" section.

Owner

@developer88 and @keichan34, IMO an interesting feature for AA would be a customizable I18n lookup scope.

ActiveAdmin.register Stuff do
  i18n_lookup_override :something, :some_other_thing
end

That way you could use the [:activerecord, :models] scope by default, but let people change it if they need to.

@developer88 developer88 and 1 other commented on an outdated diff Mar 6, 2013
lib/active_admin/resource/naming.rb
@@ -18,7 +18,7 @@ def resource_name
# Returns the name to call this resource such as "Bank Account"
def resource_label
if @options[:as]
- @options[:as]
+ I18n.t resource_name.i18n_key, :scope => [:active_admin, :custom_resources], :default => @options[:as]
developer88
developer88 Mar 6, 2013 Contributor

i've got conflict with gem Russian (i18n for Russian) plus got some other bugs so i changed your code like this:

I18n.t resource_name.i18n_key, :scope => [:activerecord, :models], :default => @options[:as], count: 1  
seanlinsley
seanlinsley Mar 6, 2013 Owner

So it's specifically the lack of a count that causing your troubles?

developer88
developer88 Mar 6, 2013 Contributor

yes for that string and count => 2 for plural_resource_label method.

@developer88 developer88 commented on an outdated diff Mar 6, 2013
lib/active_admin/resource/naming.rb
@@ -27,7 +27,7 @@ def resource_label
# Returns the plural version of this resource such as "Bank Accounts"
def plural_resource_label
if @options[:as]
- @options[:as].pluralize
+ I18n.t resource_name.i18n_key, :scope => [:active_admin, :custom_resources], :default => @options[:as].pluralize, :count => 2
developer88
developer88 Mar 6, 2013 Contributor

and changed this like this:

I18n.t resource_name.i18n_key, scope: [:activerecord, :models], default: @options[:as].pluralize, count: 1.1

of course scope can be different

Contributor

Ah, sorry! I fixed the count and changed to the activerecord.models scope.

Maybe we can implement a i18n_lookup_override class method per @Daxter 's suggestion later? That would make more sense than making a custom translation scope by default.

Owner

@keichan34 that sounds like a good option. I just opened a ticket ( ) so people can discuss.

For this PR, could you squash these commits into a single one (with a useful commit message)?

It's really simple to do with git rebase -i. Let me know if you'd like some help.

Contributor

Squashed. Thanks for opening the ticket 😃

Contributor

@keichan34 Since you changed scope we don't need translations that you had added earlier. Could you please delete it?

Contributor

In the documentation? Got it. Thanks.

Contributor

@keichan34 No, in yml file

Owner

Our Travis builds on master are now green (as of 166bc46). Please rebase your PR on the current master:

# Expectations: "upstream" is gregbell's GitHub repo and "origin" is your fork

# Rebase your fork's master branch with the latest upstream changes
git checkout master
git pull --rebase upstream master
git push origin master

# Rebase your feature branch with the latest upstream changes
git checkout your_feature_branch
git pull --rebase upstream master
git push origin your_feature_branch # note that you may need to use -f
@keichan34 keichan34 Adds localization support to re-named resources
Uses the activerecord.models I18n scope
6f3021f
Contributor

Thanks! Rebased and pushed 😉

Owner

Upon further inspection, isn't this reinventing the wheel a bit?

The whole Naming module as it stands:

def resource_name
  custom_name = @options[:as] && @options[:as].gsub(/\s/,'')
  @resource_name ||= if custom_name || !resource_class.respond_to?(:model_name)
      Resource::Name.new(resource_class, custom_name)
    else
      Resource::Name.new(resource_class)
    end
end

def resource_label
  if @options[:as]
    I18n.t resource_name.i18n_key, :scope => [:activerecord, :models], :default => @options[:as], :count => 1
  else
    resource_name.human(:default => resource_name.gsub('::', ' ').titleize)
  end
end

def plural_resource_label
  if @options[:as]
    I18n.t resource_name.i18n_key, :scope => [:activerecord, :models], :default => @options[:as].pluralize, :count => 1.1
  else
    resource_name.human(:count => 1.1, :default => resource_label.pluralize.titleize)
  end
end

Are you sure we can't just do this?

def resource_label
  resource_name.human :count => 1,   :default => resource_name.gsub('::', ' ').titleize
end

def plural_resource_label
  resource_name.human :count => 1.1, :default => resource_label.pluralize.titleize
end
Owner

@keichan34? @developer88? Anyone still alive?

Contributor

I think i do, since @keichan34 is not responding i can apply your fix... i think i'll be able to do this tomorrow.

Contributor

@Daxter Unfortunately your fix doesn't work for me.

i have a resource:

 ActiveAdmin.register Service, as: "Reporting" do

so with my and @keichan34 fixes it renders name 'Reportings'

with you fixes it renders name 'Service'

Owner

Closing in favor of #2044

@seanlinsley seanlinsley closed this Apr 3, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment