Friday, July 24, 2009

http://www.youtube.com/watch?v=5_ZPSfXWsDw&feature=PlayList&p=B6901E61C4196551&playnext=1&playnext_from=PL&index=44

little champs in commedy

Musharraf announces team of legal experts to defend

The former President General (rtd) Pervez Musharraf constituted a committee of lawyers to defend him in cases filed against him at the Supreme Court, a private TV reported Friday. The legal committee of the former president will be headed by Abdul Hafeez Pirzadah with Khalid Ranjha, Malik Abdul Qayyum and Chaudhry Fawwad as committee members. Talking to Chaudhry Fawwad by phone from London, Pervez Musharraf said he did everything during his era for national interest. The former President is quoted saying, ‘I saw good time, will see bad times now.’ It should be noted that the Supreme Court issued summon notice to the former president directing him to appear before court on July 29 in cases relating judges’ appointment and emergency.

Thursday, July 23, 2009

the top to bottom persistent internationalization tutorial

For some developers, allowing a website to support multiple languages is essential. Luckily cakePHP 1.2 has the foundations available to make this possible.
Before forging ahead, I'd like a disclaimer. I don't claim this tutorial to be uniquely mine, it's an amalgamation of techniques from several pages and sites. Neither is it the most comprehensive and in-depth guide.

That said, I certainly hope after using this guide you can quickly and easily implement multiple languages in your cake app without needing to skip around the place. If I fall short of this and you have suggestions, leave a comment.

Once you complete this tutorial your site will be able to:

1. display multiple languages
2. allow users to switch languages
3. store language settings in cookies, so returning visitors don't need to re-select their preferred language

The sites that I build typically require 3 languages:

1. British English(en-gb)
2. Simplified Chinese(zh-cn)
3. Traditional Chinese(zh-tw)

So throughout this document, I'll be using them as my reference languages. Your site may support more or less languages.
Step 1: Setup the directories for your messages

$ cd cake/app/locale/

$ mkdir en_gb

$ mkdir en_gb/LC_MESSAGES

$ mkdir zh_tw

$ mkdir zh_tw/LC_MESSAGES

$ mkdir zh_cn

$ mkdir zh_cn/LC_MESSAGES

This will create the minimum folders required for each language our site needs to support.

To find your language code(s) refer to http://api.cakephp.org/1.2/l10n_8php-source.html#l00180 Download code //this is only a sample. don't add this to your code.
'nl' => array(
'language' => 'Dutch (Standard)',
'locale' => 'dut',
'localeFallback' => 'dut',
'charset' => 'utf-8'
),
'pl' => array(
'language' => 'Polish',
'locale' => 'pol',
'localeFallback' => 'pol',
'charset' => 'utf-8'
),
'sk' => array(
'language' => 'Slovack',
'locale' => 'slo',
'localeFallback' => 'slo',
'charset' => 'utf-8'
),
By studying the sample above, we can see that directory names are actually the locale:

* 'dut' is the correct directory name(locale) for 'nl'
* 'pol' is the correct directory name(locale) for 'pl'
* 'slo' is the correct directory name(locale) for 'sk'

Step 2: Write some strings to translate.
View Template:
Download code pageTitle = __('pageTitle_home', true); ?>







note: The 2nd parameter controls whether a message should be returned or echo'd
So when you are working with a template, use:
PHP Snippet:
Download code And when you are working with code, use:
PHP Snippet:
Download code
Step 3: Let's build a database from our PHP and templates.

$ cd cake/app/

$ cake extract

This will recursively go through all the folders and check both your .php and .ctp files for all of those __() functions you typed. Once it's complete, you should have a nice message template file named default.pot file inside cake/app/locale/

So let's copy this message template file into the right directories for each language.

$ cd cake/app/locale/

$ cp default.pot locale/en_gb/LC_MESSAGES/default.po

$ cp default.pot locale/zh_tw/LC_MESSAGES/default.po

$ cp default.pot locale/zh_cn/LC_MESSAGES/default.po


