Skip to content

Commit

Permalink
Fixed #204, locale fallback for unfinished locales.
Browse files Browse the repository at this point in the history
  • Loading branch information
amyjko committed Oct 17, 2023
1 parent 0c17b71 commit 0cbd908
Show file tree
Hide file tree
Showing 396 changed files with 3,267 additions and 2,455 deletions.
6 changes: 3 additions & 3 deletions src/basis/Basis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import UnparsableExpression from '@nodes/UnparsableExpression';
import Project from '../models/Project';
import Example from '../nodes/Example';
import { Basis } from './Basis';
import DefaultLocale from '../locale/DefaultLocale';
import DefaultLocale, { DefaultLocales } from '../locale/DefaultLocale';

const basis = Basis.getLocalizedBasis(DefaultLocale);
const basis = Basis.getLocalizedBasis(DefaultLocales);

const source = new Source('basis', '');
const project = Project.make(null, 'test', source, [], DefaultLocale);
Expand Down Expand Up @@ -66,7 +66,7 @@ function checkBasisNodes(nodes: Node[]) {
)}\nPrimary node: ${conflictingNodes.primary.node.toWordplay()}\n\t${
conflict.constructor.name
}\n${conflictingNodes.primary.explanation(
basis.locales[0],
basis.locales,
context
)}`
);
Expand Down
14 changes: 7 additions & 7 deletions src/basis/Basis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ import { getNameLocales } from '../locale/getNameLocales';
import bootstrapStructure from './StructureBasis';
import { toTokens } from '../parser/toTokens';
import parseType from '../parser/paresType';
import type Locales from '../locale/Locales';

export class Basis {
readonly locales: Locale[];
readonly locales: Locales;
readonly languages: LanguageCode[];
readonly shares: ReturnType<typeof createDefaultShares>;

Expand All @@ -45,9 +46,9 @@ export class Basis {
*/
static readonly Bases: Map<string, Basis> = new Map();

constructor(locales: Locale[]) {
constructor(locales: Locales) {
this.locales = locales;
this.languages = locales.map((locale) => locale.language);
this.languages = locales.getLanguages();

this.addStructure('none', bootstrapNone(locales));
this.addStructure('boolean', bootstrapBool(locales));
Expand All @@ -62,9 +63,8 @@ export class Basis {
this.shares = createDefaultShares(locales);
}

static getLocalizedBasis(locales: Locale | Locale[]) {
locales = Array.isArray(locales) ? locales : [locales];
const languages = locales.map((locale) => locale.language);
static getLocalizedBasis(locales: Locales) {
const languages = locales.getLanguages();
const key = languages.join(',');
const basis = Basis.Bases.get(key) ?? new Basis(locales);
Basis.Bases.set(key, basis);
Expand Down Expand Up @@ -160,7 +160,7 @@ export class Basis {
}

export function createBasisFunction(
locales: Locale[],
locales: Locales,
text: (locale: Locale) => FunctionText<NameAndDoc[]>,
typeVars: TypeVariables | undefined,
types: (Type | [Type, Expression])[],
Expand Down
3 changes: 2 additions & 1 deletion src/basis/BoolBasis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import type Locale from '../locale/Locale';
import type { FunctionText, NameAndDoc } from '../locale/Locale';
import type Type from '../nodes/Type';
import AnyType from '../nodes/AnyType';
import type Locales from '../locale/Locales';

export default function bootstrapBool(locales: Locale[]) {
export default function bootstrapBool(locales: Locales) {
function createBooleanFunction(
text: (locale: Locale) => FunctionText<NameAndDoc[]>,
inputs: Type[],
Expand Down
13 changes: 8 additions & 5 deletions src/basis/InternalExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import type Context from '@nodes/Context';
import type TypeSet from '@nodes/TypeSet';
import StartFinish from '@runtime/StartFinish';
import SimpleExpression from '@nodes/SimpleExpression';
import type Locale from '@locale/Locale';
import InternalException from '@values/InternalException';
import Glyphs from '../lore/Glyphs';
import concretize from '../locale/concretize';
Expand All @@ -18,6 +17,7 @@ import Start from '@runtime/Start';
import Finish from '@runtime/Finish';
import { toTokens } from '../parser/toTokens';
import parseType from '../parser/paresType';
import type Locales from '../locale/Locales';

export default class InternalExpression extends SimpleExpression {
readonly type: Type;
Expand Down Expand Up @@ -106,12 +106,15 @@ export default class InternalExpression extends SimpleExpression {
return this;
}

getNodeLocale(translation: Locale) {
return translation.node.InternalExpression;
getNodeLocale(locales: Locales) {
return locales.get((l) => l.node.InternalExpression);
}

getStartExplanations(locale: Locale) {
return concretize(locale, locale.node.InternalExpression.start);
getStartExplanations(locales: Locales) {
return concretize(
locales,
locales.get((l) => l.node.InternalExpression.start)
);
}

getGlyphs() {
Expand Down
21 changes: 12 additions & 9 deletions src/basis/Iteration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Purpose from '../concepts/Purpose';
import type Locale from '../locale/Locale';
import concretize from '../locale/concretize';
import Glyphs from '../lore/Glyphs';
import AnyType from '../nodes/AnyType';
Expand All @@ -23,6 +22,7 @@ import Next from '@runtime/Next';
import Start from '@runtime/Start';
import type Step from '@runtime/Step';
import Value from '../values/Value';
import type Locales from '../locale/Locales';

const IterationState = 'state';

Expand Down Expand Up @@ -218,23 +218,26 @@ export class Iteration<State = any> extends Expression {
return this;
}

getNodeLocale(translation: Locale) {
return translation.node.Iteration;
getNodeLocale(locales: Locales) {
return locales.get((l) => l.node.Iteration);
}

getStartExplanations(locale: Locale) {
return concretize(locale, locale.node.Iteration.start);
getStartExplanations(locales: Locales) {
return concretize(
locales,
locales.get((l) => l.node.Iteration.start)
);
}

getFinishExplanations(
locale: Locale,
locales: Locales,
context: Context,
evaluator: Evaluator
) {
return concretize(
locale,
locale.node.Iteration.finish,
this.getValueIfDefined(locale, context, evaluator)
locales,
locales.get((l) => l.node.Iteration.finish),
this.getValueIfDefined(locales, context, evaluator)
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/basis/ListBasis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { getDocLocales } from '@locale/getDocLocales';
import { getNameLocales } from '@locale/getNameLocales';
import TypeVariable from '@nodes/TypeVariable';
import type Expression from '../nodes/Expression';
import type Locale from '../locale/Locale';
import NoneLiteral from '../nodes/NoneLiteral';
import NoneValue from '@values/NoneValue';
import { Iteration } from './Iteration';
Expand All @@ -34,8 +33,9 @@ import InternalExpression from './InternalExpression';
import ConversionException from '@values/ConversionException';
import ExceptionValue from '@values/ExceptionValue';
import SetType from '../nodes/SetType';
import type Locales from '../locale/Locales';

export default function bootstrapList(locales: Locale[]) {
export default function bootstrapList(locales: Locales) {
const ListTypeVarNames = getNameLocales(
locales,
(locale) => locale.basis.List.kind
Expand Down
4 changes: 2 additions & 2 deletions src/basis/MapBasis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import TypeVariable from '@nodes/TypeVariable';
import type Evaluation from '@runtime/Evaluation';
import type Value from '@values/Value';
import type Expression from '../nodes/Expression';
import type Locale from '../locale/Locale';
import { createFunction, createInputs } from '../locale/Locale';
import { Iteration } from './Iteration';
import NumberType from '../nodes/NumberType';
Expand All @@ -25,8 +24,9 @@ import TextType from '../nodes/TextType';
import SetType from '../nodes/SetType';
import ListType from '../nodes/ListType';
import AnyType from '../nodes/AnyType';
import type Locales from '../locale/Locales';

export default function bootstrapMap(locales: Locale[]) {
export default function bootstrapMap(locales: Locales) {
const KeyTypeVariableNames = getNameLocales(
locales,
(locale) => locale.basis.Map.key
Expand Down
5 changes: 3 additions & 2 deletions src/basis/NoneBasis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import type Expression from '../nodes/Expression';
import type Locale from '../locale/Locale';
import type { FunctionText, NameAndDoc } from '../locale/Locale';
import TextType from '../nodes/TextType';
import type Locales from '../locale/Locales';

export default function bootstrapNone(locales: Locale[]) {
export default function bootstrapNone(locales: Locales) {
function createNoneFunction(
locales: Locale[],
locales: Locales,
text: (locale: Locale) => FunctionText<NameAndDoc[]>,
expression: (
requestor: Expression,
Expand Down
3 changes: 2 additions & 1 deletion src/basis/NumberBasis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import type Locale from '../locale/Locale';
import type { FunctionText, NameAndDoc } from '../locale/Locale';
import ListType from '../nodes/ListType';
import AnyType from '../nodes/AnyType';
import type Locales from '../locale/Locales';

export default function bootstrapNumber(locales: Locale[]) {
export default function bootstrapNumber(locales: Locales) {
const subtractNames = getNameLocales(
locales,
(locale) => locale.basis.Number.function.subtract.inputs[0].names
Expand Down
4 changes: 2 additions & 2 deletions src/basis/SetBasis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ import { getDocLocales } from '@locale/getDocLocales';
import { getNameLocales } from '@locale/getNameLocales';
import TypeVariable from '@nodes/TypeVariable';
import type Expression from '../nodes/Expression';
import type Locale from '../locale/Locale';
import { createBind, createFunction, createInputs } from '../locale/Locale';
import { Iteration } from './Iteration';
import NumberType from '../nodes/NumberType';
import NumberValue from '@values/NumberValue';
import ListType from '../nodes/ListType';
import TextType from '../nodes/TextType';
import AnyType from '../nodes/AnyType';
import type Locales from '../locale/Locales';

export default function bootstrapSet(locales: Locale[]) {
export default function bootstrapSet(locales: Locales) {
const SetTypeVariableNames = getNameLocales(
locales,
(locale) => locale.basis.Set.kind
Expand Down
4 changes: 2 additions & 2 deletions src/basis/StructureBasis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import StructureDefinition from '@nodes/StructureDefinition';
import Block, { BlockKind } from '@nodes/Block';
import { getDocLocales } from '@locale/getDocLocales';
import { getNameLocales } from '@locale/getNameLocales';
import type Locale from '../locale/Locale';
import { createBasisConversion, createBasisFunction } from './Basis';
import BoolValue from '@values/BoolValue';
import BooleanType from '../nodes/BooleanType';
Expand All @@ -13,8 +12,9 @@ import Value from '../values/Value';
import type StructureValue from '../values/StructureValue';
import TextType from '../nodes/TextType';
import TextValue from '../values/TextValue';
import type Locales from '../locale/Locales';

export default function bootstrapStructure(locales: Locale[]) {
export default function bootstrapStructure(locales: Locales) {
return StructureDefinition.make(
getDocLocales(locales, (locale) => locale.basis.Structure.doc),
getNameLocales(locales, (locale) => locale.basis.Structure.name),
Expand Down
6 changes: 3 additions & 3 deletions src/basis/TableBasis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type Names from '@nodes/Names';
import { getInputLocales as getInputLocales } from '@locale/getInputLocales';
import { getDocLocales } from '@locale/getDocLocales';
import { getNameLocales } from '@locale/getNameLocales';
import type Locale from '../locale/Locale';
import type Type from '../nodes/Type';
import TableType from '../nodes/TableType';
import TableValue from '../values/TableValue';
Expand All @@ -25,8 +24,9 @@ import TextValue from '../values/TextValue';
import TypeVariables from '../nodes/TypeVariables';
import TypeVariable from '../nodes/TypeVariable';
import AnyType from '../nodes/AnyType';
import type Locales from '../locale/Locales';

export default function bootstrapTable(locales: Locale[]) {
export default function bootstrapTable(locales: Locales) {
/** This type variable represents the StructureDefinition of a row. */
const RowTypeVariable = new TypeVariable(
getNameLocales(locales, (locale) => locale.basis.Table.row)
Expand Down Expand Up @@ -135,7 +135,7 @@ export default function bootstrapTable(locales: Locale[]) {
TableType.make(),
TextType.make(),
(requestor: Expression, table: TableValue) =>
new TextValue(requestor, table.toWordplay([]))
new TextValue(requestor, table.toWordplay(locales))
),
],
BlockKind.Structure
Expand Down
3 changes: 2 additions & 1 deletion src/basis/TextBasis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import type Locale from '../locale/Locale';
import type { FunctionText, NameAndDoc } from '../locale/Locale';
import ListType from '../nodes/ListType';
import AnyType from '../nodes/AnyType';
import type Locales from '../locale/Locales';

export default function bootstrapText(locales: Locale[]) {
export default function bootstrapText(locales: Locales) {
function createBinaryTextFunction<OutputType extends Value>(
functionText: (locale: Locale) => FunctionText<NameAndDoc[]>,
fun: (
Expand Down
Loading

0 comments on commit 0cbd908

Please sign in to comment.