Symfony i18n 表:获取后备默认值的方法?

发布于 2024-08-19 04:44:56 字数 452 浏览 2 评论 0原文

我正在 MySQL 中构建一个城市名称表,其中包含大约 10K 行,作为 Symfony i18n 表。基本上,默认文化是 en_US,表中的每个城市最初都是这种文化。随着时间的推移,我只想将那些可能具有不同语言替代名称的城市添加到表中,例如“伦敦 (en_US) / 伦敦 (es_ES)”,而不必复制每个城市的所有城市数据语言在单独的表中。

现在,据我了解,如果不存在翻译,Symfony 不会自动选择后备默认城市名称。因此,我需要制定一个解决方案来执行以下操作:

如果翻译存在,请选择它....如果不存在,请选择默认的 en_US 城市名称。

到目前为止,我似乎应该使用COALESCE。不过,由于我对它不是很熟悉,我想知道经常使用它是否有什么缺点?我问这个问题是因为我需要在每个城市查询中包含它,这是我网站上非常频繁的操作。

如果有任何意见,甚至是以更好的方式做事的建议,我将不胜感激。谢谢。

I'm building a city names table in MySQL with about 10K rows to be a Symfony i18n table. Basically, the default culture is en_US and every city in the table is initially of this culture. Over time, I'd like to add to the table only those cities that might have an alternative name in a different language, such as "London (en_US) / Londres (es_ES)", without having to replicate all the city data for each language in separate tables.

Now, from what I understand, Symfony won't automatically pick a fallback default city name if a translation for it doesn't exist. So I need to craft a solution to do the following:

If translation exists, select it.... if not, select the default en_US city name.

So far it seems that I should use COALESCE. However, as I'm not very familiar with it, I'm wondering if there's any drawbacks to using it a lot? I'm asking this because I'd need to include it every city query which is a very frequent action on my site.

Would be grateful for any views, or even suggestions for doing things in a better way. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愛上了 2024-08-26 04:44:56

尝试这样的事情:

$q = Doctrine_Query::create()
     ->from('Cities c')
     ->leftJoin('c.Translation ct WITH lang IN(?)', array(array('es_ES', 'en_US')))
     ->execute();

Try something like this:

$q = Doctrine_Query::create()
     ->from('Cities c')
     ->leftJoin('c.Translation ct WITH lang IN(?)', array(array('es_ES', 'en_US')))
     ->execute();
风苍溪 2024-08-26 04:44:56

您还可以做的是覆盖城市模型中的 getName() 方法(或创建一个新方法,例如 getNameWithDefault())。
像这样的东西也可以工作:

public function getName()
{
  // Assuming that $this->name will give us the data for the current locale
  return ($this->name == NULL)? $this->Translation->en_US->name: $this->name;
}

当然,在您的数据库查询中,您每次都必须加载两个翻译。如果您用西班牙语显示主要内容,您将需要获取这两个数据,正如 Crozin 之前指出的那样。

What you could also do is overwrite de getName() method in the City Model (or create a new one, like getNameWithDefault()).
Something like this could also work:

public function getName()
{
  // Assuming that $this->name will give us the data for the current locale
  return ($this->name == NULL)? $this->Translation->en_US->name: $this->name;
}

On your database query, of course, you will have to load both Translations every time. If you are showing the main contents in spanish you will need to get both data, as pointed before by Crozin.

緦唸λ蓇 2024-08-26 04:44:56

如果您将可用语言存储在不同的表中,您可以执行类似的操作。因此它将返回默认的区域性名称,或者如果它是 NULL,它将返回第一个将找到的区域性名称。

public function getName($culture = null)
{
  if($this->getCurrentCitiesI18n($culture)->getName() != NULL)
    return $this->getCurrentCitiesI18n($culture)->getName();
  else
  {
    foreach (LanguagesPeer::getAvailablelanguages() as $lang)
    {
      if($this->getCurrentCitiesI18n($lang->getIsoCode())->getName() != NULL)
        return $this->getCurrentCitiesI18n($lang->getIsoCode())->getName();        
    }
  }
}

If you store available languages in a different table, you can do something like this. So it will return default culture name or if it is NULL, it will return a first which will be found.

public function getName($culture = null)
{
  if($this->getCurrentCitiesI18n($culture)->getName() != NULL)
    return $this->getCurrentCitiesI18n($culture)->getName();
  else
  {
    foreach (LanguagesPeer::getAvailablelanguages() as $lang)
    {
      if($this->getCurrentCitiesI18n($lang->getIsoCode())->getName() != NULL)
        return $this->getCurrentCitiesI18n($lang->getIsoCode())->getName();        
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文