Php 还有改进的余地吗?面向对象设计/代码模块化

Php 还有改进的余地吗?面向对象设计/代码模块化,php,internationalization,Php,Internationalization,如何改进国际化代码的以下组织结构?这只是一个PoC,我在intl::t()中使用php intl扩展进行翻译: index.php 1 <?php 2 //FILE translate.php, your "index.php" 3 $messages = include 'messages.php'; 4 require_once 'intl.php'; 5 Intl::setSource($messages); 6 7 class Foo { 8

如何改进国际化代码的以下组织结构?这只是一个PoC,我在intl::t()中使用php intl扩展进行翻译:

index.php

 1  <?php
 2  //FILE translate.php, your "index.php"
 3  $messages = include 'messages.php';
 4  require_once 'intl.php';
 5  Intl::setSource($messages);
 6
 7  class Foo {
 8          public function throwGreeting($name) {
 9                  throw new Exception(Intl::t('Hello %(name)s',array('name' => $name)));
10          }
11  }
12  $foo = new Foo;
13  try {
14          $foo->throwGreeting('Anonymous');
15  }
16  catch(Exception $e) {
17          echo 'The greeting is ',$e->getMessage();
18  }
 1  <?php
 2  //FILE intl.php
 3  class Intl {
 4          private static $messages;
 5
 6          /**
 7           * from php.net
 8           */
 9          public static function t($message,$args=array()) {
10                  if(array_key_exists($message,self::$messages)) {
11                          $message = self::$messages[$message];
12                  }
13                  else {
14                          //automatically queue message for translation
15                  }
16                  $keys    = array_keys($args);
17                  $keysmap = array_flip($keys);
18                  $values  = array_values($args);
19
20                  while (preg_match('/%\(([a-zA-Z0-9_ -]+)\)/', $message, $m))
21                  {
22                          if (!isset($keysmap[$m[1]]))
23                          {
24                                  throw new Exception(Intl::t("Missing argument '%(arg)s'",array('arg' => $m[1])));
25                                  
26                          }
27                          $message = str_replace($m[0], '%' . ($keysmap[$m[1]] + 1) . '$', $message);
28                  }
29                  array_unshift($values, $message);
30                  return call_user_func_array('sprintf', $values);
31          }
32          public static function setSource($src) {
33                  self::$messages = $src;
34          }
35  }
 1  <?php
 2  //FILE messages.php
 3  return array(
 4          'Hello %(name)s' => 'Hallo %(name)s',
 5  );
messages.php

 1  <?php
 2  //FILE translate.php, your "index.php"
 3  $messages = include 'messages.php';
 4  require_once 'intl.php';
 5  Intl::setSource($messages);
 6
 7  class Foo {
 8          public function throwGreeting($name) {
 9                  throw new Exception(Intl::t('Hello %(name)s',array('name' => $name)));
10          }
11  }
12  $foo = new Foo;
13  try {
14          $foo->throwGreeting('Anonymous');
15  }
16  catch(Exception $e) {
17          echo 'The greeting is ',$e->getMessage();
18  }
 1  <?php
 2  //FILE intl.php
 3  class Intl {
 4          private static $messages;
 5
 6          /**
 7           * from php.net
 8           */
 9          public static function t($message,$args=array()) {
10                  if(array_key_exists($message,self::$messages)) {
11                          $message = self::$messages[$message];
12                  }
13                  else {
14                          //automatically queue message for translation
15                  }
16                  $keys    = array_keys($args);
17                  $keysmap = array_flip($keys);
18                  $values  = array_values($args);
19
20                  while (preg_match('/%\(([a-zA-Z0-9_ -]+)\)/', $message, $m))
21                  {
22                          if (!isset($keysmap[$m[1]]))
23                          {
24                                  throw new Exception(Intl::t("Missing argument '%(arg)s'",array('arg' => $m[1])));
25                                  
26                          }
27                          $message = str_replace($m[0], '%' . ($keysmap[$m[1]] + 1) . '$', $message);
28                  }
29                  array_unshift($values, $message);
30                  return call_user_func_array('sprintf', $values);
31          }
32          public static function setSource($src) {
33                  self::$messages = $src;
34          }
35  }
 1  <?php
 2  //FILE messages.php
 3  return array(
 4          'Hello %(name)s' => 'Hallo %(name)s',
 5  );
1我想这样做

有一个名为language的文件夹。将带有键值的po文件放入其中


我将在php代码中使用key。根据语言的选择,将加载po文件。看看wordpress是怎么做的。

好吧,每次使用字符串时,您都在根据哈希值测试它。那跑不快。但拥有三个文件基本上是模块化的正确选择

更好的做法是:

/* At top of file or in eg "myintlstrings.php" */
class Intl { public static function t(&$cache,$message,$vals) {
  if ($cache==null) {
    // get translated version of $message into $cache
  } if (DOING_DEBUG) {
    // check parameters are in vals hash
  } array_unshift($vals,$cache); return call_user_func_array('sprintf',$vals);
} }


/* example 1 - function context */
static $cacheForGreeting; /* use static instead of global if in function */
throw new ExceptionIntl:t($cacheForGreeting,'Hello %(name)s'),
  array('name' => $name))));

/* example 2 - global context */
global $cacheForSomethingElse; /* use global instead of static if not in function */
echo(Intl::t($cacheForSomethingElse,"And today's sales figures are:"));

看起来不错,但我自己更喜欢基于gettext的方法,看起来不错。很难回答这个问题,因为它太主观了。谢谢。我来细细琢磨一下。你是说wordpress,最先进的蹩脚代码?不,谢谢:Pi提到了这个方法。这完全取决于你如何看待它。你可以用任何语言写蹩脚的代码。