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

Convert i18n package to TypeScript #12198

Merged
merged 5 commits into from
Sep 1, 2022
Merged
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
20 changes: 18 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,29 @@
]
},
"rules": {
"no-duplicate-imports": "off",
"@typescript-eslint/no-duplicate-imports": "error",
"@typescript-eslint/no-shadow": "error",
"jsdoc/check-param-names": "off",
miina marked this conversation as resolved.
Show resolved Hide resolved
"jsdoc/require-param": "off",
"jsdoc/require-param-type": "off",
"jsdoc/require-returns": "off",
"jsdoc/require-returns-check": "off",
"jsdoc/require-returns-type": "off",
"jsdoc/no-types": "error",
"no-duplicate-imports": "off",
"no-unused-vars": "off",
"no-shadow": "off",
"@typescript-eslint/no-shadow": "error"
"no-restricted-imports": [
"error",
{
"paths": [
{
"name": "prop-types",
"message": "Use TypeScript instead."
}
]
}
]
}
},
{
Expand Down
5 changes: 3 additions & 2 deletions packages/i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
},
"customExports": {
".": {
"default": "./src/index.js"
"default": "./src/index.ts"
}
},
"main": "dist/index.js",
"module": "dist-module/index.js",
"source": "src/index.js",
"types": "dist-types/index.d.ts",
"source": "src/index.ts",
"publishConfig": {
"access": "public"
},
Expand Down
100 changes: 0 additions & 100 deletions packages/i18n/src/i18n.js

This file was deleted.

118 changes: 118 additions & 0 deletions packages/i18n/src/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* External dependencies
*/
import Tannin from 'tannin';
import type { TanninLocaleDomain } from 'tannin';

interface LocaleData {
locale_data: {
[key: string]: TanninLocaleDomain;
};
}

const TEXT_DOMAIN = 'web-stories';

const tannin = new Tannin({
[TEXT_DOMAIN]: {
'': {
plural_forms(n: number) {
return n === 1 ? 0 : 1;
},
},
},
});

/**
* Merge locale data into the Tannin instance.
*
* @param data Locale data.
*/
export function setLocaleData(data: LocaleData) {
const translations =
data.locale_data['web-stories'] || data.locale_data.messages;

tannin.data[TEXT_DOMAIN] = {
...tannin.data[TEXT_DOMAIN],
...translations,
'': {
...tannin.data[TEXT_DOMAIN][''],
},
};
}

/**
* Retrieve the translation of the given text.
*
* @param text Text to translate.
* @param [domain] Text domain. Unique identifier for retrieving translated strings.
* @return Translated text.
*/
export function __(text: string, domain = TEXT_DOMAIN) {
return tannin.dcnpgettext(domain, undefined, text);
}

/**
* Retrieve the translation of the given text with gettext context..
*
* @param text Text to translate.
* @param context Context information for the translators.
* @param [domain] Text domain. Unique identifier for retrieving translated strings.
* @return Translated text.
*/
export function _x(text: string, context: string, domain = TEXT_DOMAIN) {
return tannin.dcnpgettext(domain, context, text);
}

/**
* Retrieve the translation of the given text with gettext context..
*
* @param singular The text to be used if the number is singular.
* @param plural The text to be used if the number is plural.
* @param number The number to compare against to use either the singular or plural form.
* @param [domain] Text domain. Unique identifier for retrieving translated strings.
* @return Translated text.
*/
export function _n(
singular: string,
plural: string,
number: number,
domain = TEXT_DOMAIN
) {
return tannin.dcnpgettext(domain, undefined, singular, plural, number);
}

/**
* Retrieve the translation of the given text with gettext context..
*
* @param singular The text to be used if the number is singular.
* @param plural The text to be used if the number is plural.
* @param number The number to compare against to use either the singular or plural form.
* @param context Context information for the translators.
* @param [domain] Text domain. Unique identifier for retrieving translated strings.
* @return Translated text.
*/
export function _nx(
singular: string,
plural: string,
number: number,
context: string,
domain = TEXT_DOMAIN
) {
return tannin.dcnpgettext(domain, context, singular, plural, number);
}
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import { __ } from './i18n';
* Join all options in a list and translate. These will be joined with
* the conjunction `or`.
*
* @param {Array.<string>} options The options that will be joined with the `or` conjunction.
* @return {string} Localized list items.
* @param options The options that will be joined with the `or` conjunction.
* @return Localized list items.
*/
function translateToExclusiveList(options) {
function translateToExclusiveList(options: string[]) {
switch (options.length) {
case 0:
return '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import { __ } from './i18n';
* Join all options in a list and translate. These will be joined with
* the conjunction `and`.
*
* @param {Array.<string>} options The options that will be joined with the `and` conjunction.
* @return {string} Localized list items.
* @param options The options that will be joined with the `and` conjunction.
* @return Localized list items.
*/
function translateToInclusiveList(options) {
function translateToInclusiveList(options: string[]) {
switch (options.length) {
case 0:
return '';
Expand Down
Loading