npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gira-de/t9n-svelte

v2.1.0

Published

> A Svelte translation (t9n) solution for @gira-de/t9n.

Downloads

145

Readme

t9n Lib

A Svelte translation (t9n) solution for @gira-de/t9n.

See a working example here: t9n@Stackblitz.

Installation

npm install @gira-de/t9n-svelte

Initialization

To get started, the following files are required:

./locale/meta.json: This json describes the structure of the actual language files. Every possible translation key needs to be described here.

{
  "pageOne": {
    "headline": "This is a fancy headline written by a developer. Don't trust this! 🦹‍♀️🦹‍♂️"
  }
}

./locale/de.json: The actual language file

{
  "pageOne": {
    "headline": "Das ist die Überschrift von einem echten Übersetzer. Echt. 👩‍🏫👨‍🏫"
  }
}

Now de.json can be referenced during initialization of the t9n library:

_./src/App.svelte

<script lang="ts">
  import de from '../locale/de.json';
  import meta from '../locale/meta.json';
  import type { TranslationArgs } from '../locale/types';
  import t9n from '@gira-de/t9n-svelte';

  // dictionary with all languages
  const languages = [
    {
      locale: 'meta',
      name: 'Developer',
      dictionary: meta,
    },
    {
      locale: 'de',
      name: 'German',
      dictionary: de,
    },
    {
      locale: 'en',
      name: 'English',
      dictionary: {},
    },
  ] as const;

  // default language
  const translationFallback= languages[0];

  // logging functions
  const logFallback = (translationKey: string, currentLanguage: string) =>
    console.warn(
      `[t9n] The translationKey «${translationKey}» is missing within «${currentLanguage}». Using the translation fallback: «${translationFallback.locale}».`,
    );

  const logMissing = (translationKey: string, currentLanguage: string) =>
    console.warn(
      `[t9n] The translationKey «${translationKey}» is missing within «${currentLanguage}». Neither does the fallback language.`,
    );

  // locale, t and ti are Svelte Stores. Use the locale Store to change the language and t/ti to get the translation.
  const { locale, t, ti } = t9n<TranslationArgs>()({
    languages,
    translationFallback,
    logFallback,
    logMissing,
  });

  let selected: 'meta' | 'de' | 'en';
</script>

Usage

Get translations

To finally get translation by keys, you can use t or ti Svelte Stores:

const { locale, t, ti } = t9n<TranslationArgs>()({
  languages,
  translationFallback,
  logFallback,
  logMissing,
});

// Returns the translated string
$t(['pageOne.headline']);

// Returns an object with the hit information and the actual text
$ti(['pageOne.headline']);

If you like you can turn these command into a Svelte component and print styles based on the hit information. See Create a T component for more.

Translate numbers and dates

getLocaleDateString(new Date()),
getLocaleTimeString(new Date()),
getLocaleStringFromNumber(1.2),

Those functions are derived from Date.prototype.toLocal... functions: MDN Web Docs.

Set the language

To set the language use the locale method. If you want to detect the language automatically use the following code:

const { locale } = t9n<TranslationArgs>()({...});

// Tries to set the language according to the current browser settings.
// If the current browser language is not supported by the list of translations available,
// use one of the defined languages as fallback (in this case 'en').:
locale.trySet(navigator.language, 'en');

Create a T component

If you like you can turn the t and ti Svelte Stores into a Svelte component and print styles based on the hit information.

<script lang="ts">
  import { t, ti } from './locale';
  import type { TranslationArgs } from './locale-types';

  export let args: TranslationArgs;
</script>

{#if process.env.MODE === 'development'}
  {@const transInfo = $ti(...args)}
  <span
    class:isFallback={translationInfo.hit === 'fallbackDictionary'}
    class:isNone={translationInfo.hit === 'none'}>{translationInfo.text}</span
  >
{:else}
  <span>{$t(...args)}</span>
{/if}

<style>
  .isNone {
    text-decoration-line: underline;
    text-decoration-style: wavy;
    text-decoration-color: rgb(252, 69, 37);
  }

  .isFallback {
    text-decoration-line: underline;
    text-decoration-style: wavy;
    text-decoration-color: rgb(110, 167, 214);
  }
</style>

RFC 5646

As described in RFC 5646: Tags for Identifying Languages (also known as BCP 47) language tags with subtags are allow. For example:

const languages = [
  //...
  {
    locale: 'en',
    name: 'English',
    dictionary: en,
  },
  {
    locale: 'en-US',
    name: 'English (US)',
    dictionary: enUS,
  },
] as const;