0.8.103 • Published 1 year ago

@enterprise_search/translation v0.8.103

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Translation Software

This package provides a framework for handling multi-language translations in projects. It supports loading and saving translation files in both JSON and CSV formats, validating configuration, and checking mismatches between translations.


Installation

npm install "@enterprise_search/translation"

Or if you want it from the command line

npm install -g "@enterprise_search/indexer"

From the command line

This requires a config file in the name

npm install -g "@enterprise_search/indexer"

index experiment translation-init         # Createsan example translation.yaml file. 
                                          # Gives instructions on where files should be placed

index experiment translation              # Turns csv files into json files
index experiment translation --invert     # Turns json files into csv files
index experiment translation --help       # For more information 

Features

  • Converts a csv file with langauges for columns into json suitable for i18n
  • Can 'invert' the process to convert json files into csv files. This is useful if you have existing json files you want to consolidate and manage better
  • Error Handling: Returns validation and processing errors, while 'doing as much as it can'
  • Mismatch detection: checks for mismatchs across languages... places where something is defined in one language but not in another.

Important Files and Modules

config/config.ts

Holds the type definitions for the core entities:

  • TranslationConfig: Describes the configuration for translations.
  • CsvFilesDescription and JsonFilesDescription: Define the source and target file structures.
  • Contains helper methods:
  • validateTranslationConfig(config: TranslationConfig): Validates the configuration.
  • invertTranslationConfig(config: TranslationConfig): Switches the source and target for reverse operations.

translate.ts

Core functions for performing translations:

  • translateAllLanguageFiles: Translates all specified files in a configuration.
  • _translateOneLanguageFile: Translates a single file, returning any errors or mismatches.

loaders and savers

  • loadCsvLanguageFile: Loads translations from CSV files.
  • saveCsvLanguageFile: Saves translations to CSV files.
  • loadJsonLanguageFiles: Loads translations from JSON files.
  • saveJsonLanguageFile: Saves translations to JSON files.

Example Usage

Sample Configuration: by default in a file called translation.yaml

source: # This is the source of truth for the language files
  type: csv
  directory: sample.translation/csv/{file}.csv  # Note the {file} placeholder. Change this to your directory structure.
  pathFieldName: path

target: # This is where we put the generated language files.
  type: json
  directory: sample.translation/json/{file}/{language}.json # Note the {file} and {language} placeholders. Change this to your directory structure.

languages: # This is the list of languages we want to generate
  - de
  - en

languageFiles: # And for each language, we want to generate these files
  - about-us
  - room-booking
  - translation```

Translation Workflow

async function runTranslation(config: TranslationConfig) {
  // Validate configuration
  const validate = defaultTranslationOperations.validateTranslationConfig(config);

  if (hasErrors(validate)) {
    console.error('Configuration errors:', validate);
    return;
  }

  // Perform translations
  try {
    const errors = await translateAllLanguageFiles(defaultTranslationOperations, config);
    if (errors.length) {
      console.error('Translation errors:', errors);
    } else {
      console.log('Translations completed successfully!');
    }
  } catch (error) {
    console.error('Unexpected error:', error.message);
  }
}

runTranslation(config);

License

MIT License. See the LICENSE.md file for more details.

ChatGPT session

This is the session where we wrote the software. https://chatgpt.com/share/674b0a46-f4e4-8013-9869-4ccd016a0209

0.8.103

1 year ago

0.8.102

1 year ago

0.8.101

1 year ago

0.8.100

1 year ago

0.8.99

1 year ago

0.8.98

1 year ago

0.8.97

1 year ago

0.8.96

1 year ago

0.8.95

1 year ago

0.8.94

1 year ago

0.8.93

1 year ago

0.8.92

1 year ago

0.8.91

1 year ago

0.8.90

1 year ago

0.8.89

1 year ago

0.8.88

1 year ago

0.8.87

1 year ago

0.8.86

1 year ago

0.8.85

1 year ago

0.8.84

1 year ago

0.8.83

1 year ago

0.8.81

1 year ago

0.8.80

1 year ago

0.8.79

1 year ago

0.8.78

1 year ago

0.8.77

1 year ago

0.8.76

1 year ago

0.8.75

1 year ago

0.8.74

1 year ago

0.8.73

1 year ago

0.8.72

1 year ago

0.8.71

1 year ago

0.8.70

1 year ago

0.8.69

1 year ago

0.8.68

1 year ago

0.8.67

1 year ago

0.8.66

1 year ago

0.8.65

1 year ago

0.8.62

1 year ago

0.8.61

1 year ago

0.8.60

1 year ago

0.8.58

1 year ago

0.8.57

1 year ago

0.8.56

1 year ago

0.8.55

2 years ago

0.8.54

2 years ago

0.8.53

2 years ago

0.8.52

2 years ago

0.8.51

2 years ago

0.8.50

2 years ago

0.8.49

2 years ago

0.8.48

2 years ago

0.8.47

2 years ago