note: at this point in time, you can freely edit the default.po files(they're just text) and start translating strings. Changes made to these files will automatically be rendered in your views.

Here are some short snippets from my default.po files.

Download code // locale/zh_cn/LC_MESSAGES/default.po
msgid "footer_copyright"
msgstr "??? � 2007. ????"

// locale/zh_tw/LC_MESSAGES/default.po
msgid "footer_copyright"
msgstr "??? � 2007. ????"

// locale/en_gb/LC_MESSAGES/default.po
msgid "footer_copyright"
msgstr "Education Bureau � 2007. All rights reserved."
Step 4: Change the default language
A fresh install of cakePHP is set to use American English, so for the rest of us: we need that changed.
Download code // config/bootstrap.php
define(DEFAULT_LANGUAGE, 'zh-tw');
Step 5: Let users change the language
Component Class:
Download code class P28nComponent extends Object {
var $components = array('Session', 'Cookie');

function startup() {
if (!$this->Session->check('Config.language')) {
$this->change(($this->Cookie->read('lang') ? $this->Cookie->read('lang') : DEFAULT_LANGUAGE));
}
}

function change($lang = null) {
if (!empty($lang)) {
$this->Session->write('Config.language', $lang);
$this->Cookie->write('lang', $lang, null, '+350 day');
}
}
}
?> Thanks Nasko for pointing out that Cookie->write() does not accept timestamps
Controller Class:
Download code class P28nController extends AppController {
var $name = 'P28n';
var $uses = null;
var $components = array('P28n');

function change($lang = null) {
$this->P28n->change($lang);

$this->redirect($this->referer(null, true));
}

function shuntRequest() {
$this->P28n->change($this->params['lang']);

$args = func_get_args();
$this->redirect("/" . implode("/", $args));
}
}
?>
Controller Class:
Download code //app_controller.php
class AppController extends Controller {
var $components = array('P28n');
}
?> The final piece of code, are some custom routes that need to be added to cake/app/config/routes.php
Download code //route to switch locale
Router::connect('/lang/*', array('controller' => 'p28n', 'action' => 'change'));

//forgiving routes that allow users to change the lang of any page
Router::connect('/eng?/*', array(
'controller' => "p28n",
'action' => "shuntRequest",
'lang' => 'en-gb'
));

Router::connect('/zh[_-]tw/*', array(
'controller' => "p28n",
'action' => "shuntRequest",
'lang' => 'zh-tw'
));

Router::connect('/zh[_-]cn/*', array(
'controller' => "p28n",
'action' => "shuntRequest",
'lang' => 'zh-cn'
));
?>
Step 6: Links to change language
View Template:
Download code









link($html->image('en_gb.gif'), '/lang/en-gb', null, null, false); ?>
link($html->image('zh_tw.gif'), '/lang/zh-tw', null, null, false); ?>
link($html->image('zh_cn.gif'), '/lang/zh-cn', null, null, false); ?>


link($html->image('en_gb.gif'), '/en-gb/news', null, null, false); ?>
link($html->image('zh_tw.gif'), '/zh-tw/news', null, null, false); ?>
link($html->image('zh_cn.gif'), '/zh-cn/news', null, null, false); ?>

CakePHP URL-based language switching for i18n and l10n

nuts and bolts of cakephp
CakePHP URL-based language switching for i18n and l10n (internationalization and localization)
Posted in CakePHP by teknoid on November 28, 2008

I should preface this post by saying that it does not cover the basics of i18n and l10n so, please, first take a look at the manual on how to get the basics going.

To better understand the goal and why some things were done the way they were, I’ll summarize the requirements:

1. The app has to support two languages or more (in this case English and Russian)
2. Default language is English
3. The language switching is based on a URL param
4. The URL format should be: example.com/eng/controller/action
5. Language choice should persist in the session and a cookie

Just a note here… there are other ways to determine the language requested by the user, for example it could come from a domain name like eng.example.com or rus.example.com. Hopefully the approach outlined here will also be helpful if other methods of language switching are used in your app…

Also, worth while to mention, that having language name in the URL (as opposed to just reading it from the session or cookie) helps with SEO… I won’t bore you here with details, but basically it helps to ensure that a variation of each page, based on the language param in the URL, is properly indexed by the search engines. Thus, each indexed page can be found later in the native language of the user.

Last, but not least, CakePHP uses three letter language name abbreviation, based on this, so I figure, should be fine to use the same in the URL’s.

Alright, so looking at the URL format, instantly raises a question… how do we tack on the language name to the “front” of each URL?

Thankfully the Router accomplishes that pretty easily (in app/config/routes.php):
view plaincopy to clipboardprint?

1. Router::connect('/:language/:controller/:action/*',
2. array(),
3. array('language' => '[a-z]{3}'));

Router::connect('/:language/:controller/:action/*',
array(),
array('language' => '[a-z]{3}'));

It takes a ‘language’ parameter and throws it to the front of the URL, just as we need it.

Now, we need to specify a default language to use, I just add this to my app/config/core.php


Configure::write('Config.language', 'eng');

So, when someone comes to the http://example.com/users/home, the site is displayed in English by default. Then we usually see a link somewhere (with a little flag next to it :)), to switch to another language.

In cake we can make those language-switching links like this:


$html->link('Русский', array('language'=>'rus'));

Notice that we set the language param, which we’ll rely on to do our switching. Providing no other params, will simply reload the current page (in the new language) with the param tacked to the front of the URL (more about this later).

Side note, it’s not a good idea to use the __() translation function on language-switching links… If I get to the site and it’s displayed in the language I can’t understand even remotely, the only savior would be a link in my native language, which indicates that i can switch to it (and well, a little flag would help too :))

