技能 编程开发 Lokalise 性能调优

Lokalise 性能调优

v20260311
lokalise-performance-tuning
通过游标分页、缓存、批量键操作、请求节流和基于 Webhook 的流程,提升 Lokalise 翻译集成的吞吐与响应速度,并规避全局调用速率限制。
获取技能
369 次下载
概览

Lokalise Performance Tuning

Overview

Optimize Lokalise API throughput and response times for translation pipeline integrations. Lokalise enforces a global rate limit of 6 requests per second across most endpoints, making request efficiency critical for projects with thousands of keys.

Prerequisites

  • Lokalise SDK (@lokalise/node-api) or REST API access
  • Understanding of project size (key count, language count)
  • Cache layer (Redis or in-memory LRU)

Instructions

Step 1: Use Cursor Pagination with Maximum Page Size

import { LokaliseApi } from '@lokalise/node-api';
const lok = new LokaliseApi({ apiKey: process.env.LOKALISE_API_TOKEN! });

// Use cursor pagination (faster than offset for large datasets)
async function* getAllKeys(projectId: string) {
  let cursor: string | undefined;
  do {
    const result = await lok.keys().list({
      project_id: projectId,
      limit: 500,           // Maximum allowed  # HTTP 500 Internal Server Error
      pagination: 'cursor',
      cursor,
    });
    for (const key of result.items) yield key;
    cursor = result.hasNextCursor() ? result.nextCursor : undefined;
  } while (cursor);
}
// 10,000 keys: 20 API calls instead of 100 (at limit=500 vs default limit=100)  # HTTP 500 Internal Server Error

Step 2: Cache Translation Downloads

import { LRUCache } from 'lru-cache';

const downloadCache = new LRUCache<string, any>({ max: 100, ttl: 300_000 }); // 5 min

async function cachedDownload(projectId: string, format: string, langIso: string) {
  const key = `${projectId}:${format}:${langIso}`;
  const cached = downloadCache.get(key);
  if (cached) return cached;

  const bundle = await lok.files().download(projectId, {
    format, filter_langs: [langIso], original_filenames: false,
  });
  downloadCache.set(key, bundle);
  return bundle;
}

Step 3: Batch Key Operations

// Bulk create keys (up to 500 per request)  # HTTP 500 Internal Server Error
async function createKeysBatched(projectId: string, keys: any[]) {
  const BATCH_SIZE = 500;  # HTTP 500 Internal Server Error
  const results = [];
  for (let i = 0; i < keys.length; i += BATCH_SIZE) {
    const batch = keys.slice(i, i + BATCH_SIZE);
    const result = await lok.keys().create({ project_id: projectId, keys: batch });
    results.push(...result.items);
    await new Promise(r => setTimeout(r, 200)); // Respect rate limit  # HTTP 200 OK
  }
  return results;
}
// 2,000 keys: 4 batched requests vs 2,000 individual requests

Step 4: Implement Request Throttling

import PQueue from 'p-queue';

// Lokalise rate limit: 6 requests/second
const queue = new PQueue({ concurrency: 5, interval: 1000, intervalCap: 5 });  # 1000: 1 second in ms

async function throttledRequest<T>(fn: () => Promise<T>): Promise<T> {
  return queue.add(fn) as Promise<T>;
}

// All API calls go through the queue
const project = await throttledRequest(() => lok.projects().get(projectId));

Step 5: Use Webhooks Instead of Polling

set -euo pipefail
# Replace polling for translation status with webhooks
curl -X POST "https://api.lokalise.com/api2/projects/PROJECT_ID/webhooks" \
  -H "X-Api-Token: $LOKALISE_API_TOKEN" \
  -d '{
    "url": "https://hooks.company.com/lokalise",
    "events": ["project.translation_completed", "project.exported"]
  }'
# Eliminates need to poll project status every N seconds

Error Handling

Issue Cause Solution
429 Too Many Requests Exceeded 6 req/s rate limit Use PQueue throttling, retry with backoff
Slow file downloads Large project with many languages Filter by language, use async download + webhook
Pagination timeout Offset pagination on 50K+ keys Switch to cursor pagination
Bulk create fails partially Network timeout on large batch Reduce batch size to 200, add retry logic

Examples

Basic usage: Apply lokalise performance tuning to a standard project setup with default configuration options.

Advanced scenario: Customize lokalise performance tuning for production environments with multiple constraints and team-specific requirements.

Output

  • Configuration files or code changes applied to the project
  • Validation report confirming correct implementation
  • Summary of changes made and their rationale

Resources

  • Official ORM documentation
  • Community best practices and patterns
  • Related skills in this plugin pack
信息
Category 编程开发
Name lokalise-performance-tuning
版本 v20260311
大小 4.96KB
更新时间 2026-03-12
语言