Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import i18n from "i18next";
  2. import { initReactI18next } from "react-i18next";
  3. import moment from "moment";
  4. import en from "./en";
  5. import dk from "./dk";
  6.  
  7. i18n
  8.   .use(initReactI18next) // passes i18n down to react-i18next
  9.   .init({
  10.     resources: {
  11.       en,
  12.       dk
  13.     },
  14.     lng: "en",
  15.     fallbackLng: "en",
  16.     keySeparator: false, // we do not use keys in form messages.welcome
  17.  
  18.     interpolation: {
  19.       escapeValue: false, // react already safes from xss
  20.       // parse format string, it should match the following pattern
  21.       // {{value, formatter, key1 = value1, key2 = value2, keyN = valueN}}
  22.       format(value, format, lng) {
  23.         const parts = format.split(",");
  24.         const formatterName = parts.shift();
  25.         const params = parts
  26.           .map(x => x.split("=").map(y => y.trim()))
  27.           .reduce((obj, pair) => {
  28.             if (pair.length > 1) {
  29.               obj[pair[0]] = pair[1];
  30.             } else {
  31.               obj[pair[0]] = pair[0];
  32.             }
  33.             return obj;
  34.           }, {});
  35.         const formatter = formatters[formatterName];
  36.         if (formatter) {
  37.           return formatter(value, params, parts.join(","), lng);
  38.         }
  39.         return value;
  40.       }
  41.     }
  42.   });
  43.  
  44. export default i18n;
  45.  
  46. const formatters = {
  47.   /**
  48.    * date formatter
  49.    * @param value
  50.    * @param params
  51.    * @param format
  52.    * @return {string}
  53.    */
  54.   date(value, params, format) {
  55.     switch (value) {
  56.       case "now":
  57.         value = moment();
  58.         break;
  59.       case "yesterday":
  60.         value = moment().add(-1, "d");
  61.         break;
  62.       case "tomorrow":
  63.         value = moment().add(1, "d");
  64.         break;
  65.     }
  66.     return moment(value).format(format);
  67.   },
  68.   /**
  69.    * amount formatter
  70.    * @param value
  71.    * @param zero
  72.    * @param many
  73.    * @param others
  74.    * @return {string}
  75.    */
  76.   amount(value, { zero, many, others }) {
  77.     if (!value && zero) return i18n.t(zero);
  78.     if (value > 1 && many) return i18n.t(many, { value });
  79.     return i18n.t(others, { value });
  80.   },
  81.   /**
  82.    * list formatter
  83.    * @param list    list of string
  84.    * @param empty   indicate the text to display if no item
  85.    * @param others  indicate the text to display for the rest of items
  86.    * @param x       indicate first items should be display (default = 1)
  87.    * @param sep     indicate list item separator (default = comma)
  88.    * @return {string|string | *|*}
  89.    *
  90.    * Samples:
  91.    * users = ['user1', 'user2', 'user3']
  92.    * {{users, list, empty=noUser, others=otherUsers, x=2}}
  93.    * noUser: No user
  94.    * otherUsers: and {{count}} other(s)
  95.    * output: user1, user2 and 1 other(s)
  96.    */
  97.   list(list, { empty, others, x = 1, sep = "," }) {
  98.     // show empty text if list is empty
  99.     if (!list || !list.length) return i18n.t(empty);
  100.     // show first item value if list has only one item
  101.     if (list.length === 1) return list[0];
  102.     // show first x item values if list has more than x items
  103.     return (
  104.       list.slice(0, x).join(sep) + i18n.t(others, { count: list.length - 1 })
  105.     );
  106.   }
  107. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement