Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add types for i18n module of cds 8.5. #371

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apis/facade.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './events'
export * from './utils'
export * from './cqn'
export * from './global'
export * from './i18n'
export { log, debug } from './log'
export { test } from './test'

Expand Down
47 changes: 47 additions & 0 deletions apis/i18n.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { csn, entity } from './cds'

type Texts = Record<string, string>

declare class I18nBundle {
constructor(options: I18nFilesOptions)
for(key: number | string | object, locale?: string | object, args?: object): string | undefined
at(key: number | string | object, locale?: string | object, args?: object): string | undefined
files: I18nFiles
get defaults(): Record<string, string>
get fallback(): Record<string, string>
daogrady marked this conversation as resolved.
Show resolved Hide resolved
key4(definition: entity): string
texts4 (locale: string): Texts
translations4 (...locales : string[]) : { [locale: string]: Texts }
translations4 (locale : 'all' ) : { [locale: string]: Texts }
all () : Record<string, Texts>
}

declare interface I18nFacade {
Bundle: typeof I18nBundle
Facade: typeof I18nFacade
Files: typeof I18nFiles
get file(): string
get folders(): string[]
get labels(): I18nBundle
get messages(): I18nBundle
daogrady marked this conversation as resolved.
Show resolved Hide resolved

bundle4 (file: string, options?: I18nFilesOptions): I18nBundle
bundle4 (model: csn.CSN): I18nBundle
}

interface I18nFilesOptions {
file? : string
model? : csn.CSN
roots? : string[]
leafs? : string[]
folders? : string[]
}

declare class I18nFiles {
constructor (options: I18nFilesOptions)
get options(): I18nFilesOptions
get basename(): string
content4(locale: string, suffix: string): Array<object>
}

export declare const i18n: I18nFacade
35 changes: 35 additions & 0 deletions test/typescript/apis/project/cds-i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import cds, { i18n } from "@sap/cds";
import { testType } from "./dummy";
const model = cds.reflect({});
const { Book: Books } = model.entities;

testType<string | undefined>(i18n.labels.for("foo"));
testType<string | undefined>(i18n.labels.at("foo"));
i18n.labels.at("foo", "bar");
i18n.labels.at("foo", "bar", [123, "baz"]);
i18n.labels.at("foo", [123, "baz"]);
i18n.labels.file = "";
// @ts-expect-error
i18n.labels.defaults = {
foo: "bar",
};
// @ts-expect-error
i18n.labels.fallback = {
foo: "bar",
};

testType<string | undefined>(i18n.messages.for("foo"));
testType<string | undefined>(i18n.messages.at("foo"));
i18n.messages.at("foo", "bar");
i18n.messages.at("foo", "bar", [123, "baz"]);
i18n.messages.at("foo", [123, "baz"]);

i18n.bundle4("foo", { file: "bar" });
i18n.labels.texts4("de");
i18n.labels.translations4("de");
i18n.labels.key4(Books);
i18n.messages.texts4("de");
i18n.messages.translations4("de");
i18n.messages.key4(Books);
cds.i18n.labels.for("foo");
cds.i18n === i18n;