Skip to content

Commit 59d7d5a

Browse files
committed
Moved language string to external file
1 parent b158456 commit 59d7d5a

File tree

5 files changed

+71
-31
lines changed

5 files changed

+71
-31
lines changed

application/config/rest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,13 @@
477477
|
478478
*/
479479
$config['rest_ajax_only'] = FALSE;
480+
481+
/*
482+
|--------------------------------------------------------------------------
483+
| REST Language File
484+
|--------------------------------------------------------------------------
485+
|
486+
| Language file to load from the language directory
487+
|
488+
*/
489+
$config['rest_language'] = 'english';
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>403 Forbidden</title>
5+
</head>
6+
<body>
7+
8+
<p>Directory access is forbidden.</p>
9+
10+
</body>
11+
</html>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/*
4+
* English language
5+
*/
6+
7+
$lang['text_rest_invalid_api_key'] = 'Invalid API key %s'; // %s is the REST API key
8+
$lang['text_rest_invalid_credentials'] = 'Invalid credentials';
9+
$lang['text_rest_ip_denied'] = 'IP denied';
10+
$lang['text_rest_ip_unauthorized'] = 'IP unauthorized';
11+
$lang['text_rest_unauthorized'] = 'Unauthorized';
12+
$lang['text_rest_ajax_only'] = 'Only Ajax requests are only allowed';
13+
$lang['text_rest_api_key_unauthorized'] = 'This API key does not have access to the requested controller';
14+
$lang['text_rest_api_key_permissions'] = 'This API key does not have enough permissions';
15+
$lang['text_rest_api_key_time_limit'] = 'This API key has reached the time limit for this method';
16+
$lang['text_rest_unknown_method'] = 'Unknown method';
17+
$lang['text_rest_unsupported'] = 'Unsupported protocol';

application/language/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>403 Forbidden</title>
5+
</head>
6+
<body>
7+
8+
<p>Directory access is forbidden.</p>
9+
10+
</body>
11+
</html>

application/libraries/REST_Controller.php

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -352,25 +352,6 @@ abstract class REST_Controller extends CI_Controller {
352352
self::HTTP_NOT_IMPLEMENTED => 'NOT IMPLEMENTED'
353353
];
354354

