从i18n-ascii.txt文件中读取数据,进行翻译,代码如下:
/**
* Generate project alias by project name
*/
private function _generateAlias($name, $projectId) {
$file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'i18n-ascii.txt';
$separator = '-';
// Read translation file
if (file_exists($file)) {
$translations = parse_ini_file($file);
}
// Translate name to ASCII
if (!empty($translations)) {
$name = strtr($name, $translations);
}
// Replace white space with the separator.
$name = preg_replace('/\s+/', $separator, $name);
// Turn to lower case
$name = strtolower($name);
// Check whether the name is exist
if ($projectId) {
$sql = 'SELECT url_alias FROM project WHERE id <> \'' . $projectId . '\' AND url_alias LIKE \'' . $name . '%\'';
} else {
$sql = 'SELECT url_alias FROM project WHERE url_alias LIKE \'' . $name . '%\'';
}
$aliases = Yii::app()->db->createCommand($sql)->queryAll();
// If the alias is exist
if (count($aliases) > 0) {
$start = strlen($name);
$subfixes = array();
foreach ($aliases as $alias) {
$subfix = $alias['url_alias'];
if ($subfix == $name) {
$subfixes[] = '0';
} else if (substr($subfix, $start, 1) == $separator) {
$subfix = substr($subfix, $start + 1);
if (is_numeric($subfix)) {
$subfixes[] = $subfix;
}
}
}
if (count($subfixes) > 0) {
$subfix = max($subfixes) + 1;
$name = $name . $separator . $subfix;
}
}
return $name;
} 文件下载路径为:/download/doushen1/7060091
如果觉得《php怎么将中文翻译成英文 PHP将中文翻译成拼音》对你有帮助,请点赞、收藏,并留下你的观点哦!