Commits

Anonymous committed beeb70c

Adding gettext support to the Nitrogen templates.

  • Participants
  • Parent commits 510a94b

Comments (0)

Files changed (9)

 { './src/*', [
 	{ i, "./include" },
 	{ i, "{{nitro_dir}}/apps" },
+	{ i, "{{gettext_dir}}/include" },
 	{ outdir, "./ebin" },
 	debug_info
 ]}.
+include dep.inc
 
 all:
-	erl -make
+	erl -pa $(GETTEXT_EBIN) -make
 
+# Run this the very first time after creating the project
 init:
 	-mkdir ebin
 	-mkdir dep
 clean:
 	rm -rf ./ebin/*.beam
 
+
+# ----------------------------------------------------------------------
+#                       GETTEXT SUPPORT
+#                       ---------------
+# GETTEXT_EBIN
+#  Path to the gettext ebin directory.
+#
+# GETTEXT_PO_DIR
+#  Set this to a directory where we have write access.
+#  This directory will hold all po-files and the dets DB file.
+#  Example: 'GETTEXT_DIR=$(MY_APP_DIR)/priv'
+#
+# GETTEXT_DEF_LANG
+#  Set the language code of the default language (e.g en,sv,...), 
+#  i.e the language you are using in the string arguments to the
+#  ?TXT macros. Example: 'GETTEXT_DEF_LANG=en'
+#
+# GETTEXT_TMP_NAME
+#  Set this to an arbitrary name.
+#  It will create a subdirectory to $(GETTEXT_DIR) where
+#  the intermediary files of this example will end up.
+#  Example: 'GETTEXT_TMP_NAME=tmp'
+#
+gettext: clean $(GETTEXT_PO_DIR)/lang/$(GETTEXT_TMP_NAME)/epot.dets 
+
+$(GETTEXT_PO_DIR)/lang/$(GETTEXT_TMP_NAME)/epot.dets:
+	@(export GETTEXT_TMP_NAME=$(GETTEXT_TMP_NAME); \\
+	  export GETTEXT_DIR=$(GETTEXT_PO_DIR); \\
+	  export GETTEXT_DEF_LANG=$(GETTEXT_DEF_LANG); \\
+	  export ERL_COMPILER_OPTIONS="[gettext]"; \\
+	  rm -f $(GETTEXT_PO_DIR)/lang/$(GETTEXT_TMP_NAME)/epot.dets; \\
+	  erl -pa $(GETTEXT_EBIN) -make; \\
+	  erl -noshell -pa $(GETTEXT_EBIN) -s gettext_compile epot2po; \\
+	  install -D $(GETTEXT_PO_DIR)/lang/$(GETTEXT_TMP_NAME)/$(GETTEXT_DEF_LANG)/gettext.po $(GETTEXT_PO_DIR)/lang/default/$(GETTEXT_DEF_LANG)/gettext.po; \\
+	  rm -rf $(GETTEXT_PO_DIR)/lang/$(GETTEXT_TMP_NAME))
+

File nitro.app.src

    ]},
 
   % configuration parameters similar to those in the config file specified on the command line
-  {env, [{ip, "0.0.0.0"}
+  {env, [{http_server, inets}  % yaws | inets | mochiweb
+         ,{ip, {127,0,0,1}}
          ,{port, 8282}
-	 ,{log_dir, "/tmp"}
+	 ,{db_name, "{{appid}}" }
+	 ,{servername, "localhost"}
+	 ,{log_dir, "./log"}
 	 ,{doc_root, "./www"}
+	 ,{external_hostname, "localhost"}
+         ,{external_port, 8282}
+         ,{template, "./templates/grid.html"}
 	 ,{acl, ["{{openid}}"
                 ]}
          ,{users,[{"{{openid}}",
          , gdate/0
          , gdate2datetime/1
 	 , friendly_date/1
+         , user/0
+         , user/1
+         , authenticated/0
+         , authenticated/1
+         , template/0
+         , db_name/0
          , hostname/0
-         , default_port/0
+         , external_hostname/0
+         , ip/0
+         , port/0
+         , servername/0
+         , http_server/0
+         , log_dir/0
+         , docroot/0
+         , top_dir/0
+         , priv_dir/0
+         , gettext_dir/0
+         , couchdb_url/0
+         , couchdb_host/0
+         , couchdb_port/0
          , i2l/1
         ]).
 
+-import({{appid}}_deps, [get_env/2]).
+
 -include_lib("nitrogen/include/wf.hrl").
            
+-define(is_bool(B), ((B =:= true) orelse (B =:= false))).
 
-default_port() -> 8080.
+
+couchdb_url() ->
+    "http://"++couchdb_host()++":"++integer_to_list(couchdb_port()).
+
+db_name()           -> get_env(db_name, "{{appid}}").
+couchdb_host()      -> get_env(couchdb_host, "localhost").
+couchdb_port()      -> get_env(couchdb_port, 5984).
+log_dir()           -> get_env(log_dir, "./tmp").
+docroot()           -> get_env(docroot, "./www").
+servername()        -> get_env(servername, "localhost").
+ip()                -> get_env(ip, {127,0,0,1}).
+port()              -> get_env(port, 8283).
+http_server()       -> get_env(http_server, inets).
+external_hostname() -> get_env(external_hostname, hostname()).
+template()          -> get_env(template, "./templates/grid.html").
+gettext_dir()       -> priv_dir().
+    
+
+hostname() ->
+    {ok,Host} = inet:gethostname(),
+    Host.
+
+top_dir() ->
+    filename:join(["/"|lists:reverse(tl(lists:reverse(string:tokens(filename:dirname(code:which(?MODULE)),"/"))))]).
+
+priv_dir() ->
+    top_dir()++"/priv".
+
+user() ->
+    case get_env(dbg_authenticated, false) of
+        false -> wf:user();
+        User  -> User
+    end.
+
+user(User) when is_list(User) -> wf:user(User);
+user(_)                       -> undefined.
+
+authenticated() ->
+    case get_env(dbg_authenticated, false) of
+        false -> wf:session(authenticated);
+        _     -> true
+    end.
+
+authenticated(Bool) when ?is_bool(Bool) ->
+    wf:session(authenticated, Bool).
+
 
 %%
 %% @doc Return gregorian seconds as of now()
     io_lib:format("+~2..0w~2..0w", [Hr, Min]).
 
 
-hostname() ->
-    {ok,Host} = inet:gethostname(),
-    Host.
-
 i2l(I) when is_integer(I) -> integer_to_list(I);
 i2l(L) when is_list(L)    -> L.
     

File nitro.template

              ,{email,       "etnt@redhoterlang.com"}
 	     ,{openid,      "http://etnt.myopenid.com/"}
              ,{nitro_dir,   "/home/tobbe/git/nitrogen"}
+             ,{gettext_dir, "/home/tobbe/git/gettext"}
 	     ,{www,         "http://www.redhoterlang.com"}
 	     ,{year,        "2010"}
             ]}.

File nitro_inets.erl

 
 -module({{appid}}_inets).
 
--export([start_link/0, stop/0, do/1]).
+-export([start_link/0, stop/0, do/1, out/1]).
 
 -include_lib("nitrogen/include/wf.hrl").
+-include_lib("yaws/include/yaws.hrl").
 
 
 %%% @doc This is the routing table.
     ].
 
 start_link() ->
+    start_link({{appid}}:http_server()).
+
+start_link(inets) ->
     inets:start(),
     {ok, Pid} =
         inets:start(httpd,
-                    [{port, {{appid}}_deps:get_env(port, {{appid}}:default_port())}
-                     ,{server_name,  {{appid}}_deps:get_env(hostname, {{appid}}:hostname())}
-                     ,{server_root, "."}
-                     ,{document_root, {{appid}}_deps:get_env(doc_root,"./www")}
-                     ,{modules, [?MODULE]}
-                     ,{mime_types, [{"css",  "text/css"},
-                                    {"js",   "text/javascript"},
-                                    {"html", "text/html"}]}
+                    [{port,           {{appid}}:port()}
+                     ,{server_name,   {{appid}}:servername()}
+	             ,{bind_address,  {{appid}}:ip()}
+                     ,{server_root,   "."}
+                     ,{document_root, {{appid}}:docroot()}
+                     ,{modules,       [?MODULE]}
+                     ,{mime_types,    [{"css",  "text/css"},
+                                       {"js",   "text/javascript"},
+                                       {"html", "text/html"}]}
                     ]),
     link(Pid),
-    {ok, Pid}.
+    {ok, Pid};
+
+start_link(mochiweb) ->
+    mochiweb:start(),
+    Options = [{port,   {{appid}}:port()}
+               ,{name,  {{appid}}:servername()}
+               ,{ip,    {{appid}}:ip()}
+               ,{loop,  fun(Req) -> do_mochiweb(Req) end}
+              ],
+    {ok, Pid} = mochiweb_http:start(Options),
+    link(Pid),
+    {ok, Pid};
+
+start_link(yaws) ->
+    	SC = #sconf {
+		appmods     = [{"/", ?MODULE}],
+		docroot     = {{appid}}:docroot(),
+		port        = {{appid}}:port(),
+		servername  = {{appid}}:servername(),
+		listen      = {{appid}}:ip()
+	},
+	DefaultGC = yaws_config:make_default_gconf(false, {{appid}}),
+	GC = DefaultGC#gconf {
+		logdir = {{appid}}:log_dir(),
+		cache_refresh_secs = 5
+	},
+	% Following code adopted from yaws:start_embedded/4. 
+	% This will need to change if Yaws changes!!!
+	ok = application:set_env(yaws, embedded, true),
+	{ok, Pid} = yaws_sup:start_link(),
+	yaws_config:add_yaws_soap_srv(GC),
+	SCs = yaws_config:add_yaws_auth([SC]),
+	yaws_api:setconf(GC, [SCs]),
+	{ok, Pid}.
+
 
 stop() ->
     httpd:stop_service({any, {{appid}}_deps:get_env(port, {{appid}}:default_port())}),
     replace_route_handler(),
     nitrogen:run().
 
+out(Info) ->
+    RequestBridge = simple_bridge:make_request(yaws_request_bridge, Info),
+    ResponseBridge = simple_bridge:make_response(yaws_response_bridge, Info),
+    nitrogen:init_request(RequestBridge, ResponseBridge),
+    replace_route_handler(),
+    nitrogen:run().
+
+do_mochiweb(Info) ->
+    RequestBridge = simple_bridge:make_request(mochiweb_request_bridge, {Info,"./www"}),
+    ResponseBridge = simple_bridge:make_response(mochiweb_response_bridge, {Info, "./www"}),
+    nitrogen:init_request(RequestBridge, ResponseBridge),
+    replace_route_handler(),
+    nitrogen:run().
+
 replace_route_handler() ->
     wf_handler:set_handler(named_route_handler, routes()).

File nitro_sup.erl

 
     %Server = worker(server),
 
+    GettextServer = {gettext_server,{gettext_server,start_link,[{{appid}}]},
+                                    permanent,5000,worker,[gettext_server]},
+
     {ok, { {one_for_one, 10, 10}, 
-          [%Server
+          [%Server,
+           GettextServer
           ]} }.
 
 

File nitrodep.inc

 NITROGEN_EBIN=$NITROGEN_TOP_DIR/apps/nitrogen/ebin
 SIMPLE_BRIDGE_EBIN=$NITROGEN_TOP_DIR/apps/simple_bridge/ebin
 NPROCREG_EBIN=$NITROGEN_TOP_DIR/apps/nprocreg/ebin
+MOCHIWEB_EBIN=$NITROGEN_TOP_DIR/apps/mochiweb/ebin
+YAWS_DIR=$NITROGEN_TOP_DIR/apps/yaws
+YAWS_EBIN=${YAWS_DIR}/ebin
 EOPENID_EBIN=
+REDBUG_EBIN=
+TRANE_EBIN=
+IBROWSE_EBIN=
+GETTEXT_EBIN={{gettext_dir}}/ebin
+GETTEXT_PO_DIR=./priv
+GETTEXT_DEF_LANG=en
+GETTEXT_TMP_NAME=tmp
 
+
+

File nitrostart.sh

 erl \\
     -sname ${NAME} \\
     -pa ./ebin ${NITROGEN_EBIN} ${SIMPLE_BRIDGE_EBIN} ${NPROCREG_EBIN} \\
-        ${EOPENID_EBIN} \\
-    -pa ./ebin ${NITROGEN_EBIN} ${SIMPLE_BRIDGE_EBIN} ${NPROCREG_EBIN} \\
+        ${EOPENID_EBIN} ${GETTEXT_EBIN} \\
     -eval "application:start(nprocreg)" \\
     -eval "application:start({{appid}})"