From: Jürg Billeter Date: Tue, 8 Feb 2011 19:32:35 +0000 (+0100) Subject: Support translated string constants X-Git-Tag: 0.11.6~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7c2fd8a2a9d9503496c96854be95fa7f91ef44a6;p=thirdparty%2Fvala.git Support translated string constants Fixes bug 641543. --- diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala index 14258bc23..328de67ef 100644 --- a/codegen/valaccodebasemodule.vala +++ b/codegen/valaccodebasemodule.vala @@ -1,6 +1,6 @@ /* valaccodebasemodule.vala * - * Copyright (C) 2006-2010 Jürg Billeter + * Copyright (C) 2006-2011 Jürg Billeter * Copyright (C) 2006-2008 Raffaele Sandrini * * This library is free software; you can redistribute it and/or @@ -3424,6 +3424,17 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator { public override void visit_string_literal (StringLiteral expr) { set_cvalue (expr, new CCodeConstant.string (expr.value.replace ("\n", "\\n"))); + + if (expr.translate) { + // translated string constant + + var m = (Method) root_symbol.scope.lookup ("GLib").scope.lookup ("_"); + add_symbol_declaration (cfile, m, m.get_cname ()); + + var translate = new CCodeFunctionCall (new CCodeIdentifier ("_")); + translate.add_argument (get_cvalue (expr)); + set_cvalue (expr, translate); + } } public override void visit_regex_literal (RegexLiteral expr) { diff --git a/vala/valaconstant.vala b/vala/valaconstant.vala index fab450c17..c9b880cb5 100644 --- a/vala/valaconstant.vala +++ b/vala/valaconstant.vala @@ -203,6 +203,21 @@ public class Vala.Constant : Symbol, Lockable { return false; } + // support translated string constants for efficiency / convenience + // even though the expression is not a compile-time constant + var call = value as MethodCall; + if (call != null) { + var method_type = call.call.value_type as MethodType; + if (method_type != null && method_type.method_symbol.get_full_name () == "GLib._") { + // first argument is string + var literal = call.get_argument_list ().get (0) as StringLiteral; + if (literal != null) { + value = literal; + literal.translate = true; + } + } + } + if (!value.is_constant ()) { error = true; Report.error (value.source_reference, "Value must be constant"); diff --git a/vala/valastringliteral.vala b/vala/valastringliteral.vala index 69fc4d79b..ffb4285be 100644 --- a/vala/valastringliteral.vala +++ b/vala/valastringliteral.vala @@ -1,6 +1,6 @@ /* valastringliteral.vala * - * Copyright (C) 2006-2010 Jürg Billeter + * Copyright (C) 2006-2011 Jürg Billeter * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -31,6 +31,8 @@ public class Vala.StringLiteral : Literal { */ public string value { get; set; } + public bool translate { get; set; } + /** * Creates a new string literal. *