i18n.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // @ts-ignore
  2. const userLangCode: string = (navigator.language || navigator.userLanguage || 'en').toLowerCase();
  3. const userLang: string = userLangCode.split('-')[0];
  4. const translations: { [lang: string]: { [msgid: string]: string } } = {
  5. "en": {
  6. "disconnected_with_server": "Disconnected from the server, please refresh the page",
  7. "connect_fail": "Failed to connect to server!",
  8. "error_in_input": "There is an error with the input, please fix the error first",
  9. "file_size_exceed": 'File "%1" size exceeds limit: the size of a single file must not exceed %2',
  10. "file_total_size_exceed": "The total file size exceeds the limit: the total file size must not exceed %1",
  11. "submit": "Submit",
  12. "reset": "Reset",
  13. "cancel": "Cancel",
  14. "duplicated_pin_name": "App Error: Duplicated pin widget name '%1'",
  15. },
  16. "zh": {
  17. "disconnected_with_server": "与服务器连接已断开,请刷新页面重新操作",
  18. "connect_fail": "连接服务器失败!",
  19. "error_in_input": "输入项存在错误,请消除错误后再提交",
  20. "file_size_exceed": '文件"%1"大小超过限制: 单个文件大小不超过%2',
  21. "file_total_size_exceed": "文件总大小超过限制: 文件总大小不超过%1",
  22. "submit": "提交",
  23. "reset": "重置",
  24. "cancel": "取消",
  25. "duplicated_pin_name": "应用错误: 输出了相同name的pin widget '%1'",
  26. },
  27. };
  28. // sprintf equivalent, takes a string and some arguments to make a computed string
  29. // eg: strfmt("%1 dogs are in %2", 7, "the kitchen"); => "7 dogs are in the kitchen"
  30. // eg: strfmt("I like %1, bananas and %1", "apples"); => "I like apples, bananas and apples"
  31. function strfmt(fmt: string) {
  32. let args = arguments;
  33. return fmt
  34. // put space after double % to prevent placeholder replacement of such matches
  35. .replace(/%%/g, '%% ')
  36. // replace placeholders
  37. .replace(/%(\d+)/g, function (str, p1) {
  38. return args[p1];
  39. })
  40. // replace double % and space with single %
  41. .replace(/%% /g, '%')
  42. }
  43. export function t(msgid: string, ...args:string[]): string {
  44. let fmt = null;
  45. for (let lang of [userLangCode, userLang, 'en']) {
  46. if (translations[lang] && translations[lang][msgid]){
  47. fmt = translations[lang][msgid];
  48. break;
  49. }
  50. }
  51. if (fmt === null)
  52. throw Error(`No translation for "${msgid}" in "${userLangCode}"`);
  53. return strfmt.apply(null, [fmt, ...args]);
  54. }