Git Product home page Git Product logo

i18n's Introduction

grammY i18n

Internationalization plugin for grammY based on Project Fluent. Check out the official documentation for this plugin.

Installation

Node.js

npm install @grammyjs/i18n

Deno

import { I18n, I18nFlavor } from "https://deno.land/x/grammy_i18n/mod.ts";

Example

Example project structure:

.
├─ locales/
│  ├── en.ftl
│  ├── it.ftl
│  └── ru.ftl
└── bot.ts

Example bot not using sessions:

import { Bot, Context } from "https://deno.land/x/grammy/mod.ts";
import { I18n, I18nFlavor } from "https://deno.land/x/grammy_i18n/mod.ts";

// For proper typings and auto-completions in IDEs,
// customize the `Context` using `I18nFlavor`.
type MyContext = Context & I18nFlavor;

// Create a new I18n instance.
const i18n = new I18n<MyContext>({
  defaultLocale: "en",
  directory: "locales",
});

// Create a bot as usual, but use the modified Context type.
const bot = new Bot<MyContext>(""); // <- Put your bot token here

// Remember to register this middleware before registering
// your handlers.
bot.use(i18n);

bot.command("start", async (ctx) => {
  // Use the method `t` or `translate` from the context and pass
  // in the message id (key) of the message you want to get.
  await ctx.reply(ctx.t("greeting"));
});

// Start your bot
bot.start();

See the documentation and examples/ for more detailed examples.

Credits

Thanks to...

i18n's People

Contributors

bymsx avatar dcdunkan avatar dependabot[bot] avatar deptyped avatar dmbaranov avatar dotcypress avatar edjopato avatar fazendaaa avatar fcfn avatar heeroml avatar igorivaniuk avatar jfkz avatar knorpelsenf avatar krolov avatar loskir avatar roj1512 avatar slavafomin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

i18n's Issues

Error when I try call db

I get error: GrammyError: Call to 'toBSON' failed! (404: Not Found), when I try call db in code

Issue with RTL formatted language mixed with Latin words

There is a problem that I'm not sure it is related to Grammy or not.
When using RTL language text (like Persian) that has LTR words within the Latin word get reversed. example

# fa.ftl
start = سلام { $name }! چطور می‌تونم کمکت کنم؟ 

The above sentence in English is "Hello { $name}! how can i help you?"

if name is Parsa (which is loaded from ctx.from.first_name) the result must be like below:
سلام Parsa! چطور می‌تونم کمکت کنم؟
but the the result will be like this:
سلام asraP! چطور می‌تونم کمکت کنم؟

I tried the Fluent playground to check if it's related to Fluent but it's not! check the link below:
https://projectfluent.org/play/?id=2b91a538f800639b728f09d8853889e6

Use of licensed code without permission

I have been made aware that this repository duplicated code from https://github.com/jonschlinkert/tableize-object without any mention of the original work. This is in breach of the MIT license.

Please make this repository abide the law by respecting the licenses of all used projects.

I am going to audit the code afterwards to see if further projects have been used without permission.

If no action is taken, this repository must be deleted along with any published npm packages.

cashtag entities ignored

My ftl string:

balance =
    💰 Your balance is <b>{$balance} ${$symbol}</b>.
    ⬇️ To deposit more funds, use /deposit.

Result:

Screenshot 2024-05-15 at 13 17 22
All entities except for cashstrings are there.

Expected result:

Screenshot 2024-05-15 at 13 21 28
When passing as plain text instead of i18n localized string all works fine.

Migrate to fluent

Fluent seems to be a much superior translation system than the current implementation via vm. However, our fluent plugin won't support Deno, despite the demand.

I suggest to:

  1. Take the code in https://github.com/dcdunkan/grammy-fluent
  2. Refactor it in a way that resembles the code style and spirit of this repo
  3. Maybe bring back any missing features in case they exist
  4. Release this as i18n 2.0

As a result, we would get Deno support, a better translation system, and a reactivation of this project.

@dcdunkan has offered to assist in this, but prefers not to become the maintainer of i18n.

This is a suggestion by me, who does not actually use the plugin, so take it with a grain of salt.

@EdJoPaTo @HeeroML @dcdunkan (and @roj1512 because I know you like the fork) how much time are each one of you willing to spend on this, irrespective of whether or not you'd be the main person in charge? If it's just about who is responsible, I don't mind taking that role and then in turn pinging you guys if something needs to be fixed that exceeds my knowlege of this plugin :)

Not compatible with cloudflare workers

Filesystem is not accessible at cloudflare workers, deploy command generates lot of errors like that:

✘ [ERROR] Could not resolve "path"

    node_modules/which/which.js:5:21:
      5 │ const path = require('path')
        ╵                      ~~~~~~

  The package "path" wasn't found on the file system but is built into node.
  Add "node_compat = true" to your wrangler.toml file to enable Node.js compatibility.

Adding node_compat = true doesn't help.

Any approach to fix that?

YAML support?

I have all my locales stored in YAML files but looks like .yaml is not supported anymore. Is there a way to use .yaml files instead of .ftl

HTML link with parameters is not rendering correctly

I am encountering an issue with rendering an HTML link using 2 parameters. Only the name of the link is rendering correctly, while the link itself is not working as expected. Here is the code that I am using:

<a href="{$link}">{$name}</a>

When I attempt to render this link, the name of the link is displaying correctly, but the link itself is not working as expected. Instead of redirecting to the specified URL, the link is simply displaying as plain text.

HTML link renders correctly when a static URL is used. the following code renders the link correctly:

<a href="https://google.com/">{$name}</a>

Any assistance in resolving this issue would be greatly appreciated. Thank you.

Ordinal numbers plural selector are not working

The third example on the Fluent docs page "Selectors" does not work with grammY's i18n plugin.

MWE

# en.ftl
request-success = You are { NUMBER($queueLength, type:"ordinal") ->
  [1] next
  [one] the {$queueLength}st
  [two] the {$queueLength}nd
  [few] the {$queueLength}rd
  *[other] the {$queueLength}th
} in queue!
// main.ts
import { load } from "dotenv";
import { Bot, Context } from "grammy";
import { I18n, I18nFlavor } from "@grammyjs/i18n";

const env = await load();

const i18n = new I18n({
  defaultLocale: "it",
  directory: "locales",
});

const bot = new Bot<Context & I18nFlavor>(env["BOT_TOKEN"]);
bot.use(i18n);
bot.command(
  "example",
  (ctx) => ctx.reply(ctx.t("request-success", { queueLength: 2 })),
);
bot.start();

Expected result

Bot replies with "You are 2nd in queue!"

Actual result

Bot replies with "Your are 2th in queue!"
Same result if queueLength = 3.

Line breaks with HTML parse mode without template variables

Telegram doesn't show line breaks when using parse_mode: "HTML" and no template variables passes to template function

en.yaml

test: First Line\nSecond Line
test2: First Line\nSecond Line${n}

Example Grammy code with and without template variables

await ctx.reply(ctx.i18n.t("test"), { parse_mode: "HTML" });
await ctx.reply(ctx.i18n.t("test2", { n: "" }), { parse_mode: "HTML" });

Result in Telegram
Screenshot 2021-09-21 at 13 49 52

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.