EzDevInfo.com

globalize

A JavaScript library for internationalization and localization that leverages the official Unicode CLDR JSON data

Translations of column in same table

I have small table:

create_table :cities do |t|
  t.string :name
end

I need to internationalize "name" column and I don't want create separate table for this. Is it possible to add columns for translations to "cities" table? In result I want that migration of this table looks like this:

create_table :cities do |t|
 t.string :en_name
 t.string :de_name
 t.string :fr_name
end

Currently I'm trying to use "globalize" gem, maybe I should use some other solution for this, please, advise.


Source: (StackOverflow)

Redirect same page to different language with Globalize & Friendly_id

I've been scratching my head for the last few hours, looking for an answer but I can't find it anywhere.

My gem file:

# Use globalize for translating models
gem "globalize", github: "ncri/globalize" # for Rails 4.2
gem 'globalize-accessors', '~> 0.1.5'

# Use friendly_id for slugs
gem 'friendly_id', '~> 5.1.0'
gem 'friendly_id-globalize', '~> 1.0.0.alpha1'

Here's the situation:

I have two languages "en" and "fr"

2 models : pages and pages_translations pages has a slug column, pages_translations also has a slug column.

if I view the page -> en/pages/slug-en, it works.

if I view the page -> fr/pages-slug-fr, it works.

So I assume friendly_id and globalize are properly configured.

However my problem is that I can't make a language switcher work using:

  <% if I18n.locale != :en %>
    <li>
    <%= link_to t('menu.languages.short_en'), url_for(locale: 'en') %>
    </li>
<% end %>

The route becomes en/pages/slug-fr (i.e. the language changes but not the slug).

I have activated config.use :finders in the initializer.

My page model:

translates :title, :slug, :blurb, :content, :seo_title, :seo_description, :seo_keywords
  globalize_accessors :locales => [:en, :fr], :attributes => [:title, :slug, :blurb, :content, :seo_title, :seo_description, :seo_keywords]

    extend FriendlyId
      friendly_id :slug, :use => :globalize
      validates :slug, presence: true, uniqueness: { case_sensitive: false }

So what do I need to do to have the proper path on my language switcher? Ideally, I'd like this to work with any models, not just the Page model.

Thanks! - Vincent


Source: (StackOverflow)

Advertisements

Rails 4 and globalize not adding translation

I upgraded to Rails 4 and globalize (instead of globalize3) and translations are not working correctly since.

Steps:

create english record
change the locale to :es
then call update_attributes on just created object

This used to create a new translation in es.
But, now, it is modifying the English record instead!!! Please help?

Gemfile:

gem "rails", "4.0.1"
gem "globalize", "~> 4.0.0.alpha.1"

Model:

class GlossaryTerm < ActiveRecord::Base
 translates :term, :definition
 accepts_nested_attributes_for :translations

 has_many :glossary_term_translations
 belongs_to :section
 validates_presence_of :term
 validates_presence_of :definition

 **after_create :create_spanish_placeholder**

 def create_spanish_placeholder
   term = self.term
   definition = self.definition
   I18n.locale = :es
   self.update_attributes(:term => "SPANISH placeholder for #{term}", :definition => "SPANISH placeholder for #{definition}")
   I18n.locale = :en
 end

#...

end

Controller:

class Admin::GlossaryTermsController < ApplicationController
before_filter :authorize_sysadmin!

def create
  find_models
  @glossary_term = @section.glossary_terms.new(glossary_term_params)
  if @glossary_term.save
    redirect_to edit_admin_section_url(@section, program_id: @program.id)
  else
    render :action => "new"
  end
end

#...

 protected

 def glossary_term_params
   params.require(:glossary_term).permit(:term, :definition, :glossary_image,   :glossary_image_file_name,
                                    :translations_attributes => [:id, :term, :definition])
 end

 #...

 end

Source: (StackOverflow)

Switch locale on current page rails

i'm currently working on a project which has 2 different locals (nl/fr).

We're facing this problem : How can I get the translated url for the current page when I display fr/nl button

I'm currently working with friendly_id and globalize

we've tried :

= link_to "nl", params.merge(locale: "nl")
= link_to "nl", url_for(:locale => :nl)

both work to change the current language but as we've friendly_url, when the page is loaded in french (localhost:3000/c/animaux)

we should have

localhost:3000/nl/c/dieren

instead of

localhost:3000/nl/c/animaux 

I've so many link to translate so I wish there's a railsway to do this.


Source: (StackOverflow)

Thinking Sphinx : Multiple indices and Delta

