hive-react-kit 0.9.2 → 0.9.4

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.
package/README.md CHANGED
@@ -10,6 +10,7 @@ A comprehensive React component library for building Hive blockchain application
10
10
  - 🎯 **TypeScript Support** - Full TypeScript support with type definitions
11
11
  - 🚀 **Tree Shaking** - Optimized bundle size with tree shaking support
12
12
  - 🎭 **Customizable** - Easy to customize and extend
13
+ - 🌐 **Content i18n** - Wrap once, every Hive post body / comment / feed preview translates to the user's language. See [docs/i18n.md](docs/i18n.md)
13
14
 
14
15
  ## Installation
15
16
 
@@ -415,6 +416,45 @@ See [docs/HiveContributionsLanding.md](docs/HiveContributionsLanding.md) for ful
415
416
  | 3Speak | https://3speak.tv/ |
416
417
  | HiveFestFacts | https://hivefestfacts.sagarkothari88.one/ |
417
418
 
419
+ ### Content Translation (i18n)
420
+
421
+ Wrap your app once with `<HiveLanguageProvider>` and every Hive content body / title / comment / feed preview rendered by the kit translates to the chosen language. Free MyMemory-backed translator by default, fully overridable for DeepL / Google / your own backend.
422
+
423
+ ```tsx
424
+ import { HiveLanguageProvider } from 'hive-react-kit';
425
+
426
+ <HiveLanguageProvider language="es">
427
+ <App />
428
+ </HiveLanguageProvider>
429
+ ```
430
+
431
+ **Auto-translates:** post bodies (`HiveDetailPost`, `InlineCommentItem`, `CommentTile`, `UserChannel`), feed-card titles + previews (`UserDetailProfile`), poll questions / previews / choice labels (`PollListItem`).
432
+
433
+ **Available exports:**
434
+
435
+ | Export | Purpose |
436
+ |--------|---------|
437
+ | `HiveLanguageProvider` | Wrap app once with `language="es"` (or any code MyMemory accepts). `language="en"` is no-op |
438
+ | `useHiveLanguage()` | Read current `{ language, translateHtml }` from context |
439
+ | `useTranslatedHtml(html)` | Hook returning `{ html, loading }` for translated rendered HTML |
440
+ | `useTranslatedText(text)` | Hook returning `{ text, loading }` for translated plain strings |
441
+ | `<TranslatedBody>` | `forwardRef`-aware drop-in for `<div … dangerouslySetInnerHTML>` |
442
+ | `<TranslatedText>` | Inline wrapper for plain strings inside `.map()` loops |
443
+ | `translateHtml`, `translateText` | Low-level helpers (rarely needed) |
444
+
445
+ **Custom translator** — pass `translateHtml` to override the bundled MyMemory implementation:
446
+
447
+ ```tsx
448
+ <HiveLanguageProvider
449
+ language={language}
450
+ translateHtml={async (html, target) => callDeepL(html, target)}
451
+ >
452
+ <App />
453
+ </HiveLanguageProvider>
454
+ ```
455
+
456
+ See [docs/i18n.md](docs/i18n.md) for the full provider API, hook signatures, custom-translator integration, caching layers, failure modes, and behaviour with code blocks / embeds.
457
+
418
458
  ### Feed Components
419
459
 
420
460
  - **PostFeedList** - Display a list of posts with sorting, filtering, and interaction capabilities
