Faker generate english sentences in ruby on rails - ruby-on-rails

While using Faker with rails, it generates sentences in some other language. I want them to be generated in English. What kind of settings do I need to change to do it?

Docs say
Just set Faker::Config.locale to the locale you want, and Faker will take care of the rest.
https://github.com/stympy/faker/blob/master/README.md
https://github.com/stympy/faker/tree/master/lib/locales
Faker::Config.locale = :en

Related

How can I see translations for the specific language in rails console? Using I18n?

I can see translations for the default language using the Rails console using this code:
I18n.t('views.signup.company_info')
But how can I see translations for the german language?
For displaying the translation for the specific language we need to set the locale:
I18n.t('views.signup.company_info', locale: :de)
I18n.with_locale(:de) {I18n.t('views.signup.company_info')}
If you want to use a specific language for translation in a code block you can do this:
I18n.with_locale(locale) do
your_code
end

Add translation into i18n locales of Rails

I want to create a simple form that adds translations dynamically into config/locales/en.rb. There's a way to do this? I'll need something like a parser to solve this?
With using Globalize3 gem given here on stack-overflow answer Add translation to I18N dynamically
Rails cast link:
http://railscasts.com/episodes/338-globalize3?view=asciicast

Rails 3 available_locales excluding gem translations

I have a Rails 3 app in which I'm using I18n.available_locales to display a list of available languages for user accounts. In my config/locales directory I only have en and nl yml files, however, I18n.available_locales returns a variety of other locale keys which I'm assuming are present due to translations being present in the gems that I'm using. Is there a common/standard way to get the method to only return the translations present in the app itself?
I would suggest, you use this gem : rails-i18n , it provides basic translations for every languages, and then, restrict available locales with
config.i18n.available_locales = ['es-CO', :de]
Of course, rails-i18n is not mandatory here, you could simply define available_locales in your config/application.rb file
And as said in the comments, you should definitely accept some answers to improve your accept_rate

Internationalization Best Practices / Rails App

I am new to ruby & rails and have started building an application.
My goal is to build this in a way I can easily translate the contents of the rails app and display the website contents in a locale preferred by registered user.
Appreciate any inputs on some of the best practices or references to any documentation to read, to build a web application that can be easily translated?
Thanks,
Krish.
Check out the Rails Internationalization (I18n) API. It does everything you've described.
Also check out Globalize3, it became a standard for model translations. Very useful.
You can use ready_for_i18n plugin that convert your erb to desired form.It saves some time.
You'd definetely watch this talk: http://www.youtube.com/watch?v=CTu4iHWGDyE
Here are some tips or best practices I noted while working through Internationalization of a Rails app. (It's possible that some of them are now outdated).
Before I get into a list here's some clarification between localization and translation that was helpful to me:
Definitions:
App localization and model translations are separate concerns. Figure out which one of those (or both) it is, that you need.
The way I use them here:
App localization: Localization your app to a locale.
Model translation: Translation your model/data into a language.
Example: Give me the french translation (model translation) of the resource in my Spanish site (app localization)
I use Globalize for model translations.
Helpful Tips/ Best Practices:
Some of the following are clearly just helpful tips while working in the Context of Rails + Globalize, some of them might be more than that... possibly best practices.
I18n.locale refers to and sets the locale of the app. When using Globalize, Globalize.locale refers to and sets the locale for model/data translations.
Set both I18n.locale and Globalize.locale on every request. Since these variables are set in Thread, it will avoid some hard-to-replicate bugs.
Set Globalize.locale to I18n.locale right after setting I18n.locale. This allows for model translations to default to the locale of the app. (On my Spanish site, I expect data to be in Spanish, by default).
Change Globalize.locale (and notI18n.locale) to change model translation.
Reset I18n.locale and Globalize.locale after every test. In rspec
RSpec.configure do |config|
config.after(:each) do
I18n.locale = :en
Globalize.locale = :en
end
end
Either use subdomain, or subfolder in the url to refer to the locale of the app.
Use a language parameter to specify the language of the data.
When you work on your rails app, you will probably use default_url_options to use I18n.locale as the default locale parameter for your route / path helpers. However this doesn't work in tests. I picked up a solution from this github issue on rspec-rails.
I monkey patch ActionDispatch::Routing::RouteSet like so:
class ActionDispatch::Routing::RouteSet
def url_for_with_locale_fix(options={})
url_for_without_locale_fix(options.merge(:locale => I18n.locale))
end
alias_method_chain :url_for, :locale_fix
end
Set up a translate helper t as a wrapper around the I18n.t method
module I18nHelper
def t string, options = {}
I18n.t string, options
end
end
RSpec.configure do |config|
config.include I18nHelper
end
This is mini-pattern I use. You can check it out: http://developers-note.blogspot.com/2012/01/rails-i18n-good-practice.html

How to manage Rails 2.3.x i18n locales programatically?

I would like to know if there is a way to insert, alter and remove i18n locale keys programatically (I guess I could use the DB, but I like Rails i18n and want to stay as close to it as possible).
Basically I want to know if there's a way (native, gem, plugin, whatever) to do things like:
I18n.add_locale_key("en", "application.messages.submit_message", "Submit message!")
I18n.add_locale_key("es", "application.messages.submit_message", "Enviar mensaje!")
I18n.remove_locale_key("en", "application.messages.submit_message")
I18n.remove_locale_key("es", "application.messages.submit_message")
As packaged, the Rails I18n API only support defining locale terms via the local .yaml or .rb files. Short of dynamically editing those files at runtime, your best bet is to use the DB functionality of a gem gem like FastGetText.
You could also roll your own solution, of course, but the DB method will likely work for your use case and will result in a smaller time investment.
Here's one way to do it:
>> I18n.backend.store_translations :en, :hello_world => "Hello, world."
=> {:hello_world=>"Hello, world."}
>> I18n.t :hello_world
=> "Hello, world."

Resources