locales.ts 14 KB

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