locales.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. import type { FormKitValidationMessages } from '@formkit/validation'
  3. import { FormKitLocale } from '@formkit/i18n'
  4. import { i18n } from '@common/i18n'
  5. import { commaSeparatedList, order } from '@common/utils/formatter'
  6. interface FormKitLocaleExtended extends FormKitLocale {
  7. validation: FormKitValidationMessages
  8. }
  9. // TODO: Use translateLabel for all validation messages if we stay with the labels inside of the messages. It's a open
  10. // question if we want use them inside of the messages.
  11. const loadLocales = (): FormKitLocaleExtended => {
  12. return {
  13. ui: {
  14. /**
  15. * Shown when a button to remove items is visible.
  16. */
  17. remove: () => i18n.t('Remove'),
  18. /**
  19. * Shown when there are multiple items to remove at the same time.
  20. */
  21. removeAll: () => i18n.t('Remove all'),
  22. /**
  23. * Shown when all fields are not filled out correctly.
  24. */
  25. incomplete: () =>
  26. i18n.t('Sorry, not all fields are filled out correctly.'),
  27. /**
  28. * Shown in a button inside a form to submit the form.
  29. */
  30. submit: () => i18n.t('Submit'),
  31. /**
  32. * Shown when no files are selected.
  33. */
  34. noFiles: () => i18n.t('No file chosen.'),
  35. },
  36. validation: {
  37. /**
  38. * The value is not an accepted value.
  39. * @see {@link https://docs.formkit.com/essentials/validation#accepted}
  40. */
  41. accepted({ name }) {
  42. /* <i18n case="Shown when the user-provided value is not a valid 'accepted' value."> */
  43. return i18n.t('Please accept the %s.', name)
  44. /* </i18n> */
  45. },
  46. /**
  47. * The value is not letter and/or spaces
  48. * @see {@link https://docs.formkit.com/essentials/validation#alpha-spaces}
  49. */
  50. alpha_spaces() {
  51. /* <i18n case="Shown when the user-provided value contains non-alphabetical and non-space characters."> */
  52. return i18n.t('This field can only contain letters and spaces.')
  53. /* </i18n> */
  54. },
  55. /**
  56. * The date is not after
  57. * @see {@link https://docs.formkit.com/essentials/validation#date-after}
  58. */
  59. date_after({ args }) {
  60. if (Array.isArray(args) && args.length) {
  61. /* <i18n case="Shown when the user-provided date is not after the date supplied to the rule."> */
  62. return i18n.t(
  63. 'This field must have a value that is after %s.',
  64. i18n.date(args[0]),
  65. )
  66. /* </i18n> */
  67. }
  68. /* <i18n case="Shown when the user-provided date is not after today's date, since no date was supplied to the rule."> */
  69. return i18n.t('This field must have a value that is in the future.')
  70. /* </i18n> */
  71. },
  72. /**
  73. * The value is not a letter.
  74. * @see {@link https://docs.formkit.com/essentials/validation#alpha}
  75. */
  76. alpha() {
  77. /* <i18n case="Shown when the user-provided value contains non-alphabetical characters."> */
  78. return i18n.t('This field can only contain alphabetical characters.')
  79. /* </i18n> */
  80. },
  81. /**
  82. * The value is not alphanumeric
  83. * @see {@link https://docs.formkit.com/essentials/validation#alphanumeric}
  84. */
  85. alphanumeric() {
  86. /* <i18n case="Shown when the user-provided value contains non-alphanumeric characters."> */
  87. return i18n.t('This field can only contain letters and numbers.')
  88. /* </i18n> */
  89. },
  90. /**
  91. * The date is not before
  92. * @see {@link https://docs.formkit.com/essentials/validation#date-before}
  93. */
  94. date_before({ args }) {
  95. if (Array.isArray(args) && args.length) {
  96. /* <i18n case="Shown when the user-provided date is not before the date supplied to the rule."> */
  97. return i18n.t(
  98. 'This field must have a value that is before %s.',
  99. i18n.date(args[0]),
  100. )
  101. /* </i18n> */
  102. }
  103. /* <i18n case="Shown when the user-provided date is not before today's date, since no date was supplied to the rule."> */
  104. return i18n.t('This field must have a value that is in the past.')
  105. /* </i18n> */
  106. },
  107. /**
  108. * The value is not between two numbers
  109. * @see {@link https://docs.formkit.com/essentials/validation#between}
  110. */
  111. between({ args }) {
  112. if (Number.isNaN(args[0]) || Number.isNaN(args[1])) {
  113. /* <i18n case="Shown when any of the arguments supplied to the rule were not a number."> */
  114. return i18n.t(
  115. "This field was configured incorrectly and can't be submitted.",
  116. )
  117. /* </i18n> */
  118. }
  119. const [first, second] = order(args[0], args[1])
  120. /* <i18n case="Shown when the user-provided value is not between two numbers."> */
  121. return i18n.t(
  122. 'This field must have a value that is between %s and %s.',
  123. first,
  124. second,
  125. )
  126. /* </i18n> */
  127. },
  128. /**
  129. * The confirmation field does not match
  130. * @see {@link https://docs.formkit.com/essentials/validation#confirm}
  131. */
  132. confirm() {
  133. /* <i18n case="Shown when the user-provided value does not equal the value of the matched input."> */
  134. return i18n.t("This field doesn't correspond to the expected value.")
  135. /* </i18n> */
  136. },
  137. /**
  138. * The value is not a valid date
  139. * @see {@link https://docs.formkit.com/essentials/validation#date-format}
  140. */
  141. date_format({ args }) {
  142. if (Array.isArray(args) && args.length) {
  143. /* <i18n case="Shown when the user-provided date does not satisfy the date format supplied to the rule."> */
  144. return i18n.t(
  145. 'This field isn\'t a valid date, please use the format "%s".',
  146. args[0],
  147. )
  148. /* </i18n> */
  149. }
  150. /* <i18n case="Shown when no date argument was supplied to the rule."> */
  151. return i18n.t(
  152. "This field was configured incorrectly and can't be submitted.",
  153. )
  154. /* </i18n> */
  155. },
  156. /**
  157. * Is not within expected date range
  158. * @see {@link https://docs.formkit.com/essentials/validation#date-between}
  159. */
  160. date_between({ args }) {
  161. /* <i18n case="Shown when the user-provided date is not between the start and end dates supplied to the rule. "> */
  162. return i18n.t(
  163. 'This field must have a value that is between %s and %s.',
  164. i18n.date(args[0]),
  165. i18n.date(args[1]),
  166. )
  167. /* </i18n> */
  168. },
  169. /**
  170. * Shown when the user-provided value is not a valid email address.
  171. * @see {@link https://docs.formkit.com/essentials/validation#email}
  172. */
  173. email: i18n.t('Please enter a valid email address.'),
  174. /**
  175. * Does not end with the specified value
  176. * @see {@link https://docs.formkit.com/essentials/validation#ends-with}
  177. */
  178. ends_with({ args }) {
  179. /* <i18n case="Shown when the user-provided value does not end with the substring supplied to the rule."> */
  180. return i18n.t(
  181. 'This field doesn\'t end with "%s".',
  182. commaSeparatedList(args),
  183. )
  184. /* </i18n> */
  185. },
  186. /**
  187. * Is not an allowed value
  188. * @see {@link https://docs.formkit.com/essentials/validation#is}
  189. */
  190. is() {
  191. /* <i18n case="Shown when the user-provided value is not one of the values supplied to the rule."> */
  192. return i18n.t("This field doesn't contain an allowed value.")
  193. /* </i18n> */
  194. },
  195. /**
  196. * Does not match specified length
  197. * @see {@link https://docs.formkit.com/essentials/validation#length}
  198. */
  199. length({ args: [first = 0, second = Infinity] }) {
  200. const min = Number(first) <= Number(second) ? first : second
  201. const max = Number(second) >= Number(first) ? second : first
  202. if (min === 1 && max === Infinity) {
  203. /* <i18n case="Shown when the length of the user-provided value is not at least one character."> */
  204. return i18n.t('This field must contain at least one character.')
  205. /* </i18n> */
  206. }
  207. if (min === 0 && max) {
  208. /* <i18n case="Shown when first argument supplied to the rule is 0, and the user-provided value is longer than the max (the 2nd argument) supplied to the rule."> */
  209. return i18n.t(
  210. 'This field must not contain more than %s characters.',
  211. max,
  212. )
  213. /* </i18n> */
  214. }
  215. if (min && max === Infinity) {
  216. /* <i18n case="Shown when the length of the user-provided value is less than the minimum supplied to the rule and there is no maximum supplied to the rule."> */
  217. return i18n.t('This field must contain at least %s characters.', min)
  218. /* </i18n> */
  219. }
  220. /* <i18n case="Shown when the length of the user-provided value is between the two lengths supplied to the rule."> */
  221. return i18n.t(
  222. 'This field must contain between %s and %s characters.',
  223. min,
  224. max,
  225. )
  226. /* </i18n> */
  227. },
  228. /**
  229. * Value is not a match
  230. * @see {@link https://docs.formkit.com/essentials/validation#matches}
  231. */
  232. matches() {
  233. /* <i18n case="Shown when the user-provided value does not match any of the values or RegExp patterns supplied to the rule. "> */
  234. return i18n.t("This field doesn't contain an allowed value.")
  235. /* </i18n> */
  236. },
  237. /**
  238. * Exceeds maximum allowed value
  239. * @see {@link https://docs.formkit.com/essentials/validation#max}
  240. */
  241. max({ node: { value }, args }) {
  242. if (Array.isArray(value)) {
  243. /* <i18n case="Shown when the length of the array of user-provided values is longer than the max supplied to the rule."> */
  244. return i18n.t("This field can't have more than %s entries.", args[0])
  245. /* </i18n> */
  246. }
  247. /* <i18n case="Shown when the user-provided value is greater than the maximum number supplied to the rule."> */
  248. return i18n.t(
  249. 'This field must have a value that is at most %s.',
  250. args[0],
  251. )
  252. /* </i18n> */
  253. },
  254. /**
  255. * The (field-level) value does not match specified mime type
  256. * @see {@link https://docs.formkit.com/essentials/validation#mime}
  257. */
  258. mime({ args }) {
  259. if (!args[0]) {
  260. /* <i18n case="Shown when no file formats were supplied to the rule."> */
  261. return i18n.t('No file formats allowed.')
  262. /* </i18n> */
  263. }
  264. /* <i18n case="Shown when the mime type of user-provided file does not match any mime types supplied to the rule."> */
  265. return i18n.t('This field must be of the type "%s".', args[0])
  266. /* </i18n> */
  267. },
  268. /**
  269. * Does not fulfill minimum allowed value
  270. * @see {@link https://docs.formkit.com/essentials/validation#min}
  271. */
  272. min({ node: { value }, args }) {
  273. if (Array.isArray(value)) {
  274. /* <i18n case="Shown when the length of the array of user-provided values is shorter than the min supplied to the rule."> */
  275. return i18n.t("This field can't have less than %s entries.", args[0])
  276. /* </i18n> */
  277. }
  278. /* <i18n case="Shown when the user-provided value is less than the minimum number supplied to the rule."> */
  279. return i18n.t(
  280. 'This field must have a value that is at least %s.',
  281. args[0],
  282. )
  283. /* </i18n> */
  284. },
  285. /**
  286. * Is not an allowed value
  287. * @see {@link https://docs.formkit.com/essentials/validation#not}
  288. */
  289. not({ node: { value } }) {
  290. /* <i18n case="Shown when the user-provided value matches one of the values supplied to (and thus disallowed by) the rule."> */
  291. return i18n.t(
  292. 'This field can\'t contain the value "%s".',
  293. value as string,
  294. )
  295. /* </i18n> */
  296. },
  297. /**
  298. * Is not a number
  299. * @see {@link https://docs.formkit.com/essentials/validation#number}
  300. */
  301. number() {
  302. /* <i18n case="Shown when the user-provided value is not a number."> */
  303. return i18n.t('This field must contain a number.')
  304. /* </i18n> */
  305. },
  306. /**
  307. * Required field.
  308. * @see {@link https://docs.formkit.com/essentials/validation#required}
  309. */
  310. required() {
  311. /* <i18n case="Shown when a user does not provide a value to a required input."> */
  312. return i18n.t('This field is required.')
  313. /* </i18n> */
  314. },
  315. /**
  316. * Does not start with specified value
  317. * @see {@link https://docs.formkit.com/essentials/validation#starts-with}
  318. */
  319. starts_with({ args }) {
  320. /* <i18n case="Shown when the user-provided value does not start with the substring supplied to the rule."> */
  321. return i18n.t(
  322. 'This field doesn\'t start with "%s".',
  323. commaSeparatedList(args),
  324. )
  325. /* </i18n> */
  326. },
  327. /**
  328. * Is not a url
  329. * @see {@link https://docs.formkit.com/essentials/validation#url}
  330. */
  331. url() {
  332. /* <i18n case="Shown when the user-provided value is not a valid url."> */
  333. return i18n.t('Please include a valid url.')
  334. /* </i18n> */
  335. },
  336. },
  337. }
  338. }
  339. export default loadLocales