CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-intlify--unplugin-vue-i18n

Unplugin that provides comprehensive Vue I18n integration capabilities for various bundlers including Vite, Webpack, and Nuxt

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

sfc-integration.mddocs/

SFC Integration

Processing of Vue Single File Component <i18n> custom blocks with support for multiple formats, scoping, and import functionality.

Capabilities

i18n Custom Blocks

Process <i18n> custom blocks within Vue Single File Components.

/**
 * i18n custom block processing in Vue SFCs
 * Supports inline locale definitions and external imports
 */
interface I18nCustomBlock {
  /**
   * Inline locale messages in various formats
   */
  inline: boolean;
  
  /**
   * Block language format
   */
  lang?: SFCLangFormat;
  
  /**
   * Scope for the i18n block
   */
  global?: boolean;
  
  /**
   * Locale identifier for the block
   */
  locale?: string;
  
  /**
   * External resource import
   */
  src?: string;
}

type SFCLangFormat = 'json' | 'json5' | 'yml' | 'yaml';

Vue Request Query Parsing

Parse Vue SFC requests with i18n-specific query parameters.

/**
 * Parse Vue SFC request with query parameters
 * @param id - Request identifier with query string
 * @returns Parsed filename and query object
 */
function parseVueRequest(id: string): {
  filename: string;
  query: VueQuery;
};

interface VueQuery {
  /** Whether this is a Vue file request */
  vue?: boolean;
  
  /** Whether this is a source file request */
  src?: boolean;
  
  /** Whether this block has global scope */
  global?: boolean;
  
  /** Type of SFC block */
  type?: 'script' | 'template' | 'style' | 'custom' | 'i18n';
  
  /** Custom block type name */
  blockType?: string;
  
  /** Block index within the SFC */
  index?: number;
  
  /** Locale identifier for the block */
  locale?: string;
  
  /** Language format of the block */
  lang?: string;
  
  /** Whether to return raw content */
  raw?: boolean;
  
  /** Path of the file that imported this resource */
  issuerPath?: string;
}

SFC Descriptor Processing

Create and process Vue SFC descriptors for i18n block extraction.

/**
 * Create SFC descriptor from source code
 * @param filename - Source file name
 * @param source - Vue SFC source code
 * @param options - Vue plugin options
 * @returns Parsed SFC descriptor and any errors
 */
function createDescriptor(
  filename: string,
  source: string,
  options: VuePluginResolvedOptions
): SFCParseResult;

/**
 * Get SFC descriptor, throwing on parse errors
 * @param filename - Source file name
 * @param code - Vue SFC source code
 * @param options - Vue plugin options
 * @returns Parsed SFC descriptor
 * @throws {Error} If parsing fails
 */
function getDescriptor(
  filename: string,
  code: string,
  options: VuePluginResolvedOptions
): SFCDescriptor;

interface SFCParseResult {
  descriptor: SFCDescriptor;
  errors: (CompilerError | SyntaxError)[];
}

Vue Plugin Integration

Integration with Vue SFC compiler and plugin system.

/**
 * Extract Vue compiler from Vue plugin instance
 * @param vuePlugin - Vue plugin instance
 * @returns Vue SFC compiler
 */
function getVueCompiler(vuePlugin: RollupPlugin): typeof import('vue/compiler-sfc');

/**
 * Extract Vue plugin options
 * @param vuePlugin - Vue plugin instance
 * @returns Resolved Vue plugin options
 */
function getVuePluginOptions(vuePlugin: RollupPlugin): VuePluginResolvedOptions;

/**
 * Get SFC descriptor with error handling
 * @param filename - Source file name
 * @param code - Vue SFC source code  
 * @param options - Vue plugin options
 * @returns Parsed SFC descriptor
 * @throws {Error} If parsing fails
 */
function getDescriptor(
  filename: string,
  code: string,
  options: VuePluginResolvedOptions
): SFCDescriptor;

interface VuePluginResolvedOptions {
  isProduction: boolean;
  root: string;
  compiler: typeof import('vue/compiler-sfc');
  template?: Partial<SFCTemplateCompileOptions>;
}

SFC Configuration Options

Block Language Configuration

/**
 * Configuration for SFC i18n block language handling
 */
interface SFCLanguageOptions {
  /**
   * Default language format for i18n blocks without lang attribute
   * @default 'json'
   */
  defaultSFCLang?: SFCLangFormat;
}

Block Scoping Configuration

/**
 * Configuration for SFC i18n block scoping
 */
interface SFCScopingOptions {
  /**
   * Make all i18n blocks global scope by default
   * Overrides individual block scope attributes
   * @default false
   */
  globalSFCScope?: boolean;
}

Block Transformation

/**
 * Custom transformation for i18n block content
 */
interface SFCTransformOptions {
  /**
   * Transform function for i18n block source
   * Called before block processing
   * @param src - Original i18n block source
   * @returns Transformed source string
   */
  transformI18nBlock?: (src: string | Buffer) => string;
}