Since I have a model with localized fields (using Globalize, I'd like to make localized search. I set up two indices on my model :

ThinkingSphinx::Index.define :document, name: "document_fr", :with => :active_record, :delta => true do

    indexes translations.name, :as => :options
    indexes translations.description
    where "(#{Document.translations_table_name}.locale='fr' OR #{Document.translations_table_name}.locale IS NULL)"
    has created_at, updated_at

end

ThinkingSphinx::Index.define :document, name: "document_nl", :with => :active_record, :delta => true do

    indexes translations.name, :as => :options
    indexes translations.description
    where "(#{Document.translations_table_name}.locale='nl' OR #{Document.translations_table_name}.locale IS NULL)"
    has created_at, updated_at

end

How can I make a search on one index, using the delta ?

I tried to use the indices options but it does not really work :

Document.search 'something', {
    indices: ["document_fr_core", "document_fr_delta"]
}

=> If I change a Document instance and save it, delta indexing happens and the change are indexed in *_delta. But since I am also searching on *_core, the modified instance is still found even when the has no match for the search anymore :-(

Edit (24/04/2015) Alternative solution may be to use facets search for the translations. But I am not really sure how to use them. If you think it's a better solution, please let me know :-)


Source: (StackOverflow)

What steps are involved when adding Globalize to my Rails Project

I have a rails project that needs to be multi-lingual. I ran into a few issues while enabling globalize for my rails project.

I was wondering if there is a thorough checklist / recipe for enabling internationalisation for a rails app.

Please note that I use ActiveAdmin which needs to be part of this as well.


Source: (StackOverflow)

jquery globalize convert bulgerian shortdate pattern date string to date object

I have bulgarian short date pattern string from calendar control how can I create date object without removing 'г.' from it.

new Date('26.6.2015 г.')

I wanted to compare the date selected from calender with today's date

 if (($.datepicker.formatDate('yy/mm/dd', new Date()))
     ($.datepicker.formatDate('yy/mm/dd', new Date('26.6.2015 г.'))))

           { alert('success')}

Source: (StackOverflow)

How to display all translation's fields in one form with globalize and rails 4

How am I supposed to write the forms for my models where I'm using rails 4 and https://github.com/globalize/globalize for translations. i want to display all translations in one form like below example. i have found a solution here https://github.com/rilla/batch_translations but i don't know how do i implement this. is this "batch translation" a gem or what? and how can i install it.

<h1>Editing post</h1> 

   <% form_for(@post) do |f| %>
     <%= f.error_messages %>

     <h2>English (default locale)</h2>
     <p><%= f.text_field :title %></p>
     <p><%= f.text_field :teaser %></p>
     <p><%= f.text_field :body %></p>

     <hr/>

     <h2>Spanish translation</h2>
     <% f.globalize_fields_for :es do |g| %>
       <p><%= g.text_field :title %></p>
       <p><%= g.text_field :teaser %></p>
       <p><%= g.text_field :body %></p>
     <% end %>

     <hr/>

     <h2>French translation</h2>
     <% f.globalize_fields_for :fr do |g| %>
       <p><%= g.text_field :title %></p>
       <p><%= g.text_field :teaser %></p>
       <p><%= g.text_field :body %></p>
     <% end %>

   <% end %>

Source: (StackOverflow)

gem globalize3 on rails 4

Someon are using globalize3 with rails4. How you configure it?

i tryed to install, on my Gemfile:

gem 'globalize3', github: 'svenfuchs/globalize3', branch: 'rails4'

but i have these errors:

Bundler could not find compatible versions for gem "railties":
  In Gemfile:
    globalize3 (>= 0) ruby depends on
      railties (~> 3.0) ruby

    coffee-rails (~> 4.0.0) ruby depends on
      railties (4.0.0)

Bundler could not find compatible versions for gem "rails":
  In Gemfile:
    globalize3 (>= 0) ruby depends on
      rails (~> 3) ruby

    rails (4.0.0)

Bundler could not find compatible versions for gem "actionpack":
  In Gemfile:
    rails (= 4.0.0) ruby depends on
      actionpack (= 4.0.0) ruby

    simple_form (~> 3.0.0.rc) ruby depends on
      actionpack (4.0.0.rc1)

Source: (StackOverflow)

Globalize gem and Rails 4 strong parameters

I am using rails 4.0.2 and globalize 4.0.0.alpha.3, but i can not get the thing to write the data into the translation databases with a list of strong parameters.

I have an offer model and a concern (OfferTranslationConcern)

    class Offer < ActiveRecord::Base
      include OfferTranslationConcern
    end

The concern

    module OfferTranslationConcern
      extend ActiveSupport::Concern

      included do
        attr_accessor :attribute_translations

        translates :name, :city, :includes, :notes, :description, :slug
      end 
    end

The controller

    def update
      respond_to do |format|
        if @offer.update(offer_params)
          format.html { redirect_to @offer, notice: 'Offer was successfully updated.' }
          format.json { head :no_content }
        else
          format.html { render action: 'edit' }
          format.json { render json: @offer.errors, status: :unprocessable_entity }
        end
      end
    end       

And the definition of the strong parameters

    params.require(:user).permit('a lot of offer parameters', :attribute_translations => [:id, :name, :city, :includes, :notes, :description, :slug]
    )

For the translations I am using for example the spanish and italian language (it and es). When I update the offer I get Unpermitted parameters: it, es

The parameters looks like this:

    "offer"=>{"attribute_translations"=>{"it"=>{"name"=>"dsfdsf", "city"=>"sdf", "includes"=>"sdfsdf", "notes"=>"sdfsd", "description"=>"fsdf"}, "es"=>{"name"=>"", "city"=>"", "includes"=>"", "notes"=>"", "description"=>""}}, "provider_id"=>"1",...a bunch of other stuff

Right now I made it work with this definition of strong parameters

    def offer_params
      params.require(:offer).permit! 
    end 

This work, but I don't think this is the best way. So, my question is if there is a way to define the list of parameters and make this work?


Source: (StackOverflow)

Rails and Globalize - join translation table of related model in scope

I want to order some records of a model that has a relation to another model (with translated attributes). Here an example:

  1. I have a model Project
  2. I have a model Task
  3. I have the relation Project has_many Tasks
  4. The model Task has attribute name globalized (on task_translations table)

Now, I want to order all projects by its tasks name. How can I write this scope? How can I join the translation table in Rails like method with_translation in gem globalize (https://github.com/globalize/globalize/blob/eccb924ac9641b52399f22525b0e3ec004739f4c/lib/globalize/active_record/class_methods.rb) but from related object Project?

> Project.all.joins(:tasks) ... (how to include task translation table) ...

Source: (StackOverflow)

rails 4, globalize gem, how to setting default locale

I'm using the globalize gem to support multiple locales.

config/locales directory have three file

en.yml,  ja.yml, ch-HK.yml

and my routing file is

....
root :to => 'main#index', :locale => :en
 scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
   namespace(:api){ resources :badges }
   namespace(:api){
     resources :events do
       get :list, :on => :collection
     end 
   }   
end 
....

my root path (/) is setting default locales

but I don't know how to setting default locales in api_controller

I wanna

/api/events.json ->> /en/api/event.json
/ja/api/events.json ->> /ja/api/events.json

is it not accept in rails Project?

must add /:locales/api~~ ?


Source: (StackOverflow)

Rails 4 and globalize: fields for different languages at the same time

I have a multilingual website. For translations I use globalize gem.

I want to give an user two text fields on the same page. User can fill one of them, or both. I want to save records for all locales on submit button clicked.

Say, there are two text fields named username. I can type Ivan for English one, and Иван for Russian one. When I click Submit button, I want to see two records in my user_translations table for en and ru respectively.

What's the most elegant way to get that worked?

ps: batch_translations seems to be outdated.


Source: (StackOverflow)

Rails switch from translated URL

I have configured I18n, Globalize and FriendlyId in a Multilingual Rails Application. Moreover, I pretend to translate url according locale.

For example:

http://localhost:3000/es/micontroller/mi-casa   
http://localhost:3000/en/mycontroller/my-house  

Those urls already exists, the translation works as expected.

These are the language switcher links I have added:

= link_to_unless I18n.locale == :es, "Español", locale: :es  
= link_to_unless I18n.locale == :en, "English", locale: :en

My issue is that when I switch languages, the url only changes the locale parameter, instead of changing slug.

For example, toggling from English to Spanish results in something like:

http://localhost:3000/es/mycontroller/my-house

PD: Is a good practice what I pretend to do with my urls? I have searched some time with no results.


Source: (StackOverflow)

Globalize & globalize-accessors single form translations - throwing error when updating attributes

I wanted to make a form for creating and updating a model, with support for adding/editing translations for multiple languages inline.

This is how my form looks like:

<%= form_for([:admin, @category], remote: true) do |f| %>
  <% Category.globalize_attribute_names.each do |lang| %>
    <div class="field">
      <%= f.label lang %><br>
      <%= f.text_field lang %>
    </div>
  <% end %>

 <div class="actions">
   <%= f.submit %>
  </div>
<% end %>

Strangely, everything works fine when I`m creating a model. But when I try to edit it I get this error:

undefined method `name_en' for #Category::Translation:0x007f3ce896afd8>

I said strangely, because it works fine in the console. When i type:

Category.friendly.find("men-shoes").name_en 

everything works fine. (I`m using friendlyID gem).

I have no idea what the problem could be. I even don`t know why is it fetching Category::Translation, should`n it be just Category?

Thanks


Source: (StackOverflow)