@@ -0,0 +1,20 @@
1
+ import React from "react";
2
+ export interface TranslatedBodyProps {
3
+ /** Rendered post / comment body HTML (output of `createHiveRenderer`). */
4
+ html: string;
5
+ className?: string;
6
+ onClick?: React.MouseEventHandler<HTMLDivElement>;
7
+ }
8
+ /**
9
+ * Drop-in replacement for
10
+ * `<div className="…" onClick={…} dangerouslySetInnerHTML={{ __html: html }} />`
11
+ * that translates the body into the language supplied by the nearest
12
+ * `<HiveLanguageProvider>`. Forwards refs to the inner div so existing
13
+ * click-delegation / image-fallback logic continues to attach correctly.
14
+ *
15
+ * While translation is in flight the original HTML is shown and the
16
+ * `data-translating="true"` attribute is set on the div so consumers
17
+ * can apply a CSS-only loading style if they want.
18
+ */
19
+ export declare const TranslatedBody: React.ForwardRefExoticComponent<TranslatedBodyProps & React.RefAttributes<HTMLDivElement>>;
20
+ export default TranslatedBody;
@@ -0,0 +1,26 @@
1
+ import React from "react";
2
+ export interface TranslatedTextProps {
3
+ /** Plain-text source string (post title, snippet, caption, etc.). */
4
+ text: string | null | undefined;
5
+ /**
6
+ * Optional fallback rendered when both translated and source text are
7
+ * empty — typically the calling component already conditionally renders
8
+ * this wrapper, so it's rarely needed.
9
+ */
10
+ fallback?: React.ReactNode;
11
+ }
12
+ /**
13
+ * Drop-in inline wrapper for translating a plain string into the language
14
+ * supplied by the nearest `<HiveLanguageProvider>`. Useful inside loops /
15
+ * `.map()` render functions where calling `useTranslatedText` directly
16
+ * would break the rules of hooks.
17
+ *
18
+ * ```tsx
19
+ * <h2><TranslatedText text={item.title} /></h2>
20
+ * ```
21
+ *
22
+ * Returns the original synchronously while the translation is in flight,
23
+ * so titles never flash empty. No-op for English.
24
+ */
25
+ export declare const TranslatedText: React.FC<TranslatedTextProps>;
26
+ export default TranslatedText;
@@ -0,0 +1,67 @@
1
+ import React from "react";
2
+ import { type KitMessageKey } from "./messages";
3
+ export type TranslateHtmlFn = (html: string, target: string) => Promise<string>;
4
+ /** Look up a UI label in the supplied dict, with English fallback. */
5
+ export type KitTFn = (key: KitMessageKey, vars?: Record<string, string | number>) => string;
6
+ export interface HiveLanguageContextValue {
7
+ /**
8
+ * Two-letter language code for translating user-generated content
9
+ * (post bodies, comments, activity bodies). `"en"` is the no-op default —
10
+ * content passes through unchanged.
11
+ */
12
+ language: string;
13
+ /**
14
+ * Custom translator. Defaults to a free MyMemory-backed implementation
15
+ * that ships with the kit. Override to plug in DeepL, Google Translate,
16
+ * or any other service — receives the rendered HTML and target language,
17
+ * returns translated HTML.
18
+ */
19
+ translateHtml: TranslateHtmlFn;
20
+ /**
21
+ * UI label translator. Resolves a key against the merged messages dict
22
+ * (consumer overrides → kit built-ins → English source-of-truth).
23
+ */
24
+ t: KitTFn;
25
+ }
26
+ export interface HiveLanguageProviderProps {
27
+ /** Two-letter language code, e.g. `"en"`, `"es"`. Defaults to `"en"`. */
28
+ language?: string;
29
+ /**
30
+ * Optional override translator for user-generated HTML. If omitted, the
31
+ * kit's default MyMemory-backed implementation is used.
32
+ */
33
+ translateHtml?: TranslateHtmlFn;
34
+ /**
35
+ * Optional UI label overrides keyed by language code. Merges on top of
36
+ * the kit's built-in `en` + `es` dicts:
37
+ *
38
+ * ```tsx
39
+ * messages={{
40
+ * es: { "action.follow": "Seguir cuenta" }, // override one label
41
+ * fr: { "action.follow": "Suivre" }, // add a new language
42
+ * }}
43
+ * ```
44
+ *
45
+ * Missing keys for a non-English language fall back to English.
46
+ */
47
+ messages?: Record<string, Record<string, string>>;
48
+ children: React.ReactNode;
49
+ }
50
+ /**
51
+ * Wrap your app once with this provider so every Hive content body
52
+ * rendered by the kit (`<HiveDetailPost>`, comments, snaps, profile feeds,
53
+ * activity bodies) translates to the chosen language.
54
+ *
55
+ * ```tsx
56
+ * <HiveLanguageProvider language={i18n.language}>
57
+ * <App />
58
+ * </HiveLanguageProvider>
59
+ * ```
60
+ *
61
+ * When `language === "en"` (the default), the provider is a no-op.
62
+ */
63
+ export declare const HiveLanguageProvider: React.FC<HiveLanguageProviderProps>;
64
+ /** Read the current Hive language + translator + label resolver from context. */
65
+ export declare function useHiveLanguage(): HiveLanguageContextValue;
66
+ /** Convenience hook returning just the UI label resolver. */
67
+ export declare function useKitT(): KitTFn;
@@ -0,0 +1,5 @@
1
+ export { HiveLanguageProvider, useHiveLanguage, useKitT, type HiveLanguageProviderProps, type HiveLanguageContextValue, type TranslateHtmlFn, type KitTFn, } from "./HiveLanguageContext";
2
+ export { useTranslatedHtml } from "./useTranslatedHtml";
3
+ export { useTranslatedText } from "./useTranslatedText";
4
+ export { translateHtml, translateText } from "./translateService";
5
+ export { BUILTIN_MESSAGES, formatMessage, type KitMessageKey, type KitMessages, } from "./messages";
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Built-in UI label translations for hive-react-kit components.
3
+ *
4
+ * Keys follow the dotted convention `area.label`. Each language is a
5
+ * flat Record<string, string>. Consumers can extend or override specific
6
+ * keys via the `messages` prop on `<HiveLanguageProvider>`:
7
+ *
8
+ * ```tsx
9
+ * <HiveLanguageProvider
10
+ * language="es"
11
+ * messages={{ es: { "action.follow": "Suivre" } }}
12
+ * />
13
+ * ```
14
+ *
15
+ * Missing keys for a non-English language fall back to the English entry,
16
+ * which is the source of truth for every label.
17
+ */
18
+ export type KitMessageKey = "tab.blogs" | "tab.posts" | "tab.snaps" | "tab.polls" | "tab.comments" | "tab.replies" | "tab.activities" | "tab.authorRewards" | "tab.curationRewards" | "tab.growth" | "tab.followers" | "tab.following" | "tab.wallet" | "tab.votingPower" | "tab.badges" | "tab.witnessVotes" | "meta.followers" | "meta.following" | "meta.posts" | "action.follow" | "action.unfollow" | "action.ignoreAuthor" | "action.reportUser" | "action.shareProfile" | "action.cancel" | "action.confirmIgnore" | "action.processing" | "modal.ignoreAuthorTitle" | "modal.ignoreAuthorBody" | "empty.noBlogs" | "empty.noPosts" | "empty.noSnaps" | "empty.noPolls" | "empty.noComments" | "empty.noReplies" | "empty.noFollowers" | "empty.notFollowing" | "empty.noBadges" | "empty.noWitnessVotes" | "empty.noPendingAuthor" | "empty.noPendingCuration" | "empty.curationHint" | "empty.votingPowerUnavailable" | "empty.userNotFound" | "status.active" | "status.ended" | "poll.selectAnOption" | "poll.selectUpTo" | "poll.changeYourVote" | "poll.selected" | "poll.submitVote" | "poll.submitVotes" | "poll.changeVote" | "poll.submitting" | "poll.voted" | "poll.voteChangesAllowed" | "poll.voter" | "poll.voters" | "poll.option" | "poll.options" | "poll.endsIn" | "common.translating" | "common.processing" | "common.fullyCharged" | "reward.pendingAuthor" | "reward.pendingCuration" | "reward.posts" | "reward.comments" | "reward.totalHbd" | "reward.totalHp" | "reward.avgEfficiency" | "reward.post" | "reward.comment" | "vp.upvotePower" | "vp.downvotePower" | "vp.resourceCredits";
19
+ export type KitMessages = Partial<Record<KitMessageKey, string>>;
20
+ export declare const BUILTIN_MESSAGES: Record<string, Record<string, string>>;
21
+ /**
22
+ * Format helper — replaces `{key}` placeholders with the corresponding
23
+ * value from `vars`. Missing vars leave the placeholder intact.
24
+ */
25
+ export declare function formatMessage(template: string, vars?: Record<string, string | number>): string;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * MyMemory-backed translation service for translating Hive post / comment
3
+ * bodies (rendered HTML) into the user's selected app language.
4
+ *
5
+ * Free, no API key, soft daily quota. The kit ships this as the default
6
+ * translator — consumers can override by passing their own `translate`
7
+ * function to `<HiveLanguageProvider>` (e.g. DeepL, Google Translate, an
8
+ * authenticated MyMemory account).
9
+ *
10
+ * Heavy caching: in-memory map (session) + localStorage (cross-reload) +
11
+ * in-flight dedup so two views of the same body share one network call.
12
+ */
13
+ /** Translate a single text string to `target` language. */
14
+ export declare function translateText(text: string, target: string, source?: string): Promise<string>;
15
+ /**
16
+ * Translate every visible text node in an HTML string to `target`. Skips
17
+ * text inside `<code>`, `<pre>`, `<script>`, `<style>`, `<noscript>`, and
18
+ * `<iframe>` so embeds and code samples render unchanged.
19
+ */
20
+ export declare function translateHtml(html: string, target: string): Promise<string>;
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Translate a rendered Hive post / comment body (HTML) into the language
3
+ * supplied by the nearest `<HiveLanguageProvider>`. Returns the original
4
+ * HTML for English (the source language) and synchronously falls back to
5
+ * the original on translation errors so the UI never looks broken.
6
+ *
7
+ * The translation is async — `loading` is true while the API request is
8
+ * in flight on the very first render. Subsequent renders read from cache
9
+ * and resolve instantly.
10
+ */
11
+ export declare function useTranslatedHtml(html: string): {
12
+ html: string;
13
+ loading: boolean;
14
+ };
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Plain-text counterpart to `useTranslatedHtml`. Use for titles, captions,
3
+ * preview snippets, and any other short user-generated string that should
4
+ * follow the language set on `<HiveLanguageProvider>`.
5
+ *
6
+ * Returns the original synchronously while the translation API call is
7
+ * in flight, so titles never flash blank. Falls back to the original on
8
+ * error and bypasses the translator entirely for English.
9
+ */
10
+ export declare function useTranslatedText(text: string | undefined | null): {
11
+ text: string;
12
+ loading: boolean;
13
+ };