0

我想知道如何做一个多语言应用程序。使用标志 -J 似乎是可能的,但它们不是此功能的文档。本页http://www.digitalmars.com/d/2.0/dmd-linux.html中给出的链接似乎是错误的

如果你能做一个小例子,会很好。如果不能使用 -J 标志,则在运行时检测或不检测的东西

谢谢

亲切的问候

4

3 回答 3

1

我不确定您所说的多语言应用程序是什么意思——-J标志用于import(some_string)表达式,传递给 DMD(它只是一个编译器)。

项目管理超出了 DMD 的范围。

于 2011-08-12T22:31:50.543 回答
1

这些-J标志为 DMD 提供了用于Import Expressions的根路径。您可能可以将它用作某种i18n系统的一部分,但它是为在编译时导入任意数据 blob 而设计的。


编辑:从记忆中:

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标志的唯一用途是控制导入表达式读取的路径。

于 2011-08-12T23:55:58.857 回答
0

谢谢@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_.properties <language>。属性文件中的位置类似于:

key1=value
key2=value

我使用“=”字符拆分字符串并放入哈希图。要获得正确的声明,只需使用密钥

于 2011-08-16T12:00:50.133 回答