D 你需要一个多语言应用程序吗?
我想知道如何使用多语言应用程序。使用flag-J似乎是可能的,但是它们不是这个特性的文档。 此页面中给出的链接似乎是错误的 如果你能举个小例子,那就好了。如果不可能使用usage-J标志,则可以在运行时检测或不检测 谢谢D 你需要一个多语言应用程序吗?,d,phobos,D,Phobos,我想知道如何使用多语言应用程序。使用flag-J似乎是可能的,但是它们不是这个特性的文档。 此页面中给出的链接似乎是错误的 如果你能举个小例子,那就好了。如果不可能使用usage-J标志,则可以在运行时检测或不检测 谢谢 亲切问候我不确定您所说的多语言应用程序是什么意思,-J标志用于importsome_字符串表达式,传递给DMD,DMD只是一个编译器 项目管理不在DMD的范围之内。使用-J标志为DMD提供根路径。您可以将其用作某种系统的一部分,但它是为在编译时导入任意数据块而设计的 编辑:从内
亲切问候我不确定您所说的多语言应用程序是什么意思,-J标志用于importsome_字符串表达式,传递给DMD,DMD只是一个编译器 项目管理不在DMD的范围之内。使用-J标志为DMD提供根路径。您可以将其用作某种系统的一部分,但它是为在编译时导入任意数据块而设计的 编辑:从内存:
void main() {
// the import expression resolves at compile
// time to the contents of the named file.
stirng s = import("some_data_file.txt");
writef("%s", s);
}
编译如下:
echo Hello World > some_data_file.txt
dmd code.d -J ./
将生成一个程序,在运行时打印:
Hello World
这是导入表达式的长、短和总计,而-J标志的唯一用途是控制导入表达式从中读取的路径。谢谢@BCS
因此,在没有-J标志的情况下,为了使用本地化,我必须:
module localisation;
import std.string;
import std.stdio : write, File, exists, StdioException, lines;
import std.array : split;
import std.process : getenv;
import std.exception: enforce, enforceEx;
struct Culture{
string[string] data = null;
string name = null;
public static Culture opCall( string[string] data, string name ){ // Constructor
Culture res;
res.data = data;
res.name = name;
return res;
}
}
static Culture culture = Culture(null, null);
Culture getLocalization(in string language){
string fileName = null;
string name = null;
string[string] localization = null;
if ( exists("messages_"~ language ~ ".properties") ){
fileName = "messages" ~ language ~ ".properties";
name = language;
}
else if ( language.length >= 5 ){
if ( language[2] == '-' ){
fileName = "messages_" ~ language[0..2] ~ "_" ~ language[4..5] ~ ".properties";
name = language[0..2] ~ "_" ~ language[4..5];
}
else{
fileName = "messages_" ~ language[0..5] ~ ".properties";
name = language[0..5];
}
}
// Thrown an exception if is null
enforce( fileName, "Unknow Culture format: " ~ language);
// Thrown an exception if name is null
enforce( name, "Error: name is null");
// Thrown an exception if is path do not exist
enforceEx!StdioException( exists( fileName ), "Cannot open file " ~ fileName ~ ", do not exist or is not include with -J flag" );
File fileCulture = File( fileName, "r" );
foreach(string line; lines(fileCulture)){
string[] result = split(line, "=");
localization[ result[0] ] = result[1];
}
return Culture( localization, name);
}
void main ( string[] args ){
string[string] localization = null;
string language = getenv("LANG");
culture = getLocalization( language );
}
每个文件的名称类似于:message_u2;.properties。属性文件中的位置类似于:
key1=value
key2=value
我使用=字符分割字符串,并将其放入hashmap中。对于get right语句,只需使用gnu GETEX multilanguage之类的键即可。@bioinformnatics:-J标志与国际化和本地化无关。如果我想在rutime而不是在编译时检测本地化,谢谢@Mehrdad。我尝试了一个最小的代码,我假设它是posix,但它可以被改进,所以最好使用gettex来实现这一点?或者它们是D编程的另一种方式。这与-J标志无关,也与-J标志无关。谢谢@BCS。我尝试了一个最小的代码,我假设它是posix,但它可以改进,-J标志将不会对代码产生任何影响。与printf函数一样,-J标志与本地化无关。你似乎看到了一种没有联系的联系。请你发布一个链接,最好是一个引用,无论它是什么,给你一个完全错误的想法,-J标志和本地化之间有任何联系。通过这种方式,可以修复文档,以避免引起这种混乱的任何原因。它在ressource中为ressource使用-J标志,它们是uProperties.files区域设置。而且,它们是我们的工作伙伴。问题是std.process.getenv处于运行时并导入。。编译时间是的,如果您在运行时之前不知道需要什么文件,import/-J将帮不上忙。解决方案是在编译时获取所有这些代码,并在运行时选择其中一个。OTOH如果在编译时没有所有的文件,这对您没有任何好处,因此您可能希望在运行时放入一个默认的处理程序,该处理程序使用正常的libs来尝试从磁盘加载编译时未涉及的文件。-顺便说一句,令人困惑的是,你的问题除了作为一个激励性的例子外,与本地化没有太大关系。是的,我对-J标志感到困惑,现在我明白了。非常感谢您花时间和解释。只是为了理解,我想要一个多本地化程序,在运行时选择正确的语言。如果我使用-J标志,我可以直接提供可执行文件,如果不需要,我需要提供更多的语言环境文件。所以有3种可能性:-J/import+像你说的那样在运行时选择;不要像show my code那样使用-J/import-only运行时;使用getex。