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

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,7 @@ 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, useTranslate } from '.';
10
9
 
11
10
  /**
12
11
  * 预定义动作类型
@@ -34,9 +33,7 @@ export function useLongPress() {
34
33
  }
35
34
  });
36
35
  const agent = useChatAgent();
37
- const {
38
- i18nTranslate: t
39
- } = useRenderOptions();
36
+ const t = useTranslate();
40
37
  const [screenSize, setScreenSize] = useState({
41
38
  width: 0,
42
39
  height: 0
@@ -124,11 +121,20 @@ export function useLongPress() {
124
121
  menuItems.push({
125
122
  key: 'like',
126
123
  label: t('t-agent.message.action.like'),
127
- action: () => {
128
- ty.showToast({
129
- title: t('t-agent.message.like.success'),
130
- icon: 'none'
131
- });
124
+ action: async () => {
125
+ const {
126
+ feedback
127
+ } = agent.plugins.assistant;
128
+ if (typeof feedback === 'function') {
129
+ await feedback({
130
+ requestId: message.id,
131
+ type: 'like'
132
+ });
133
+ ty.showToast({
134
+ title: t('t-agent.message.like.success'),
135
+ icon: 'none'
136
+ });
137
+ }
132
138
  }
133
139
  });
134
140
  break;
@@ -136,11 +142,20 @@ export function useLongPress() {
136
142
  menuItems.push({
137
143
  key: 'unlike',
138
144
  label: t('t-agent.message.action.unlike'),
139
- action: () => {
140
- ty.showToast({
141
- title: t('t-agent.message.unlike.success'),
142
- icon: 'none'
143
- });
145
+ action: async () => {
146
+ const {
147
+ feedback
148
+ } = agent.plugins.assistant;
149
+ if (typeof feedback === 'function') {
150
+ await feedback({
151
+ requestId: message.id,
152
+ type: 'unlike'
153
+ });
154
+ ty.showToast({
155
+ title: t('t-agent.message.unlike.success'),
156
+ icon: 'none'
157
+ });
158
+ }
144
159
  }
145
160
  });
146
161
  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
+ };