So now we actually need the code to switch the language, when a user clicks on the link, like above.

It’s best done in the App Controller, kinda like here:
view plaincopy to clipboardprint?

1. var $components = array('Session', 'Cookie');
2.
3. function beforeFilter() {
4. $this->_setLanguage();
5. }
6.
7. function _setLanguage() {
8.
9. if ($this->Cookie->read('lang') && !$this->Session->check('Config.language')) {
10. $this->Session->write('Config.language', $this->Cookie->read('lang'));
11. }
12. else if (isset($this->params['language']) && ($this->params['language']
13. != $this->Session->read('Config.language'))) {
14.
15. $this->Session->write('Config.language', $this->params['language']);
16. $this->Cookie->write('lang', $this->params['language'], null, '20 days');
17. }
18. }

var $components = array('Session', 'Cookie');

function beforeFilter() {
$this->_setLanguage();
}

function _setLanguage() {

if ($this->Cookie->read('lang') && !$this->Session->check('Config.language')) {
$this->Session->write('Config.language', $this->Cookie->read('lang'));
}
else if (isset($this->params['language']) && ($this->params['language']
!= $this->Session->read('Config.language'))) {

$this->Session->write('Config.language', $this->params['language']);
$this->Cookie->write('lang', $this->params['language'], null, '20 days');
}
}

Let’s take a look at the code quickly and consider some scenarios…

I created a separate method _setLanguage();, the reason I like doing this is that it keeps the beforeFilter() cleaner, which already has enough crap in there usually.
Secondly, it can be overridden in the child controllers, if required.

So let’s consider some user-case scenarios:

1.

The user comes to the site for the very first time

In this case the default language is read from the core.php file, so the site is set to English
2.

The user starts clicking around the site for the very first time in his native English

Nothing really needs to be done, so we can happily skip that part
3.

The user comes to the site and has to switch the language to Russian

Thankfully he sees a link to do so, and clicks on it. Now we check our else if, since no cookie or session with configured language exist yet. We see that the link has a /rus/ param in the URL and it is not yet stored in the session, therefore we write the new value of the default language to the session and the cookie.
4.

The above user browses around the site, leaves, and then comes back

The session value is still present and therefore the site is automagically translated to Russian. This is good if the user forgot or doesn’t care to use links like example.com/rus/controller/action, because even plain links like example.com/controller/action will display the site in the right language because of the session value.
5.

The above user closes the browser, goes out hunting for wild boars, and comes to the site on some other day

Now we rely on our previously stored cookie to read in the language and ensure we don’t override anything that might be in the session already. (the first if )
6.

Now if the user decides to read the site in English

We pretty much follow through the same steps as above.

Now the last thing we need to do is to ensure that a URL param gets automatically added to all the links on the site, if a given language is chosen. Remember, that this is important to have such links for SEO as well.

Well, we’re sure as hell not going to supply the ‘language’ param manually to each link, so let’s override the cake’s default url() method to ensure the language param is now added to all links.

We create app_helper.php in /app/ (same place for app_controller.php and app_model.php), something like this:
view plaincopy to clipboardprint?

1. class AppHelper extends Helper {
2.
3. function url($url = null, $full = false) {
4. if(!isset($url['language']) && isset($this->params['language'])) {
5. $url['language'] = $this->params['language'];
6. }
7.
8. return parent::url($url, $full);
9. }
10.
11. }

class AppHelper extends Helper {

function url($url = null, $full = false) {
if(!isset($url['language']) && isset($this->params['language'])) {
$url['language'] = $this->params['language'];
}

return parent::url($url, $full);
}

}

Basically we check if ‘language’ param is already in the URL if it is, we don’t need to worry about it.
If not, and $this->params['language'] is available we pre-pend the required language to the URL.
The rest of the site, and all standard links will now include that ‘language’ param at the front of the URL (again, good for SEO).

And that’s pretty much it, even though the post was a bit long-winded (and beer to you, if you’ve made through the whole thing) it is quite nice to be able to do i18n & l10n in just about 15 lines of code.

A little disclaimer: even though the code seems to work fine, it is still experimental… so if you find some problems I haven’t yet encountered, please be sure to let me know.

P.S. Here’s a sampe test view, from which you can generate your .po files (easily done with cake i18n console command, but this is a topic for another tutorial and there are plenty of them “out there”).
view plaincopy to clipboardprint?

1. 2. __('This is only a test message');
3. ?>
4.
5.


6. link(__('Regular link', true), array('action'=>'test')); ?>
7.


