做多语言应用程序?

时间:2011-08-12 22:27:25

标签: d phobos

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

如果你能做一个小例子,那就太好了。在运行时检测的东西,如果不可能,则使用-J标志

感谢

亲切的问候

3 个答案:

答案 0 :(得分:1)

我不确定多语言应用程序的含义 - -J标志用于import(some_string)表达式,传递给DMD(它只是一个编译器)。

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

答案 1 :(得分:1)

-J标志为DMD提供了用于Import Expressions的根路径。您可以将其用作某种i18n系统的 part ,但它是为在编译时导入任意数据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标志的唯一用途是控制导入表达式读取的路径。

答案 2 :(得分: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 _ <language>。properties。属性文件中的位置如下:

key1=value
key2=value

我使用“=”字符拆分字符串并放入一个hashmap。要获得正确的陈述,只需使用密钥

即可
相关问题