mirror of
https://github.com/wassname/talk.git
synced 2026-07-23 13:10:20 +08:00
Updated fluent
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { LocalizationProvider } from "fluent-react/compat";
|
||||
import { MessageContext } from "fluent/compat";
|
||||
import { FluentBundle } from "fluent/compat";
|
||||
import { Child as PymChild } from "pym.js";
|
||||
import React, { StatelessComponent } from "react";
|
||||
import { MediaQueryMatchers } from "react-responsive";
|
||||
@@ -15,8 +15,8 @@ export interface TalkContext {
|
||||
/** relayEnvironment for our relay framework. */
|
||||
relayEnvironment: Environment;
|
||||
|
||||
/** localMessages for our i18n framework. */
|
||||
localeMessages: MessageContext[];
|
||||
/** localeBundles for our i18n framework. */
|
||||
localeBundles: FluentBundle[];
|
||||
|
||||
/** formatter for timeago. */
|
||||
timeagoFormatter?: Formatter;
|
||||
@@ -61,7 +61,7 @@ export const TalkContextProvider: StatelessComponent<{
|
||||
value: TalkContext;
|
||||
}> = ({ value, children }) => (
|
||||
<Provider value={value}>
|
||||
<LocalizationProvider messages={value.localeMessages}>
|
||||
<LocalizationProvider bundles={value.localeBundles}>
|
||||
<UIContext.Provider
|
||||
value={{
|
||||
timeagoFormatter: value.timeagoFormatter,
|
||||
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
import { RestClient } from "talk-framework/lib/rest";
|
||||
import { ClickFarAwayRegister } from "talk-ui/components/ClickOutside";
|
||||
|
||||
import { generateMessages, LocalesData, negotiateLanguages } from "../i18n";
|
||||
import { generateBundles, LocalesData, negotiateLanguages } from "../i18n";
|
||||
import { createFetch, TokenGetter } from "../network";
|
||||
import { PostMessageService } from "../postMessage";
|
||||
import { TalkContext } from "./TalkContext";
|
||||
@@ -104,12 +104,12 @@ export default async function createContext({
|
||||
console.log(`Negotiated locales ${JSON.stringify(locales)}`);
|
||||
}
|
||||
|
||||
const localeMessages = await generateMessages(locales, localesData);
|
||||
const localeBundles = await generateBundles(locales, localesData);
|
||||
|
||||
// Assemble context.
|
||||
const context = {
|
||||
relayEnvironment,
|
||||
localeMessages,
|
||||
localeBundles,
|
||||
timeagoFormatter,
|
||||
pym,
|
||||
eventEmitter,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import "fluent-intl-polyfill/compat";
|
||||
import { negotiateLanguages as negotiate } from "fluent-langneg/compat";
|
||||
import { MessageContext } from "fluent/compat";
|
||||
import { FluentBundle } from "fluent/compat";
|
||||
|
||||
export interface BundledLocales {
|
||||
[locale: string]: string;
|
||||
@@ -47,18 +47,20 @@ export function negotiateLanguages(
|
||||
}
|
||||
|
||||
// Don't warn in production.
|
||||
let decorateWarnMissing = (cx: MessageContext) => cx;
|
||||
let decorateWarnMissing = (bundle: FluentBundle) => bundle;
|
||||
|
||||
// Warn about missing locales if we are not in production.
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
decorateWarnMissing = (() => {
|
||||
const warnings: string[] = [];
|
||||
return (cx: MessageContext) => {
|
||||
const original = cx.hasMessage;
|
||||
cx.hasMessage = (id: string) => {
|
||||
const result = original.apply(cx, [id]);
|
||||
return (bundle: FluentBundle) => {
|
||||
const original = bundle.hasMessage;
|
||||
bundle.hasMessage = (id: string) => {
|
||||
const result = original.apply(bundle, [id]);
|
||||
if (!result) {
|
||||
const warn = `${cx.locales} translation for key "${id}" not found`;
|
||||
const warn = `${
|
||||
bundle.locales
|
||||
} translation for key "${id}" not found`;
|
||||
if (!warnings.includes(warn)) {
|
||||
// tslint:disable:next-line: no-console
|
||||
console.warn(warn);
|
||||
@@ -67,7 +69,7 @@ if (process.env.NODE_ENV !== "production") {
|
||||
}
|
||||
return result;
|
||||
};
|
||||
return cx;
|
||||
return bundle;
|
||||
};
|
||||
})();
|
||||
}
|
||||
@@ -79,21 +81,21 @@ if (process.env.NODE_ENV !== "production") {
|
||||
*
|
||||
* Use it in conjunction with `negotiateLanguages`.
|
||||
*/
|
||||
export async function generateMessages(
|
||||
export async function generateBundles(
|
||||
locales: ReadonlyArray<string>,
|
||||
data: LocalesData
|
||||
): Promise<MessageContext[]> {
|
||||
): Promise<FluentBundle[]> {
|
||||
const promises = [];
|
||||
|
||||
for (const locale of locales) {
|
||||
const cx = new MessageContext(locale);
|
||||
const bundle = new FluentBundle(locale);
|
||||
if (locale in data.bundled) {
|
||||
cx.addMessages(data.bundled[locale]);
|
||||
promises.push(decorateWarnMissing(cx));
|
||||
bundle.addMessages(data.bundled[locale]);
|
||||
promises.push(decorateWarnMissing(bundle));
|
||||
} else if (locale in data.loadables) {
|
||||
const content = await data.loadables[locale]();
|
||||
cx.addMessages(content);
|
||||
promises.push(decorateWarnMissing(cx));
|
||||
bundle.addMessages(content);
|
||||
promises.push(decorateWarnMissing(bundle));
|
||||
} else {
|
||||
throw Error(`Locale ${locale} not available`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user