KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method: Get All 4D Constants
PRODUCT: 4D | VERSION: 13.0 | PLATFORM: Mac & Win
Published On: May 25, 2012

This method can be used to obtain an array of all 4D constant names and ID's.

The idea behind the method is to use the XLIFF file containing all 4D constants to find the names and IDs.

Note: it is recommended to copy and paste this code into 4D as there are several long statements. Also this method only works with the US version of 4D.

// Get all 4D constant names and IDs.
C_POINTER($1;$constantNames_p_at)
C_POINTER($2;$constantIDs_p_at)

C_TIME($constantsXLIFFFile_h)
C_TEXT($constantsXLIFFFile_t)
C_TEXT($elementName_t;$elementPrefix_t;$constantName_t;$constantID_t)
C_BOOLEAN($transUnitFound_f;$targetFound_f)
C_LONGINT($SAXEvent_l;$pos_l)

ARRAY TEXT($attributeNames_at;0)
ARRAY TEXT($attributeVals_at;0)

$constantNames_p_at:=$1
$constantIDs_p_at:=$2

$constantsXLIFFFile_t:=UTIL_PATH_LongNameToPath (Application file)\
  +"Resources"+Folder separator+"en.lproj"+Folder separator+"4D_ConstantsEN.xlf"

$constantsXLIFFFile_h:=Open document($constantsXLIFFFile_t;Read Mode)

If (OK=1)

   Repeat
      $SAXEvent_l:=SAX Get XML node($constantsXLIFFFile_h)
      Case of

         : ($SAXEvent_l=XML Start Element)
         SAX GET XML ELEMENT($constantsXLIFFFile_h;$elementName_t;\
          $elementPrefix_t;$attributeNames_at;$attributeVals_at)

          // Need to locate the "target" element for each constant "trans-unit".
         Case of
            : ($elementName_t="trans-unit")
            $pos_l:=Find in array($attributeNames_at;"id")
            If ($pos_l>0)
               $constantID_t:=$attributeVals_at{$pos_l}
                // Ignore the first trans-unit in each group, it's not a constant.
               If ($constantID_t#"@_0")
                  $transUnitFound_f:=True
               End if
            End if

            : ($elementName_t="target")
            If ($transUnitFound_f)
               $targetFound_f:=True
               $transUnitFound_f:=False
            End if
         End case

         : ($SAXEvent_l=XML DATA)
          // With the target located, we have the constant name.
         If ($targetFound_f)
            SAX GET XML ELEMENT VALUE($constantsXLIFFFile_h;$constantName_t)
            APPEND TO ARRAY($constantNames_p_at->;$constantName_t)
            APPEND TO ARRAY($constantIDs_p_at->;$constantID_t)
            $constantName_t:=""
            $constantID_t:=""
            $targetFound_f:=False
         End if

      End case
   Until ($SAXEvent_l=XML End Document)

   CLOSE DOCUMENT($constantsXLIFFFile_h)
End if


The above method relies on this code (UTIL_PATH_LongNameToPath) to obtain the path to the XLIFF file:

// From: http://doc.4d.com/4Dv13/help/Title/en/page737.html
//
// Updated to use constant for path separator.
//
// Long name to path name ( String ) -> String
// Long name to path name ( Long file name ) -> Path name
C_STRING(255;$1;$0)
C_STRING(1;$vsDirSymbol)
C_INTEGER($viLen;$viPos;$viChar;$viDirSymbol)

$viDirSymbol:=Character code(Folder separator)
$viLen:=Length($1)
$viPos:=0
For ($viChar;$viLen;1;-1)
   If (Character code($1[[$viChar]])=$viDirSymbol)
      $viPos:=$viChar
      $viChar:=0
   End if
End for
If ($viPos>0)
   $0:=Substring($1;1;$viPos)
Else
   $0:=$1
End if