I am writing a site with more than one language, is there any easy way or technique to reduct the workload of changing which text to another language.I have an idea, but I don't know whether it is suitable or easy enough. I create a XML that contain all the text in my web, when the user change their language, my program will base on the language the user choose, and get the suitable tags from the XML, and fill in the page. What do you think? or maybe is there any more easy way to do?
(Assuming I am using RoR, if suggest any gems.)
Check out Rails Internationalization (I18n) API:
The Ruby I18n (shorthand for
internationalization) gem which is
shipped with Ruby on Rails (starting
from Rails 2.2) provides an
easy-to-use and extensible framework
for translating your application to a
single custom language other than
English or for providing
multi-language support in your
application.
I wrote a blog post about "Scoping By Locales". It suggests to put all your content in the database and when you want to fetch it, use the I18n API to set the user's locale to be their language and the fetcher will default to that language.
Related
We need to start delivering dynamic content in multiple languages with our different apps.
There are two types of localisation that should be considered: UI localisation and Content localisation. UI localisation is the language the interface is delivered in (static text) while Content localisation is the language content is viewed/edited (Rails model)
We basically have 3 type of apps
Mobile apps - hits an API endpoints for CRUD operations
Client Web app - built with backbone.js so also using API endpoints for CRUD operations via backbone models/collections
Admin web app - built using activeadmin (https://github.com/activeadmin/activeadmin)
I should note we are using Rails 3.2, ActiveAdmin 0.6.6 (upgrading needed but not priority :))
To localise the content, we are looking the globalize gem (https://github.com/globalize/globalize)
There doesn't seem to be any issues using globalize when interfacing with the API endpoints. We can use the Accept-Language header and set the I18n.locale property in a before_filter and everything seems to work fine. This means the Mobile apps and Client Web app should be covered.
For ActiveAdmin, this becomes a bit more difficult. For the ActiveAdmin app, we would like to deliver the UI static text in the user's preferred language while allowing them to switch between languages for the content.
The globalize gem leverages the I18n.locale property to determine which locale to update/read for the content translations. Since ActiveAdmin is not using AJAX, setting I18n.locale will also affect the UI static text.
At the moment, this is not a big deal as we only have one locale for the UI (config/locales only contain en yml files). This means we can use the method described at https://github.com/activeadmin/activeadmin/wiki/Specifying-locale and set the I18n.locale property to update the correct dynamic content, via globalize, while the UI interface will continue to be delivered in English
However, thinking to the future, where we potentially add additional languages for the UI, I'm looking for ideas of how to keep the UI locale and the content locale separate. Other languages, such as Java and C#, do this with different properties on their I18n equivalent library but Rails doesn't seem to embrace this
Thoughts?
Thanks in advance
after further investigation, I ended up using activeadmin-globalize gem. Initially, I didn't think it was compatible with the globalize gem based on the documentation but it is
Hello everyone :D I've just started learning Rails and currently I have one concern.
I am building a Rails web site which needs to be translated to 4 languages. What would be the most practical and convenient method to do it?
I've read the main goal would be to make separate folders for each language and copy all the views for each language. But I would still have notice messages on English inside my controller so how would I handle that. Routes are also my concern. Should I have 4 different routes for 4 different translated Views.
What do you recommend to handle this problem? I didn't find anything concrete online.
Thank you for your suggestions!
for your notice messages you can do
def create
if user.save
flash[:notice] = t(:user_was_successfully_created)
redirect_to users_users_path
else
render :new
end
end
you should not have 4 different routs
Rails Internationalization (I18n) API
take a look at this link http://guides.rubyonrails.org/i18n.html
Rails Internationalization (I18n) API
The Ruby I18n (shorthand for internationalization) gem which is shipped with Ruby on Rails (starting from Rails 2.2) provides an easy-to-use and extensible framework for translating your application to a single custom language other than English or for providing multi-language support in your application.
The process of "internationalization" usually means to abstract all strings and other locale specific bits (such as date or currency formats) out of your application. The process of "localization" means to provide translations and localized formats for these bits.1
So, in the process of internationalizing your Rails application you have to:
Ensure you have support for i18n.
Tell Rails where to find locale dictionaries.
Tell Rails how to set, preserve and switch locales.
In the process of localizing your application you'll probably want to do the following three things:
Replace or supplement Rails' default locale — e.g. date and time formats, month names, Active Record model names, etc.
Abstract strings in your application into keyed dictionaries — e.g. flash messages, static text in your views, etc.
Store the resulting dictionaries somewhere.
This guide will walk you through the I18n API and contains a tutorial on how to internationalize a Rails application from the start.
After reading this guide, you will know:
I want to copy some specific content from a website using ruby/rails.
The content I need is inside a marquee html tag, divided by divs.
How can I get access to this content using ruby?
To be more precise - I want to use some kind of ruby gui (Preferably shoes).
How do I do it?
This isn't really a Rails question. It's something you'd do using Ruby, then possibly display using Rails, or Sinatra or Padrino - pick your poison.
There are several different HTTP clients you can use:
Open-URI comes with Ruby and is the easiest. Net::HTTP comes with Ruby and is the standard toolbox, but it's lower-level so you'd have to do more work. HTTPClient and Typhoeus+Hydra are capable of threading and have both high-level and low-level interfaces.
I recommend using Nokogiri to parse the returned HTML. It's very full-featured and robust.
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('http://www.example.com'))
puts doc.to_html
If you need to navigate through login screens or fill in forms before you get to the page you need to parse, then I'd recommend looking at Mechanize. It relies on Nokogiri internally so you can ask it for a Nokogiri document and parse away once Mechanize retrieves the desired URL.
If you need to deal with Dynamic HTML, then look into the various WATIR tools. They drive various web browsers then let you access the content as seen by the browser.
Once you have the content or data you want, you can "repurpose" it into text inside a Rails page.
If I'm to understand correctly, you want a GUI interface to a website scraper. If that's so, you might have to build one yourself.
The easiest way to scrape a website is using nokogiri or mechanize gems. Basically, you will give those libraries the address of the website and then use their XPath capabilities to select the text out of the DOM.
https://github.com/sparklemotion/nokogiri
https://github.com/sparklemotion/mechanize (for the documentation)
While I have experience developing Rails apps in English, I am a blank slate when it comes to handling globalization, so please don't shoot me in the head if my question 'doesn't make sense' :)
I have been asked to add multi language feature to a part of a Rails app that I am working on. Initially its only going to be 2 languages, French and German. The content that will be translated (which is in English now) is rendered using partials at the moment hence I am getting a bit inclined on creating partials with different languages and then based on the users language selection call the relevant partial - Would you recommend this approach?
Although it seems a heavy weight solution for this particular purpose, but I am also looking at the Rails Globalize plugin. This seems to appeal if I look at the long term gains, something like what if later I am asked to translate the entire app.
Any insights on what would be a proper structured approach to handle globalization in Rails?
Many Thanks
Have you had a look at the i18n (internationalization) API that is in Rails itself as of 2.2? It makes it easy to store your language translation files as .yml files, so a fr.yml and a de.yml or whatever. It also steps through different approaches of making languages accessible via browser prefs, or URLs, subdomains, etc. In your HTML template files you use symbols to specify the keys for the string you've translated. At least in my basic usage and testing, it was very easy to work with.
I preffer using translator instead of Rail's i18n, since the former handles "scoping of translation" automatically. I recommend you give it a look.
This is a great article on how to use Ruby's GetText to localise you Rails App.
I'm creating a fast application with ruby on rails, and after doing a lot of scaffolding and validation I'm very happy with some of the features that are embeded in RoR... but I live in Mexico and all my users would love the application to be in Spanish of course. So, I noticed theres a lot of functions that write actual text in English for example time_ago_in_words and all the errors produced by the scaffolding and validation.
Before actually doing those things on my own (like I would in php for example) I wanted to know if theres some kind of language file I could edit (or even download one in Spanish). After all, the books I'm reading and the tutorials (and webcast) I'm following to learn this new framework are all in English (and fail to include this problem).
Here you can find a nice sample app which demonstrates some basics of i18n (internationalization features) in Rails.
This guide contains the most recent work on internationalization. A pretty decent reference:
http://guides.rails.info/i18n.html