Symfony i18n 表:获取后备默认值的方法?
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试这样的事情:
Try something like this:
您还可以做的是覆盖城市模型中的 getName() 方法(或创建一个新方法,例如 getNameWithDefault())。
像这样的东西也可以工作:
当然,在您的数据库查询中,您每次都必须加载两个翻译。如果您用西班牙语显示主要内容,您将需要获取这两个数据,正如 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:
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.
如果您将可用语言存储在不同的表中,您可以执行类似的操作。因此它将返回默认的区域性名称,或者如果它是 NULL,它将返回第一个将找到的区域性名称。
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.