[英]best practice/design pattern for language files
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. 现在,我正在使用从ini文件读取术语的类。 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. 也欢迎对众所周知的框架实现它的方式进行简单描述。
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. 如果您想要一种更动态的方法,或者在启动(或登录)时读取整个阵列花费的时间太长,请查看Proxy模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.