Hash based multi language solution

UpvoteUpvote 2 DownvoteDownvote

Index

Features on these Courses

Attached Files

The following files have been attached to this tutorial:

.c3p

multi-language-using-hash.c3p

Download now 170.6 KB

Stats

212 visits, 328 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 1 May, 2025. Last updated 2 May, 2025

Generate Strings Hash

I chose the hashFnv1a64 algorithm. It's ideal for parallel hash - calculating large - scale data, computing values fast with low overhead. With a uniform distribution, it has a very low collision probability.

Add a script file in the Scripts folder, Paste the code into it, and set the Purpose property to "Imports For Events".

function hashFnv1a64(input: string): string {
  const FNV_PRIME_LOW = 0x1b3;
  const FNV_PRIME_HIGH = 0x1000000;
  let hashLow = 0x62b82175;
  let hashHigh = 0xcbf29ce4;

  for (let i = 0; i < input.length; i++) {
    const char = input.charCodeAt(i);

    // XOR low and high with char
    hashLow ^= char;

    // Multiply 64-bit hash by 64-bit prime (FNV_PRIME)
    const low = hashLow * FNV_PRIME_LOW;
    const high = hashLow * FNV_PRIME_HIGH + hashHigh * FNV_PRIME_LOW;

    hashLow = low >>> 0;
    hashHigh = high >>> 0;
  }

  // Combine high and low to hex string
  const highHex = hashHigh.toString(16).padStart(8, '0');
  const lowHex = hashLow.toString(16).padStart(8, '0');
  return highHex + lowHex;
}

In this way, we can use this hashFnv1a64 in the code block of the eventsheet. I added a Hash function to return the processing result of hashFnv1a64.

{"is-c3-clipboard-data":true,"type":"events","items":[{"functionName":"Hash","functionDescription":"","functionCategory":"","functionReturnType":"string","functionCopyPicked":false,"functionIsAsync":false,"functionParameters":[{"name":"String","type":"string","initialValue":"","comment":""}],"eventType":"function-block","conditions":[],"actions":[{"type":"script","language":"typescript","script":["runtime.setReturnValue(hashFnv1a64(localVars.String));"]}]}]}

Set function return value:

runtime.setReturnValue();

Referencing function parameters (local variables):

localVars.String

Generate Language JSON

This can be initialized once after loading the language file: iterate through the Array of the original language and generate their Hash. I store them separately in a Hash array here.

In the game, when we want to use translated String, it will generate a hash from the original language and then find the corresponding Hash Key in JSON. And a fallback mechanism to use back to the original language if the hash is not found.

.C3P

multi-language-using-hash.c3p

Download now 170.6 KB
  • 0 Comments

Want to leave a comment? Login or Register an account!