@ray-js/t-agent-ui-ray 0.1.0-beta-4 → 0.1.0-beta-6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,13 +13,11 @@ import imageSvg from './icons/image.svg';
13
13
  import videoSvg from './icons/video.svg';
14
14
  import loadingSvg from './icons/loading.svg';
15
15
  import closeCircleSvg from './icons/close-circle.svg';
16
- import { useAttachmentInput, useChatAgent, useEmitEvent, useIsUnmounted, useOnEvent, useRenderOptions } from '../hooks';
16
+ import { useAttachmentInput, useChatAgent, useEmitEvent, useIsUnmounted, useOnEvent, useRenderOptions, useTranslate } from '../hooks';
17
17
  import AsrInput from './AsrInput';
18
18
  export default function MessageInput(props) {
19
19
  const [moreOpen, setMoreOpen] = useState(false);
20
- const {
21
- i18nTranslate: t
22
- } = useRenderOptions();
20
+ const t = useTranslate();
23
21
  const [text, setText] = useState('');
24
22
  const [asrText, setAsrText] = useState('');
25
23
  const attachmentInput = useAttachmentInput();
@@ -4,3 +4,4 @@ export * from './useAsrInput';
4
4
  export * from './useSleep';
5
5
  export * from './useIsUnmounted';
6
6
  export * from './useLongPress';
7
+ export * from './useTranslate';
@@ -3,4 +3,5 @@ export * from './useAttachmentInput';
3
3
  export * from './useAsrInput';
4
4
  export * from './useSleep';
5
5
  export * from './useIsUnmounted';
6
- export * from './useLongPress';
6
+ export * from './useLongPress';
7
+ export * from './useTranslate';
@@ -5,8 +5,8 @@ import "core-js/modules/esnext.iterator.map.js";
5
5
  import "core-js/modules/web.dom-collections.iterator.js";
6
6
  import { useState, useEffect, useCallback } from 'react';
7
7
  import logger from '../logger';
8
- import { useRenderOptions } from './context';
9
- import { useChatAgent } from '.';
8
+ import { useChatAgent } from './context';
9
+ import { useTranslate } from './useTranslate';
10
10
 
11
11
  /**
12
12
  * 预定义动作类型
@@ -34,9 +34,7 @@ export function useLongPress() {
34
34
  }
35
35
  });
36
36
  const agent = useChatAgent();
37
- const {
38
- i18nTranslate: t
39
- } = useRenderOptions();
37
+ const t = useTranslate();
40
38
  const [screenSize, setScreenSize] = useState({
41
39
  width: 0,
42
40
  height: 0
@@ -124,11 +122,20 @@ export function useLongPress() {
124
122
  menuItems.push({
125
123
  key: 'like',
126
124
  label: t('t-agent.message.action.like'),
127
- action: () => {
128
- ty.showToast({
129
- title: t('t-agent.message.like.success'),
130
- icon: 'none'
131
- });
125
+ action: async () => {
126
+ const {
127
+ feedback
128
+ } = agent.plugins.assistant;
129
+ if (typeof feedback === 'function') {
130
+ await feedback({
131
+ requestId: message.id,
132
+ type: 'like'
133
+ });
134
+ ty.showToast({
135
+ title: t('t-agent.message.like.success'),
136
+ icon: 'none'
137
+ });
138
+ }
132
139
  }
133
140
  });
134
141
  break;
@@ -136,11 +143,20 @@ export function useLongPress() {
136
143
  menuItems.push({
137
144
  key: 'unlike',
138
145
  label: t('t-agent.message.action.unlike'),
139
- action: () => {
140
- ty.showToast({
141
- title: t('t-agent.message.unlike.success'),
142
- icon: 'none'
143
- });
146
+ action: async () => {
147
+ const {
148
+ feedback
149
+ } = agent.plugins.assistant;
150
+ if (typeof feedback === 'function') {
151
+ await feedback({
152
+ requestId: message.id,
153
+ type: 'unlike'
154
+ });
155
+ ty.showToast({
156
+ title: t('t-agent.message.unlike.success'),
157
+ icon: 'none'
158
+ });
159
+ }
144
160
  }
145
161
  });
146
162
  break;
@@ -0,0 +1,2 @@
1
+ export declare const translateWith: (key: string, i18nTranslate: (key: string) => string) => string;
2
+ export declare const useTranslate: () => (key: string) => string;
@@ -0,0 +1,62 @@
1
+ import "core-js/modules/es.regexp.exec.js";
2
+ import "core-js/modules/web.dom-collections.iterator.js";
3
+ import { useRenderOptions } from './context';
4
+ import { useCallback } from 'react';
5
+ import strings from '../i18n/strings';
6
+ const keyRE = /^t-agent\./;
7
+ const langRE = {
8
+ 'zh-Hans': /^zh([-_](hans|cn|zh))/i,
9
+ 'zh-Hant': /^zh([-_](hant|tw|hk|mo))/i,
10
+ en: /^en([-_](us|gb))/i,
11
+ ja: /^ja([-_](jp))/i,
12
+ de: /^de([-_](de))/i,
13
+ fr: /^fr([-_](fr))/i,
14
+ es: /^es([-_](es))/i,
15
+ it: /^it([-_](it))/i
16
+ };
17
+ const keys = Object.keys(langRE);
18
+ const cache = new Map();
19
+ const normalizeLanguage = lang => {
20
+ if (langRE[lang]) {
21
+ return lang;
22
+ }
23
+ if (cache.has(lang)) {
24
+ return cache.get(lang);
25
+ }
26
+ for (const key of keys) {
27
+ if (langRE[key].test(lang)) {
28
+ cache.set(lang, key);
29
+ return key;
30
+ }
31
+ }
32
+ cache.set(lang, 'en');
33
+ // 默认返回英文
34
+ return 'en';
35
+ };
36
+ const translateCache = new Map();
37
+ export const translateWith = (key, i18nTranslate) => {
38
+ if (translateCache.has(key)) {
39
+ return translateCache.get(key);
40
+ }
41
+ const result = i18nTranslate(key);
42
+ // result 全等于 key 说明没有翻译,使用内置翻译
43
+ if (result === key && keyRE.test(key)) {
44
+ var _strings$language;
45
+ let {
46
+ language
47
+ } = ty.getSystemInfoSync();
48
+ language = normalizeLanguage(language);
49
+ if ((_strings$language = strings[language]) !== null && _strings$language !== void 0 && _strings$language[key]) {
50
+ translateCache.set(key, strings[language][key]);
51
+ return strings[language][key];
52
+ }
53
+ }
54
+ translateCache.set(key, result);
55
+ return result;
56
+ };
57
+ export const useTranslate = () => {
58
+ const {
59
+ i18nTranslate
60
+ } = useRenderOptions();
61
+ return useCallback(key => translateWith(key, i18nTranslate), [i18nTranslate]);
62
+ };