Downloading files and using webhooks

Updated 16 days ago ​by Nick Ustinov

Filenames

Lokalise is a project-based platform, which means we do not keep files as logical entities. There are two ways to group data by filenames when you are exporting.

Option 1. All keys to a single file

For most projects, this is a recommended option. All you need to do is select the file format. Optionally, you may configure exported .zip structure and filename  as required using flexible placeholder options, for example setting structure to locale/%LANG_ISO%.%FORMAT% would produce the following structure:

locale/en_US.json
locale/ru_RU.json
locale/fr.json

In case you did not pay attention to filenames while uploading, that's easy to fix. If some keys are not going to the files as you expect, just navigate to project editor, click affected key name to bring up the key editor, navigate to Advanced tab and make sure Filename assignment is correct for the platform you are exporting to. If there is a language code that should be rotated in the filename on export, just click "Rename filename" and use %LANG_ISO% placeholder instead of hardcoded language code.

Option 2.Use previously assigned filenames

If you need to split keys accross multiple files select this option. There are several things to pay attention to:

  • When importing a file, keys are automatically assigned to filenames.
  • Keys can be assigned to different filenames per platforms (that is useful if you share keys between platforms, e.g. Cancel button text). As an example, you can assign a key to frontend.json on Web and strings.xml on Android.
  • If you need to rotate language code in the filename, use %LANG_ISO% placeholder in the filename, e.g. backend_%LANG_ISO%.%FORMAT%
  • Adding a new key via web interface or API, you can either keep it unassigned to filename or assign to existing / create new filename.


Webhooks

The webhooks are assigned per project per user, meaning if you share a project with another owner, the other person will not have the same webhooks or export options you set up for the project. If you need to share the webhook settings with other project users, simply use the sidebar chat.

Once a webhook fires (once the export has been performed), the request is sent to a specified URL. Lokalise sends HTTP POST['file'] to a URL you set. For security reasons only the filename is sent, so you need to add https://s3-eu-west-1.amazonaws.com/lokalise-assets/ in front when pulling. Here is a sample PHP script to pull and unzip the bundle: 


<?php
// requires php curl 
$url = 'https://s3-eu-west-1.amazonaws.com/lokalise-assets/' . $_POST['file'];
$local_file = '/tmp/langs.zip';
$extract_path = '/var/www/mysite.com/';
    
$zip = new ZipArchive;
$ch = curl_init(); 
$fp = fopen($local_file, "w");     
    
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);  
curl_close($ch); 
fclose($fp); 
    
if ($zip->open($local_file)) {
    $zip->extractTo($extract_path);
    $zip->close();
} else {
    echo "Could not download remote file.";
}

How did we do