Localizing timeAgoInWords in CakePHP
Cake's messages are extracted into a different domain, in this case, the cake
domain. This means that cake messages will not be extracted into your default.pot
file, but will go into cake.pot
file.
Curiously, cake.pot doesn't seem to be included in the download, nor does the i18n shell allow you to pass a param to include the cake core during extraction. However, it is still easily done (my comments start with a #
):
$:/var/www/path/app$ cake i18n extract
# if will ask you here if you want to extract from your app folder
# simply press enter to confirm
What is the path you would like to extract?
[Q]uit [D]one
[/var/www/path/app/]
# now it will ask you again, in this case enter the cake path
What is the path you would like to extract?
[Q]uit [D]one
[D] > /var/www/path/lib/Cake
# third time, just press enter
What is the path you would like to extract?
[Q]uit [D]one
[D] >
# press enter to accept the app/Locale
What is the path you would like to output?
[Q]uit
[/var/www/path/app//Locale] >
# press enter to keep translation domains deparate
Would you like to merge all domains strings into the default.pot file? (y/n)
[n] >
Now wait for the extraction to finish and enjoy the pain of translating ;)
time_ago_in_words and localize
If you are using Rails > 2.2, the helper is already locale-aware.
Just download the right localization file from the locale repository and store it into your /config/locales
path. Then set your locale preferences.
CakePHP 2.4 and Timehelper's timeAgoInWords locale
Normally I change current language like this:
$this->Session->write('Config.language', $lang);
I set default language like this in every request in core.php
:
Configure::write('Config.language', 'tur');
Regarding to CakePHP documents I must add this code to my AppController:
class AppController extends Controller {
public function beforeFilter() {
if ($this->Session->check('Config.language')) {
Configure::write('Config.language', $this->Session->read('Config.language'));
}
}
}
After adding the code above to beforeFilter() of AppController, I refreshed /tmp/cache/persistent/
folder and everything works fine.
Edit:
This solution worked well for english but it didn't work for other languages.
Why ? As noted in this question cake looks for cake.po
files inside your Locale folder.
For example if you want to see cake's messages in french, so you need to create this file:
/app/Locale/fra/LC_MESSAGES/cake.po
Where do CakePHP use cake.po records ?
For example: this method
You can create default po files like this: i18n shell tutorial
After shell's file creation, you can edit cake.po file for your language.
For some languages cake.po files are ready. For example: french file
You can find rest from there: https://github.com/cakephp/localized
CakePHP 2.4 and Timehelper's timeAgoInWords locale
Normally I change current language like this:
$this->Session->write('Config.language', $lang);
I set default language like this in every request in core.php
:
Configure::write('Config.language', 'tur');
Regarding to CakePHP documents I must add this code to my AppController:
class AppController extends Controller {
public function beforeFilter() {
if ($this->Session->check('Config.language')) {
Configure::write('Config.language', $this->Session->read('Config.language'));
}
}
}
After adding the code above to beforeFilter() of AppController, I refreshed /tmp/cache/persistent/
folder and everything works fine.
Edit:
This solution worked well for english but it didn't work for other languages.
Why ? As noted in this question cake looks for cake.po
files inside your Locale folder.
For example if you want to see cake's messages in french, so you need to create this file:
/app/Locale/fra/LC_MESSAGES/cake.po
Where do CakePHP use cake.po records ?
For example: this method
You can create default po files like this: i18n shell tutorial
After shell's file creation, you can edit cake.po file for your language.
For some languages cake.po files are ready. For example: french file
You can find rest from there: https://github.com/cakephp/localized
Cakephp understanding Localization..?
All this code will do is edit the $this->viewPath
variable.
This will make CakePHP look in a different directory from the standard when calling render()
. This is normally done if you intend to create different folders, each of which contains the View files for a specific localisation.
If your View folder currently looks like this:
View
- Elements
- Emails
- Errors
- Helper
- ...
It should, instead, look like this:
View
- deu
- Elements
- Emails
- Errors
- Helper
- ...
- Elements
- Emails
- Errors
- Helper
- ...
In this way, you can specify entirely different View files for lots of different localisations. The folders in the root directory should be the default localisation.
The reason it currently isn't doing anything is because it can't find the relevant view files when it makes the file_exists()
check. Once you have restructured as necessary it should work fine.
How to translate words from $this- Time- timeAgoInWords?
Yes, they are in a different domain, the cake
domain, the months are in that domain too. Take a look at the source, all translatable core messages are in that domain.
I'd suggest that you extract them using the I18N shell (backup your existing translations first in case you accidently overwrite them), it won't get simpler than that.
See Cookbook Console & Shells > I18N Shell > Generating POT Files
Related Topics
How to Start the Ruby Debugger on Exception
Using the Value of a Variable as Another Variables Name in Ruby
Heroku: How to Push Seeds.Rb to Existing Rails App
Spork 0.9.2 and Rspec 3.0.0 = Uninitialized Constant Rspec::Core::Commandline (Nameerror)
Vi Input Mode in Command Line Matlab
Differencebetween "Be_True" and "Be True" in Rspec
Rails: How to Get a File Extension/Postfix Based on the Mime Type
Creating a Hash with Values as Arrays and Default Value as Empty Array
Are There Better Ways to Prevent 'Yield' When No Block Is Passed In
Missing a Template for This Request Format and Variant
Rails Before_Filter for Specific Actions in Controller
Accepts_Nested_Attributes_For Rails 4 Is Not Deleting
Verb-Agnostic Matching in Sinatra
How to Run Ruby 2.0 with Jruby 1.7
What Is a Worker in Ruby/Rails
How to Force One Field in Ruby's CSV Output to Be Wrapped with Double-Quotes
Ruby CSV Parsing String with Escaped Quotes
Webkit_Server Hangs Periodically When Run from Capybara in Ruby