第四章 使用系统类提供国家语言支持 - %SYS.NLS类

115 阅读1分钟

第四章 使用系统类提供国家语言支持 - %SYS.NLS类

更改日期和时间显示

%SYS.NLS.Format 类包含属性 DateSeparatorTimeSeparator,例如分别保存用于分隔日期和时间项的组件的字符。在美国的默认区域设置 enu8(或 Unicode 系统的 enuw)中,它们分别是斜杠字符 (/) 和冒号 (:)。以下示例显示了如何更改这些:

  // display the current defaults
  // date is 10 April 2005
  // time is 6 minutes 40 seconds after 11 in the morning
  Write $ZDATE("60000,40000"), !

  // now change the separators and display it again
  Set fmt = ##class(%SYS.NLS.Format).%New()
  Set fmt.DateSeparator = "_"
  Set fmt.TimeSeparator = "^"
  Write !, $ZDATE("60000,40000")

以下示例将月份名称更改为字母表中的连续字母(出于演示目的)。为此,它将属性 MonthName 设置为以空格分隔的月份名称列表。请注意,列表以空格开头:

  // get the format class instance
  Set fmt = ##class(%SYS.NLS.Format).%New()

  // define the month names
  Set Names = " AAA BBB CCC DDD EEE FFF GGG HHH III JJJ KKK LLL"
  Set fmt.MonthAbbr = Names
  Set rtn = ##class(%SYS.NLS.Format).SetFormatItem("DATEFORMAT", 2)

  // show the result
  Write $ZDATE(60000, 2)

更改数字的显示方式

%SYS.NLS.Format中的某些属性控制 $Number() 解释数字的方式。在英语语言环境中,小数点用于分隔整数和数字的小数部分,逗号用于分隔 3 位数字组。这也可以改变:

  // give the baseline display
  Write $Number("123,456.78"), !

  Set fmt = ##class(%SYS.NLS.Format).%New()
  // use "/" for groups of digits
  Set fmt.NumericGroupSeparator = "."

  // group digits in blocks of 4
  Set fmt.NumericGroupSize = 4

  // use ":" for separating integer and fractional parts
  Set fmt.DecimalSeparator = ","

  // try interpreting again
  Write $Number("12.3456,78"), !

设置文件的翻译

下面显示了应用程序可以控制写入文件的数据的表示形式。

  // show the process default translation (RAW, no translation performed)
  Set Tbl = ##class(%SYS.NLS.Table).%New("Process")
  Write "Process default translation: ", Tbl.File, !

  // create and open a temporary file
  // use XML for the translation
  Set TempName = ##class(%Library.File).TempFilename("log")
  Set TempFile = ##class(%Library.File).%New(TempName)
  Do TempFile.Open("WSNK\XML\")
  Write "Temp file: ", TempFile.CanonicalNameGet(), !

  // write a few characters to show the translation
  // then close it
  Do TempFile.WriteLine(("--" _ $CHAR(38) _ "--"))
  Do TempFile.Close()

  // now re-open it in raw mode and show content
  Do TempFile.Open("RSK\RAW\")
  Do TempFile.Rewind()
  Set MaxChars = 50
  Set Line = TempFile.Read(.MaxChars)
  Write "Contents: """, Line, """", !

  // finish
  Do TempFile.Close()
  Do ##class(%Library.File).Delete(TempName)
  Set TempFile = ""