Usage Examples

Basic i18n Custom Block

<template>
  <div>
    <h1>{{ t('title') }}</h1>
    <p>{{ t('description') }}</p>
  </div>
</template>

<script setup>
import { useI18n } from 'vue-i18n';

const { t } = useI18n({
  useScope: 'local'
});
</script>

<i18n>
{
  "en": {
    "title": "Welcome",
    "description": "This is a local component message"
  },
  "fr": {
    "title": "Bienvenue", 
    "description": "Ceci est un message de composant local"
  }
}
</i18n>

YAML Format i18n Block

<template>
  <div>{{ t('message') }}</div>
</template>

<script setup>
import { useI18n } from 'vue-i18n';
const { t } = useI18n({ useScope: 'local' });
</script>

<i18n lang="yaml">
en:
  message: Hello from YAML
  nested:
    key: Nested value
fr:
  message: Bonjour depuis YAML
  nested:
    key: Valeur imbriquée
</i18n>

Global Scope i18n Block

<template>
  <div>{{ $t('global.message') }}</div>
</template>

<i18n global>
{
  "en": {
    "global": {
      "message": "This message is available globally"
    }
  },
  "fr": {
    "global": {
      "message": "Ce message est disponible globalement"
    }
  }
}
</i18n>

External Resource Import

<template>
  <div>{{ t('imported.message') }}</div>
</template>

<script setup>
import { useI18n } from 'vue-i18n';
const { t } = useI18n({ useScope: 'local' });
</script>

<!-- Import external i18n resource -->
<i18n src="./component-messages.json"></i18n>

Multiple i18n Blocks

<template>
  <div>
    <h1>{{ t('title') }}</h1>
    <nav>
      <a href="/">{{ $t('nav.home') }}</a>
      <a href="/about">{{ $t('nav.about') }}</a>
    </nav>
  </div>
</template>

<script setup>
import { useI18n } from 'vue-i18n';
const { t } = useI18n({ useScope: 'local' });
</script>

<!-- Local messages for this component -->
<i18n>
{
  "en": { "title": "Component Title" },
  "fr": { "title": "Titre du Composant" }
}
</i18n>

<!-- Global navigation messages -->
<i18n global>
{
  "en": {
    "nav": {
      "home": "Home",
      "about": "About"
    }
  },
  "fr": {
    "nav": {
      "home": "Accueil", 
      "about": "À propos"
    }
  }
}
</i18n>

Locale-specific Blocks

<template>
  <div>{{ t('message') }}</div>
</template>

<script setup>
import { useI18n } from 'vue-i18n';
const { t } = useI18n({ useScope: 'local' });
</script>

<!-- English-only block -->
<i18n locale="en">
{
  "message": "English-specific message",
  "feature": "This feature is English-only"
}
</i18n>

<!-- French-only block -->
<i18n locale="fr" lang="yaml">
message: Message spécifique au français
feature: Cette fonctionnalité est réservée au français
</i18n>

Custom Block Transformation

// Plugin configuration with custom transformation
VueI18nPlugin({
  transformI18nBlock: (source) => {
    // Transform array of message keys to full message objects
    if (source.trim().startsWith('[')) {
      const keys = JSON.parse(source);
      const messages = {};
      
      keys.forEach(key => {
        messages.en = messages.en || {};
        messages.fr = messages.fr || {};
        messages.en[key] = `English: ${key}`;
        messages.fr[key] = `Français: ${key}`;
      });
      
      return JSON.stringify(messages);
    }
    return source;
  }
});
<!-- Before transformation -->
<i18n>
[
  "welcome",
  "goodbye",
  "thank-you"
]
</i18n>

<!-- After transformation (automatic) -->
<i18n>
{
  "en": {
    "welcome": "English: welcome",
    "goodbye": "English: goodbye", 
    "thank-you": "English: thank-you"
  },
  "fr": {
    "welcome": "Français: welcome",
    "goodbye": "Français: goodbye",
    "thank-you": "Français: thank-you"
  }
}
</i18n>

Configuration Examples

Basic SFC Configuration

VueI18nPlugin({
  defaultSFCLang: 'yaml',
  globalSFCScope: false
});

Advanced SFC Configuration

VueI18nPlugin({
  defaultSFCLang: 'json5',
  globalSFCScope: true,
  transformI18nBlock: (src) => {
    // Custom preprocessing logic
    return preprocessI18nBlock(src);
  }
});

Development vs Production

const isDev = process.env.NODE_ENV === 'development';

VueI18nPlugin({
  defaultSFCLang: isDev ? 'yaml' : 'json',
  globalSFCScope: false,
  strictMessage: !isDev,
  escapeHtml: !isDev
});

docs

build-optimizations.md

index.md

plugin-configuration.md

resource-processing.md

sfc-integration.md

tile.json