KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Creating Macros that work regardless of 4D Localization
PRODUCT: 4D | VERSION: 11 | PLATFORM: Mac & Win
Published On: June 24, 2010

A great use for macros is for auto entry of commonly repeated code. This can be applied to method shells as well as code snippets. If you are developing code that is going to be localized in many languages, the drawback to this practice is that the macro is going to enter the code in the language it was created in regardless of the localization of 4D.

If you write the following macro it will come out in English and the local developer will have the hand correct each 4D command:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE macros SYSTEM "https://www.4d.com/dtd/2007/macros.dtd">
<macros>
<macro name="Cal4D On Web Authentication">
<text>
If (False)
`*****************************************************************************
`
` On Web Authentication
`
` $0 - C_BOOLEAN - Success or Failure
` $1 - C_TEXT - URL
` $2 - C_TEXT - Header
` $3 - C_TEXT - Client IP
` $4 - C_TEXT - Server IP
` $5 - C_TEXT - User
` $6 - C_TEXT - Password
`
`*****************************************************************************
End if
C_TEXT($MethodName_T)
$MethodName_T:=Current method name(684)
`===================== Declare Variables ==================================
`method_parameters_declarations
C_BOOLEAN($0;$acceptConnection_b)
C_TEXT($url_t;$1)
C_TEXT($header_t;$2)")
C_TEXT($clientIP_t;$3)
C_TEXT($serverIP_t;$4)
C_TEXT($user_t;$5)
C_TEXT($pass_t;$6)
`--------------------------------------------------------------------------------
`method_wide_constants_declarations
`--------------------------------------------------------------------------------
`local_variable_declarations
`====================== Initialize and Setup ================================

$url_t:=$1
$header_t:=$2
$clientIP_t:=$3
$serverIP_t:=$4
$user_t:=$5
$pass_t:=$6

`======================== Method Actions ==================================

$acceptConnection_b:=My_OnWebAuth_Method

`======================== Clean up and Exit =================================

$0:=$acceptConnection_b
</text>
</macro>
</macros>


If your application will be localized to other languages you need code that localizes. 4D provides a nice solution for this problem. Below is the above macro written for the same method, On Web Authentication, that when entered will work regardless of the localization of 4D. The commands EXECUTE FORMULA and Command name are the same regardless of localization.

The following macro will work in any language:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE macros SYSTEM "https://www.4d.com/dtd/2007/macros.dtd">
<macros>
<macro name="Cal4D On Web Authentication">
<text>
If (False)
`*****************************************************************************
`
` On Web Authentication
`
` $0 - C_BOOLEAN - Success or Failure
` $1 - C_TEXT - URL
` $2 - C_TEXT - Header
` $3 - C_TEXT - Client IP
` $4 - C_TEXT - Server IP
` $5 - C_TEXT - User
` $6 - C_TEXT - Password
`
`*****************************************************************************
End if
EXECUTE FORMULA (Command name(284)+"($MethodName_T)") `C_TEXT
EXECUTE FORMULA("$MethodName_T:="+Command name(684)) `Current method name
`===================== Declare Variables ==================================
`method_parameters_declarations
EXECUTE FORMULA(Command name(305)+"($0;$acceptConnection_b)") `C_BOOLEAN
EXECUTE FORMULA(Command name(284)+"($url_t;$1)") `C_TEXT"
EXECUTE FORMULA(Command name(284)+"($header_t;$2)") `C_TEXT"
EXECUTE FORMULA(Command name(284)+"($clientIP_t;$3)") `C_TEXT"
EXECUTE FORMULA(Command name(284)+"($serverIP_t;$4)") `C_TEXT"
EXECUTE FORMULA(Command name(284)+"($user_t;$5)") `C_TEXT"
EXECUTE FORMULA(Command name(284)+"($pass_t;$6)") `C_TEXT"
`--------------------------------------------------------------------------------
`method_wide_constants_declarations
`--------------------------------------------------------------------------------
`local_variable_declarations
`====================== Initialize and Setup ================================

$url_t:=$1
$header_t:=$2
$clientIP_t:=$3
$serverIP_t:=$4
$user_t:=$5
$pass_t:=$6

`======================== Method Actions ==================================

$acceptConnection_b:=My_OnWebAuth_Method

`======================== Clean up and Exit =================================

$0:=$acceptConnection_b
</text>
</macro>
</macros>


Here is the resulting code from that macro that will run regardless of localization:

If (False)
    `*****************************************************************************
    `
    ` On Web Authentication
    `
    ` $0 - C_BOOLEAN - Success or Failure
    ` $1 - C_TEXT - URL
    ` $2 - C_TEXT - Header
    ` $3 - C_TEXT - Client IP
    ` $4 - C_TEXT - Server IP
    ` $5 - C_TEXT - User
    ` $6 - C_TEXT - Password
    `
    `*****************************************************************************

End if
EXECUTE FORMULA(Command name(284)+"($MethodName_T)") `C_TEXT
EXECUTE FORMULA("$MethodName_T:="+Command name(684)) `Current method name
   `===================== Declare Variables ==================================
   `method_parameters_declarations
EXECUTE FORMULA(Command nameme(305)+"($0;$acceptConnection_b)") `C_BOOLEAN
EXECUTE FORMULA(Command name(284)+"($url_t;$1)") `C_TEXT
EXECUTE FORMULA(Command name(284)+"($header_t;$2)") `C_TEXT
EXECUTE FORMULA(Command name(284)+"($clientIP_t;$3)") `C_TEXT
EXECUTE FORMULA(Command name(284)+"($serverIP_t;$4)") `C_TEXT
EXECUTE FORMULA(Command name(284)+"($user_t;$5)") `C_TEXT
EXECUTE FORMULA(Command name(284)+"($pass_t;$6)") `C_TEXT
   `--------------------------------------------------------------------------------
   `method_wide_constants_declarations
   `--------------------------------------------------------------------------------
   `local_variable_declarations
   `====================== Initialize and Setup ================================

$url_t:=$1
$header_t:=$2
$clientIP_t:=$3
$serverIP_t:=$4
$user_t:=$5
$pass_t:=$6

   `======================== Method Actions ==================================

$acceptConnection_b:=My_OnWebAuth_Method

   `======================== Clean up and Exit =================================

$0:=$acceptConnection_b