php 中英文语言转换类代码
起初想到制成XML文档形式,这样操作也起来很容易。只是看到说XML效率不怎样
再者就是不同的模板,可这样也有个小问题,有些词汇比如时间提示是不确定,与可能是minute ,day。也有可能复数加 s
那好吧,做成数组,可数组就得做成在php文件的变量,很难做些扩展(我所知道所认为的是这样)
最后做成txt文本文件的形式,同样也为这样的效率担心,打开文件,搜索字符串,截取字符串这些,所幸最后运行了一下,一般机子大概0.0004秒,这让我很惊奇原以为会很慢,毕竟要调用多次。
好吧,上代码
复制代码 代码如下:
class language
{
static $lanObject;
public $type; // unit , dashboard , menu ,other
public $lan; // language
private $special; // The common in the file
private function __construct()
{
if( isset($_GET['hl']) || isset($_POST['hl']) )
{
switch( isset($_GET['hl'])?$_GET['hl']:$_POST['hl'] )
{
case 'en':
$this->lan = 'en';
case 'zh':
$this->lan = 'zh';
case 'all':
$this->lan = 'all';
default:
$this->error();
}
}
else
$this->lan = isset($_COOKIE['hl']) ? $_COOKIE['hl']:'zh';
}
public static function getObject()
{
if( !(self::$lanObject instanceof self) )
self::$lanObject = new language();
return self::$lanObject;
}
public function lto($key) //$key is English
{
if( $this->lan !== 'zh' )
return $key;
if( empty($this->special) ) // if the $special is null
{
if( isset($this->type) )
$this->special = file_get_contents($this->type.'.txt');
else
return $key;
}
echo $this->search($key);
}
private function search($searchTozh) // PHP String
{
$key_start = strpos($this->special,$searchTozh);
$key_end = strpos($this->special,' ',$key_start);
$len_str = strlen($searchTozh);
$for_sub = $key_start + $len_str + 1;
return substr($this->special, $for_sub, $key_end - $for_sub);
}
}
strpos(); 是找到字符串第一次出现的位置 比如 ‘wo' 在 ‘hello world' 中,返回值为 6
substr();是截取字符串的一部分
接下来是调试时加上的代码
复制代码 代码如下:
$la = language::getObject();
$la->type = 'unit';
$la->lto('min');
echo '
';
$la->lto('hello');
lto(这里面要翻译的英文);
unit.txt 文件的内容格式是
hello-你好 min-小 minute-分钟 minutes-分钟
$special设计为全局也是想到不止一次会调用lto() ,如果反复加载文件太浪费性能了。
$type设计为公有是考虑到加载的文件的效率问题,有的时候并不需要显示几天前这些,所以不如把这些按使用类型分开,比如有专门负责菜单翻译的menu.txt ,也有专门为操作,比如删除,收藏 翻译的txt文本。这样可以自由设定要加载的文本
语言也可以自由设定。
好吧,程序还可以改进,我没有按http请求中的客户端语言来设置$lan

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
