Opencart function to quickly extract language data
Opencart 函数,可以快速提取语言数据
Readme in Chinese 中文
This is the Opencart system language functions lang()
and __()
, which makes it easy to extract language data, and the two functions are equivalent.
When developing the opencart(2.3.2) tpl file, if you need to use the data in the language
directory, you need to first write $this->config->get('key')
in controller
to assign the data to $data
before it can be used in the tpl
file.
If you use this function lang()
, you can omit this step.
(PHP 5, PHP 7)
lang — Find language data (case sensitive)
string lang ( string $key [, string $default = ''])
Returns or prints the language data found by $key
.
key
The value being searched for, otherwise known as the needle.
default
Optionally, if needle does not find the corresponding data, return the value of this parameter (the default empty string).
Returns the string
data found by $key
. If not found, returns the value of the $default
parameter. If there is no $default
argument, an empty string
is returned.
If the function is called in a php file, the result will be returned. If the function is called in the tpl file, it will be printed directly.
- Overwrite the corresponding file in the
system
directory - Update the cache file in the
modification
directory
Used in tpl files
<!-- Extract the `language/*/common/header.php` data in `common/header.tpl` -->
<div><?php __('heading_title'); ?></div>
<!-- Extract the data of `language/*/sale/order.php` in `common/header.tpl` -->
<div><?php __('sale/order.heading_title'); ?></div>
<!-- When `heading_title` is not found, it will return `hello world!` -->
<div><?php __('heading_title', 'hello world!'); ?></div>
<!-- When `heading_title` is not found and `$default` is not passed, an empty `string` is printed -->
<div><?php __('heading_title'); ?></div>
Used in php files
// Extract the data of `language/*/common/forgotten.php` at `common/forgotten.tpl`
$this->error['error_password'] = __('error_password');
// Extract the data of `language/*/common/login.php` at `common/forgotten.tpl`
$this->error['error_login'] = __('common/login.error_login');
// When `error_login` is not found, it will return `Error message`
$this->error['error_login'] = __('error_login', 'Error message');
// When `error_login` is not found and `$default` is not passed, an empty `string` is returned
if (__('error_login') === '') {
echo 'No data found';
}