21 December 2021

Example where we download via api and parse translations. In multiple language export there is only one format "I18NEXT_MULTILINGUAL_JSON". That format does cut phrase ids using "." as a separator. In this example we create simple language based .json files. Where keys and values are all in a flat structure.

Source code viewer
  1. define('ROOT', dirname(__DIR__) . '/');
  2. require_once ROOT . 'vendor/autoload.php';
  3.  
  4. use Onesky\Api\Client;
  5.  
  6. $path = realpath(ROOT . '/../app/locales/');
  7.  
  8. $files = ['en', 'et', 'ru', 'lv'];
  9.  
  10. $client = new Client();
  11. $client->setApiKey(ONESKY_PUBLIC_KEY)->setSecret(ONESKY_SECRET_KEY);
  12.  
  13. $response = json_decode(($client->translations('multilingual', [
  14. 'project_id' => ONESKY_PROJECT_ID,
  15. 'source_file_name' => 'Manually input',
  16. 'file_format' => 'I18NEXT_MULTILINGUAL_JSON',
  17. ]), TRUE);
  18.  
  19. function recursion($input_key, $input_value, &$array) {
  20. if (!is_string($input_value)) {
  21. foreach ($input_value as $key => $value) {
  22. recursion($input_key . '.' . $key, $value, $array);
  23. }
  24. }
  25. else {
  26. $array[$input_key] = $input_value;
  27. }
  28. }
  29.  
  30. foreach ($files as $file) {
  31. $array = [];
  32. foreach ($response[$file]['translation'] as $key => $value) {
  33. recursion($key, $value, $array);
  34. }
  35. file_put_contents("{$path}/{$file}.json", json_encode($array, JSON_PRETTY_PRINT));
  36. }
Programming Language: PHP