355-
/**
356-
* Language strings for the response message
357-
*
358-
* @var array
359-
*/
360-
protected $text_lang_strings = [
361-
'text_rest_invalid_api_key' => 'Invalid API key %s', // %s is the REST API key
362-
'text_rest_invalid_credentials' => 'Invalid credentials',
363-
'text_rest_ip_denied' => 'IP denied',
364-
'text_rest_ip_unauthorized' => 'IP unauthorized',
365-
'text_rest_unauthorized' => 'Unauthorized',
366-
'text_rest_ajax_only' => 'Only Ajax requests are only allowed',
367-
'text_rest_api_key_unauthorized' => 'This API key does not have access to the requested controller',
368-
'text_rest_api_key_permissions' => 'This API key does not have enough permissions',
369-
'text_rest_api_key_time_limit' => 'This API key has reached the time limit for this method',
370-
'text_rest_unknown_method' => 'Unknown method',
371-
'text_rest_unsupported' => 'Unsupported protocol'
372-
];
373-
374355
/**
375356
* Extend this function to apply additional checking early on in the process
376357
*
@@ -425,6 +406,16 @@ public function __construct($config = 'rest')
425406
// At present the library is bundled with REST_Controller 2.5+, but will eventually be part of CodeIgniter (no citation)
426407
$this->load->library('format');
427408

409+
// Get the language
410+
$language = $this->config->item('rest_language');
411+
if ($language === NULL)
412+
{
413+
$language = 'english';
414+
}
415+
416+
// Load the language file
417+
$this->lang->load('rest_controller', $language);
418+
428419
// Initialise the response, request and rest objects
429420
$this->request = new stdClass();
430421
$this->response = new stdClass();
@@ -520,7 +511,7 @@ public function __construct($config = 'rest')
520511
// Display an error response
521512
$this->response([
522513
$this->config->item('rest_status_field_name') => FALSE,
523-
$this->config->item('rest_message_field_name') => $this->text_lang_strings['text_rest_ajax_only']
514+
$this->config->item('rest_message_field_name') => $this->lang->line('text_rest_ajax_only')
524515
], self::HTTP_NOT_ACCEPTABLE);
525516
}
526517

@@ -582,7 +573,7 @@ public function _remap($object_called, $arguments)
582573
{
583574
$this->response([
584575
$this->config->item('rest_status_field_name') => FALSE,
585-
$this->config->item('rest_message_field_name') => $this->text_lang_strings['text_rest_unsupported']
576+
$this->config->item('rest_message_field_name') => $this->lang->line('text_rest_unsupported')
586577
], self::HTTP_FORBIDDEN);
587578
}
588579

@@ -607,7 +598,7 @@ public function _remap($object_called, $arguments)
607598

608599
$this->response([
609600
$this->config->item('rest_status_field_name') => FALSE,
610-
$this->config->item('rest_message_field_name') => sprintf($this->text_lang_strings['text_rest_invalid_api_key'], $this->rest->key)
601+
$this->config->item('rest_message_field_name') => sprintf($this->lang->line('text_rest_invalid_api_key'), $this->rest->key)
611602
], self::HTTP_FORBIDDEN);
612603
}
613604

@@ -621,7 +612,7 @@ public function _remap($object_called, $arguments)
621612

622613
$this->response([
623614
$this->config->item('rest_status_field_name') => FALSE,
624-
$this->config->item('rest_message_field_name') => $this->text_lang_strings['text_rest_api_key_unauthorized']
615+
$this->config->item('rest_message_field_name') => $this->lang->line('text_rest_api_key_unauthorized')
625616
], self::HTTP_UNAUTHORIZED);
626617
}
627618

@@ -630,7 +621,7 @@ public function _remap($object_called, $arguments)
630621
{
631622
$this->response([
632623
$this->config->item('rest_status_field_name') => FALSE,
633-
$this->config->item('rest_message_field_name') => $this->text_lang_strings['text_rest_unknown_method']
624+
$this->config->item('rest_message_field_name') => $this->lang->line('text_rest_unknown_method')
634625
], self::HTTP_NOT_FOUND);
635626
}
636627

@@ -640,7 +631,7 @@ public function _remap($object_called, $arguments)
640631
// Check the limit
641632
if ($this->config->item('rest_enable_limits') && $this->_check_limit($controller_method) === FALSE)
642633
{
643-
$response = [$this->config->item('rest_status_field_name') => FALSE, $this->config->item('rest_message_field_name') => $this->text_lang_strings['text_rest_api_key_time_limit']];
634+
$response = [$this->config->item('rest_status_field_name') => FALSE, $this->config->item('rest_message_field_name') => $this->lang->line('text_rest_api_key_time_limit')];
644635
$this->response($response, self::HTTP_UNAUTHORIZED);
645636
}
646637

@@ -657,7 +648,7 @@ public function _remap($object_called, $arguments)
657648
}
658649

659650
// They don't have good enough perms
660-
$response = [$this->config->item('rest_status_field_name') => FALSE, $this->config->item('rest_message_field_name') => $this->text_lang_strings['text_rest_api_key_permissions']];
651+
$response = [$this->config->item('rest_status_field_name') => FALSE, $this->config->item('rest_message_field_name') => $this->lang->line('text_rest_api_key_permissions')];
661652
$authorized || $this->response($response, self::HTTP_UNAUTHORIZED);
662653
}
663654

@@ -1863,7 +1854,7 @@ protected function _check_php_session()
18631854
// Display an error response
18641855
$this->response([
18651856
$this->config->item('rest_status_field_name') => FALSE,
1866-
$this->config->item('rest_message_field_name') => $this->text_lang_strings['text_rest_unauthorized']
1857+
$this->config->item('rest_message_field_name') => $this->lang->line('text_rest_unauthorized')
18671858
], self::HTTP_UNAUTHORIZED);
18681859
}
18691860
}
@@ -1961,7 +1952,7 @@ protected function _prepare_digest_auth()
19611952
// Display an error response
19621953
$this->response([
19631954
$this->config->item('rest_status_field_name') => FALSE,
1964-
$this->config->item('rest_message_field_name') => $this->text_lang_strings['text_rest_invalid_credentials']
1955+
$this->config->item('rest_message_field_name') => $this->lang->line('text_rest_invalid_credentials')
19651956
], self::HTTP_UNAUTHORIZED);
19661957
}
19671958
}
@@ -1983,7 +1974,7 @@ protected function _check_blacklist_auth()
19831974
// Display an error response
19841975
$this->response([
19851976
$this->config->item('rest_status_field_name') => FALSE,
1986-
$this->config->item('rest_message_field_name') => $this->text_lang_strings['text_rest_ip_denied']
1977+
$this->config->item('rest_message_field_name') => $this->lang->line('text_rest_ip_denied')
19871978
], self::HTTP_UNAUTHORIZED);
19881979
}
19891980
}
@@ -2011,7 +2002,7 @@ protected function _check_whitelist_auth()
20112002
{
20122003
$this->response([
20132004
$this->config->item('rest_status_field_name') => FALSE,
2014-
$this->config->item('rest_message_field_name') => $this->text_lang_strings['text_rest_ip_unauthorized']
2005+
$this->config->item('rest_message_field_name') => $this->lang->line('text_rest_ip_unauthorized')
20152006
], self::HTTP_UNAUTHORIZED);
20162007
}
20172008
}
@@ -2045,7 +2036,7 @@ protected function _force_login($nonce = '')
20452036
// Display an error response
20462037
$this->response([
20472038
$this->config->item('rest_status_field_name') => FALSE,
2048-
$this->config->item('rest_message_field_name') => $this->text_lang_strings['text_rest_unauthorized']
2039+
$this->config->item('rest_message_field_name') => $this->lang->line('text_rest_unauthorized')
20492040
], self::HTTP_UNAUTHORIZED);
20502041
}
20512042

0 commit comments

Comments
 (0)