bioinfornatics
bioinfornatics

Reputation: 1808

Do a multilanguage application?

I would like to know how do a multilanguage application. It seem it is possible by using flag -J but they are no document for this feature. link given in this page http://www.digitalmars.com/d/2.0/dmd-linux.html seem to be wrong

if you can do a little example, will be nice. Something for detect at runtime or not if not possibe with usage -J flag

thanks

kind regards

Upvotes: 0

Views: 218

Answers (3)

bioinfornatics
bioinfornatics

Reputation: 1808

Thanks @BCS

So without -J flag, for use localization i have do:

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 );
}

and each files are named like: message_<language>.properties. Where in properties files is something like:

key1=value
key2=value

i split string by using "=" character and put in a hashmap. For get right statement just use the key

Upvotes: 0

BCS
BCS

Reputation: 78683

The -J flags give DMD the root paths to use for Import Expressions. You might be able to use this as part of some kind of i18n system but it's designed for importing arbitrary data blobs at compile time.


Edit: From memory:

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);
}

Compiled like this:

echo Hello World > some_data_file.txt
dmd code.d -J ./

will make produce a program that will print this when run:

Hello World

This is the long, short and total of what the import expression is for and the sole use for the -J flag is to control the path's the import expression reads from.

Upvotes: 1

user541686
user541686

Reputation: 210785

I'm not sure what you mean by a multilanguage application -- the -J flag is for the import(some_string) expression, passed to DMD (which is just a compiler).

Project management is outside the scope of DMD.

Upvotes: 1

Related Questions