How to translate javascript strings in Rails – .js localization

Hi there!

Today i have achieved the configuration of a great gem that will help you to translate your javascript strings.

It is called https://github.com/fnando/i18n-js

I was following the instructions but a couple of things did not work. Both in application.rb

1. Library

In the instructions it says to add the following line:

config.middleware.use I18n::JS::Middleware

But in my case it did not work. I searched and found this alternative:

config.middleware.use SimplesIdeias::I18n::Middleware

(Thanks http://stackoverflow.com/users/2933134/halogen for the hint here: http://stackoverflow.com/questions/20724971/uninitialized-constant-i18njs-ruby-on-rails

2. Configuration

I had to change this line

config.assets.initialize_on_precompile = false

and set it to true

config.assets.initialize_on_precompile = true

3. Inform js of the current locale
Another important thing I had to do was to add a couple of lines to the application.html.erb


I18n.defaultLocale = "";
I18n.locale = "";

And then it started working nicely.

As an example, in the .js file I put

var alertMsg = I18n.t('posts.alert');

And that corresponds to


en:
posts:
alert: "Alert message"

How this works internally I am not sure. I have realised that the key file is translations.js that is automatically created under public/assets

Translations.js gets automatically updated, and you can also update it manually with:

$ rake i18n:js:export

I have also evidenced that the Rails cache can spoil the party. So if you notice erratic behaviour do clear the cache:

rake tmp:clear (I have had similar issues in the past…)

Hope it helped! Thanks to Nando Vieira for the amazing work!

Improve performance and speed in Apache

We have given some ideas about how to gain performance in some Apache tasks such as redirection.

In this post, I would like to point out some basic ideas about improving performance with an Apache server. I will refer to version 2.2. running in Ubuntu server 12.04 32 bits.

1. Activate the Apache Cache

Apache has several modules that provide cache services. This guide explains a little bit the modules available: http://httpd.apache.org/docs/2.2/caching.html.

And DigitalOcean provides a more practical look at it with straightforward tips https://www.digitalocean.com/community/articles/how-to-configure-content-caching-using-apache-modules-on-a-vps

So the simplest approach is to enable the module:

sudo nano /etc/apache2/mods-available/mem_cache.conf

where you will see the configuration. I have not changed nothing for the moment.

And then, make it run:

sudo a2enmod mem_cache

service apache2 restart

This part so far is covering the Memory Cache. I will not talk about the Disk Cache, since I am using the WordPress SuperCache plugin and would not like to have a conflict between both.

2. Allow Override none Directive

If you have set “Allow Override none” then Apache will look in every folder to find an .htaccess file. But it is most likely that you only have one .htaccess file, and not a whole cascade of .htaccess files, so, why not put it in the configuration file and save time? the benchmark shown by the excellent post by ESchrade http://www.eschrade.com/page/why-you-should-not-use-htaccess-allowoverride-all-in-production/ is very convincing. This idea is also reinforced by this post https://www.tinywp.in/move-htaccess-rule-to-apache-conf/

This action is also recommended in this great post with tips for improving Apache speed https://servercheck.in/blog/3-small-tweaks-make-apache-fly

3. Use third party cache software

If you are using WordPress, it is highly recommended to use the WordPress SuperCache Plugin. More info here http://ocaoimh.ie/wp-super-cache/

There is also a more generic option, called Varnish, https://www.varnish-cache.org/, and also a nice configuration guide by Donncha Ó Caoimh http://ocaoimh.ie/2011/08/09/speed-up-wordpress-with-apache-and-varnish/.

4. Offload unused modules

This is thoroughly explained here https://www.digitalocean.com/community/articles/how-to-optimize-apache-web-server-performance. As well as other great ideas such as

5. Reduce number of connections

Same link as above.

6. Set the right number of MaxClients

In short, this is the formula that you have to use to calculate the number of MaxClients:

(Total RAM – RAM used for Linux, MySQL, etc.) / Average httpd process size

There is a script here that will help you to get this for your server: http://cloudinservice.com/tune-apache-performance-using-mpm-prefork-module/

More details on this can be found in this link: https://servercheck.in/blog/3-small-tweaks-make-apache-fly

7. KeepAlive On or KeepAlive Off?

This is not an easy one, I have turned it to KeepAlive Off, and the server works much better now. If you want a detail explanation check out:

https://servercheck.in/blog/3-small-tweaks-make-apache-fly

http://abdussamad.com/archives/169-Apache-optimization:-KeepAlive-On-or-Off.html

8. Define Spare Servers

In this post there is a useful guide for that: http://cloudinservice.com/tune-apache-performance-using-mpm-prefork-module/

******

Do you know of other ways of improving Apache performance? Please get in touch with us and help us complete this guide!

WordPress SuperCache and Apache, PHP APC problems

Yes it is true, there is some problem with APC cache PHP module and WordPress SuperCache plugin.

I have experienced those random blank screens with an error 500, just like it is described here:

http://askubuntu.com/questions/141734/incompatibility-between-wp-super-cache-and-php-apc

My current configuration is:

Ubuntu 12.04 32 bits
PHP 5.3.1
Apache 2.2.2
WordPress 3.8
WordPress SuperCache 1.3.1

The issue has been identified by the plugin dev team: https://wordpress.org/plugins/wp-super-cache/faq/

The “white screen of death” or a blank page when you visit your site is almost always caused by a PHP error but it may also be caused by APC. Disable that PHP extension if you have trouble and replace with eAccelerator or Xcache.

In order to disable the APC module:

sudo nano /etc/php5/apache2/conf.d/apc.ini

and comment the line

;extension=apc.so

Then restart apache

sudo service apache2 restart

More about Rails cache, the asset pipeline and javascript

I have installed a gem that assigns a javascript variable with a dynamic ruby (rails) variable, in this case it is the language locale I18n.locale.

Because the javascript files are precompiled and cached, I see that the variable is not being properly updated. So I have studied a bit how the cache works to see what is the best way to work around it.

1. Identifying the issue
Look for the cached javascript text in the files on your local development environment, I have found them in the tmp file

You can clear those with

rake tmp:clear

2. How to solve it
I have found good hints in this articles:

http://technicalpickles.com/posts/some-tips-for-caching-javascript-and-css-in-rails/

http://stackoverflow.com/questions/15378018/prevent-rails-3-2-11-asset-pipeline-from-caching

I need to put and exception and say “do not cache this file”