8.
9.


10. link(__('Regular link two', true), array('controller'=>'users', 'action'=>'test5', 'some stuff')); ?>
11.


12.
13.


14. link('English', array('language'=>'eng')); ?>
15.


16.
17.


18. link('Русский', array('language'=>'rus')); ?>
19.

Wednesday, July 22, 2009

how to create Plugin in cake php

Creating a Plugin
As a working example, let's create a new plugin that orders pizza for you. What could be more useful in any CakePHP application?
To start out, we'll need to place our plugin files inside the /app/plugins folder. The name of the parent folder for all the plugin files
is important, and will be used in many places, so pick wisely. For this plugin, let's use the name 'pizza'. This is how the setup will
eventually look:
Pizza Ordering Filesystem Layout
/app
/plugins
/pizza
/controllers <- plugin controllers go here
/models <- plugin models go here
/views <- plugin views go here
/pizza_app_controller.php <- plugin's AppController, named after the plugin
/pizza_app_model.php <- plugin's AppModel, named after the plugin

While defining an AppController and AppModel for any normal application is not required, defining them for plugins is. You'll
need to create them before your plugin works. These two special classes are named after the plugin, and extend the parent
application's AppController and AppModel. Here's what they should look like:
Pizza Plugin AppController: /app/plugins/pizza_app_controller.php

class PizzaAppController extends AppController
{
//...
}
?>
Pizza Plugin AppModel: /app/plugins/pizza_app_model.php
class PizzaAppModel extends AppModel
{
//...
}
?>
If you forget to define these special classes, CakePHP will hand you "Missing Controller" errors until the problem is rectified.

Section 2
Plugin Controllers
Controllers for our pizza plugin will be stored in /app/plugins/pizza/controllers. Since the main thing we'll be tracking is pizza
orders, we'll need an OrdersController for this plugin.

While it isn't required, it is recommended that you name your plugin controllers something relatively unique in order to avoid
namespace conflicts with parent applications. Its not a stretch to think that a parent application might have a UsersController,
OrderController, or ProductController: so you might want to be creative with controller names, or prepend the name of the plugin to
the classname (PizzaOrdersController, in this case).
So, we place our new PizzaOrdersController in /app/plugins/pizza/controllers and it looks like so:
/app/plugins/pizza/controllers/pizza_orders_controller.php
class PizzaOrdersController extends PizzaAppController
{
var $name = 'PizzaOrders';
function index()
{
//...
}
function placeOrder()
{
//...
}
}
?>
Note how this controller extends the plugin's AppController (called PizzaAppController) rather than just the parent application's
AppController.

Section 3
Plugin Models
Models for the plugin are stored in /app/plugins/pizza/models. We've already defined a PizzaOrdersController for this plugin, so
let's create the model for that controller, called PizzaOrders (the classname PizzaOrders is consistent with our naming scheme, and
is unique enough, so we'll leave it as is).
/app/plugins/pizza/models/pizza_order.php
class PizzaOrder extends PizzaAppModel
{
var $name = 'PizzaOrder';
}
?>
Again, note that this class extends PizzaAppModel rather than AppModel.

Section 4
Plugin Views
Views behave exactly as they do in normal applications. Just place them in the right folder inside of the /app/plugins/[plugin]/views
folder. For our pizza ordering plugin, we'll need at least one view for our PizzaOrdersController::index() action, so let's include that
as well:
/app/plugins/pizza/views/pizza_orders/index.thtml

Order A Pizza


Nothing goes better with Cake than a good pizza!




Section 5
Working With Plugins
So, now that you've built evertything, it should be ready to distribute (though we'd suggest you also distribute a few extras like a
readme, sql file, etc.).
Once a plugin as been installed in /app/plugins, you can access it at the URL /pluginname/controllername/action. In our pizza
ordering plugin example, we'd access our PizzaOrdersController at /pizza/pizzaOrders.
Some final tips on working with plugins in your CakePHP applications:
When you don't have a [Plugin]AppController and [Plugin]AppModel, you'll get missing Controller errors when trying to
access a plugin controller.

1.
You can have a default controller with the name of your plugin. If you do that, you can access it via /[plugin]/action. For
example, a plugin named 'users' with a controller named UsersController can be accessed at /users/add if there is no plugin
called AddController in your [plugin]/controllers folder.

2.
Plugins will use the layouts from the /app/views/layouts folder by default.

3.
You can do inter-plugin communication by using
$this->requestAction('/plugin/controller/action');
in your controllers.

4.
If you use requestAction, make sure controller and model names are as unique as possible. Otherwise you might get PHP
"redefined class ..." errors.

local users at twitter

Hy ,
How can i find local user at twitter according to particular location
thanks