语言文件的最佳实践/设计模式
我正在寻找一种在应用程序中进行多种翻译的好方法。
现在我正在使用一个从 ini 文件中读取术语的类。 每个术语都是一个变量。 例如:
string allowed ;
[...]
allowed = config.readString (sectionName, "allowed") ;
这个解决方案的好处是它很轻,易于实现并在编译时检查 但不够灵活。
更灵活的解决方案可能是使用关联数组(或类似的)
currLang [ "allowed" ]
是否有人知道任何最佳实践或设计模式,或者只是透露他个人的实现方式?
我们也欢迎对众所周知的框架实现它的方式进行简单的描述。
I'm looking for a good way to have multiple translations in an application.
Right now I'm using a class that reads the terms from an ini file.
Every term is a variable.
For example:
string allowed ;
[...]
allowed = config.readString (sectionName, "allowed") ;
the good of this solution is that it's light, easy to implement and checked at compile time
but it's not flexible enough.
a more flexible solution could be to use an associative array (or similar)
currLang [ "allowed" ]
Does any-one know any best practice or design pattern or would just disclose his personal way of implementing it?
It's also welcome a simple description of the way the well known frameworks implement it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,我不知道你的问题的具体模式。但是,我多次实现了不同语言的翻译支持。我见过的最常见且与语言无关的一种是数组。您在启动时加载该数组并在应用程序中使用它。根据当前的语言设置,您可以使用该特定语言进行加载。
如果您想要一种更动态的方法,或者如果在启动(或登录)时读取整个数组需要很长时间,请查看代理模式。
Well, I don't know a specific pattern for your problem. However, I implemented a few times translation support in different languages. The most common one, and language independent, I've seen is with an array. You load up the array at the startup and use it in your application. Based on the current language setting, you load up in that specific language.
If you want a more dynamic approach, or if it takes too long to read the whole array at startup (or login), check out the Proxy pattern.