Index: includes/path.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/path.inc,v
retrieving revision 1.70
diff -u -p -r1.70 path.inc
--- includes/path.inc	31 Jul 2010 04:10:30 -0000	1.70
+++ includes/path.inc	31 Jul 2010 14:06:33 -0000
@@ -96,7 +96,7 @@ function drupal_lookup_path($action, $pa
           // Now fetch the aliases corresponding to these system paths.
           // We order by ASC and overwrite array keys to ensure the correct
           // alias is used when there are multiple aliases per path.
-          $cache['map'][$path_language] = db_query("SELECT source, alias FROM {url_alias} WHERE source IN (:system) AND language IN (:language, :language_none) ORDER BY language ASC, pid ASC", array(
+          $cache['map'][$path_language] = db_query("SELECT source, alias FROM {url_alias} WHERE source IN (:system) AND language IN (:language, :language_none) ORDER BY language_sort ASC, pid ASC", array(
             ':system' => $cache['system_paths'],
             ':language' => $path_language,
             ':language_none' => LANGUAGE_NONE,
@@ -118,7 +118,7 @@ function drupal_lookup_path($action, $pa
       // For system paths which were not cached, query aliases individually.
       elseif (!isset($cache['no_aliases'][$path_language][$path])) {
         // Get the most fitting result falling back with alias without language
-        $alias = db_query("SELECT alias FROM {url_alias} WHERE source = :source AND language IN (:language, :language_none) ORDER BY language DESC, pid DESC", array(
+        $alias = db_query("SELECT alias FROM {url_alias} WHERE source = :source AND language IN (:language, :language_none) ORDER BY language_sort DESC, pid DESC", array(
           ':source' => $path,
           ':language' => $path_language,
           ':language_none' => LANGUAGE_NONE,
@@ -134,7 +134,7 @@ function drupal_lookup_path($action, $pa
       $source = FALSE;
       if (!isset($cache['map'][$path_language]) || !($source = array_search($path, $cache['map'][$path_language]))) {
         // Get the most fitting result falling back with alias without language
-        if ($source = db_query("SELECT source FROM {url_alias} WHERE alias = :alias AND language IN (:language, :language_none) ORDER BY language DESC, pid DESC", array(
+        if ($source = db_query("SELECT source FROM {url_alias} WHERE alias = :alias AND language IN (:language, :language_none) ORDER BY language_sort DESC, pid DESC", array(
                      ':alias' => $path,
                      ':language' => $path_language,
                      ':language_none' => LANGUAGE_NONE))
@@ -395,6 +395,7 @@ function path_load($conditions) {
  */
 function path_save(&$path) {
   $path += array('pid' => NULL, 'language' => LANGUAGE_NONE);
+  $path['language_sort'] = ($path['language'] == LANGUAGE_NONE) ? 0 : 1;
 
   // Insert or update the alias.
   $status = drupal_write_record('url_alias', $path, (!empty($path['pid']) ? 'pid' : NULL));
Index: includes/update.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/update.inc,v
retrieving revision 1.64
diff -u -p -r1.64 update.inc
--- includes/update.inc	17 Jul 2010 19:44:06 -0000	1.64
+++ includes/update.inc	31 Jul 2010 14:06:33 -0000
@@ -400,6 +400,16 @@ function update_fix_d7_requirements() {
     db_add_field('url_alias', 'source', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
     db_add_field('url_alias', 'alias', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
 
+    // Add language_sort column is needed for drupal_lookup_path().
+    $spec = array(
+      'description' => 'A flag to indicate if the alias for unknown (0) or defined language (1).',
+      'type' => 'int',
+      'not null' => TRUE,
+      'size' => 'small',
+    );
+    db_add_field('url_alias', 'language_sort', $spec);
+
     // Add new columns to {menu_router}.
     db_add_field('menu_router', 'delivery_callback', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''));
     db_add_field('menu_router', 'context', array(
Index: modules/locale/locale.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/locale/locale.test,v
retrieving revision 1.74
diff -u -p -r1.74 locale.test
--- modules/locale/locale.test	30 Jul 2010 01:52:54 -0000	1.74
+++ modules/locale/locale.test	31 Jul 2010 14:06:34 -0000
@@ -1486,7 +1486,50 @@ class LocalePathFunctionalTest extends D
     $this->drupalGet($prefix . '/' . $custom_language_path);
     $this->assertText($node->title, t('Custom language alias works.'));
 
-    $this->drupalLogout();
+    // Create custom path.
+    $custom_path = $this->randomName(8);
+
+    // Check priority of language for alias by source path.
+    $edit = array(
+      'source'   => 'node/' . $node->nid,
+      'alias'    => $custom_path,
+      'language' => LANGUAGE_NONE,
+    );
+    path_save($edit);
+    $lookup_path = drupal_lookup_path('alias', 'node/' . $node->nid, 'en');
+    $this->assertEqual($english_path, $lookup_path, t('English language alias has priority.'));
+    // Same check for language 'xx'.
+    $lookup_path = drupal_lookup_path('alias', 'node/' . $node->nid, $prefix);
+    $this->assertEqual($custom_language_path, $lookup_path, t('Custom language alias has priority.'));
+    path_delete($edit);
+
+    // Create language nodes to check priority of aliases.
+    $first_node = $this->drupalCreateNode(array('type' => 'page'));
+    $second_node = $this->drupalCreateNode(array('type' => 'page'));
+
+    // Assing custom path alias to first node with english language.
+    $edit = array(
+      'source'   => 'node/' . $first_node->nid,
+      'alias'    => $custom_path,
+      'language' => 'en',
+    );
+    $this->drupalPost($path, $edit, t('Save'));
+
+    // Assing custom path alias to second node with LANGUAGE_NONE.
+    $edit = array(
+      'source'   => 'node/' . $second_node->nid,
+      'alias'    => $custom_path,
+      'language' => LANGUAGE_NONE,
+    );
+    $this->drupalPost($path, $edit, t('Save'));
+
+    // Confirm custom path leads to first node.
+    $this->drupalGet($custom_path);
+    $this->assertText($first_node->title, t('Custom alias returns first node.'));
+
+    // Confirm custom path with prefix leads to second node.
+    $this->drupalGet($prefix . '/' . $custom_path);
+    $this->assertText($second_node->title, t('Custom alias with prefix returns second node.'));
   }
 }
 
Index: modules/system/system.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.install,v
retrieving revision 1.494
diff -u -p -r1.494 system.install
--- modules/system/system.install	31 Jul 2010 12:29:31 -0000	1.494
+++ modules/system/system.install	31 Jul 2010 14:06:34 -0000
@@ -1560,12 +1560,19 @@ function system_schema() {
         'default' => '',
       ),
       'language' => array(
-        'description' => 'The language this alias is for; if blank, the alias will be used for unknown languages. Each Drupal path can have an alias for each supported language.',
+        'description' => "The language this alias is for; if 'und', the alias will be used for unknown languages. Each Drupal path can have an alias for each supported language.",
         'type' => 'varchar',
         'length' => 12,
         'not null' => TRUE,
         'default' => '',
       ),
+      'language_sort' => array(
+        'description' => 'A flag to indicate if the alias for unknown (0) or defined language (1).',
+        'type' => 'int',
+        'not null' => TRUE,
+        'size' => 'small',
+      ),
     ),
     'primary key' => array('pid'),
     'indexes' => array(
@@ -2382,6 +2389,14 @@ function system_update_7047() {
  * Convert path languages from the empty string to LANGUAGE_NONE.
  */
 function system_update_7048() {
+  $spec = array(
+    'description' => "The language this alias is for; if 'und', the alias will be used for unknown languages. Each Drupal path can have an alias for each supported language.",
+    'type' => 'varchar',
+    'length' => 12,
+    'not null' => TRUE,
+    'default' => '',
+  );
+  db_change_field('url_alias', 'language', 'language', $spec);
   db_update('url_alias')
     ->fields(array('language' => LANGUAGE_NONE))
     ->condition('language', '')
