2020-06-28 10:55:27 +02:00
using System.IO ;
using System.Linq ;
2018-11-14 08:48:25 +01:00
using System.Net.Http ;
using System.Text ;
using System.Threading.Tasks ;
using Microsoft.Extensions.Configuration ;
using NBitcoin.DataEncoders ;
using Newtonsoft.Json.Linq ;
using Xunit ;
namespace BTCPayServer.Tests
{
/// <summary>
/// This class hold easy to run utilities for dev time
/// </summary>
public class UtilitiesTests
{
/// <summary>
/// Download transifex transactions and put them in BTCPayServer\wwwroot\locales
/// </summary>
2021-11-23 05:17:29 +01:00
[FactWithSecret("TransifexAPIToken")]
2018-11-14 08:48:25 +01:00
[Trait("Utilities", "Utilities")]
public async Task PullTransifexTranslations ( )
{
// 1. Generate an API Token on https://www.transifex.com/user/settings/api/
// 2. Run "dotnet user-secrets set TransifexAPIToken <youapitoken>"
2021-11-23 05:17:29 +01:00
var client = new TransifexClient ( FactWithSecretAttribute . GetFromSecrets ( "TransifexAPIToken" ) ) ;
2018-11-14 08:48:25 +01:00
var json = await client . GetTransifexAsync ( "https://api.transifex.com/organizations/btcpayserver/projects/btcpayserver/resources/enjson/" ) ;
2018-12-01 05:19:35 +01:00
var langs = new [ ] { "en" } . Concat ( ( ( JObject ) json [ "stats" ] ) . Properties ( ) . Select ( n = > n . Name ) ) . ToArray ( ) ;
2018-11-14 08:48:25 +01:00
2019-10-06 16:41:27 +02:00
var langsDir = Path . Combine ( TestUtils . TryGetSolutionDirectoryInfo ( ) . FullName , "BTCPayServer" , "wwwroot" , "locales" ) ;
2018-11-14 08:48:25 +01:00
2018-12-01 05:19:35 +01:00
JObject sourceLang = null ;
2018-11-14 08:48:25 +01:00
Task . WaitAll ( langs . Select ( async l = >
{
2018-12-01 05:19:35 +01:00
bool isSourceLang = l = = "en" ;
2018-11-14 08:48:25 +01:00
var j = await client . GetTransifexAsync ( $"https://www.transifex.com/api/2/project/btcpayserver/resource/enjson/translation/{l}/" ) ;
2020-06-28 10:55:27 +02:00
if ( ! isSourceLang )
2018-12-01 05:19:35 +01:00
{
while ( sourceLang = = null )
await Task . Delay ( 10 ) ;
}
2018-11-14 08:48:25 +01:00
var content = j [ "content" ] . Value < string > ( ) ;
if ( l = = "ne_NP" )
l = "np_NP" ;
if ( l = = "zh_CN" )
l = "zh-SP" ;
if ( l = = "kk" )
l = "kk-KZ" ;
var langCode = l . Replace ( "_" , "-" ) ;
var langFile = Path . Combine ( langsDir , langCode + ".json" ) ;
var jobj = JObject . Parse ( content ) ;
jobj [ "code" ] = langCode ;
2018-12-12 06:18:17 +01:00
2018-12-01 05:19:35 +01:00
if ( ( string ) jobj [ "currentLanguage" ] = = "English" & & ! isSourceLang )
return ; // Not translated
2018-12-12 06:18:17 +01:00
if ( ( string ) jobj [ "currentLanguage" ] = = "disable" )
return ; // Not translated
2018-11-14 08:48:25 +01:00
jobj . AddFirst ( new JProperty ( "NOTICE_WARN" , "THIS CODE HAS BEEN AUTOMATICALLY GENERATED FROM TRANSIFEX, IF YOU WISH TO HELP TRANSLATION COME ON THE SLACK http://slack.btcpayserver.org TO REQUEST PERMISSION TO https://www.transifex.com/btcpayserver/btcpayserver/" ) ) ;
2018-12-01 05:19:35 +01:00
if ( isSourceLang )
{
sourceLang = jobj ;
}
else
{
2020-06-28 10:55:27 +02:00
if ( jobj [ "InvoiceExpired_Body_3" ] . Value < string > ( ) = = sourceLang [ "InvoiceExpired_Body_3" ] . Value < string > ( ) )
2018-12-01 05:19:35 +01:00
{
jobj [ "InvoiceExpired_Body_3" ] = string . Empty ;
}
}
2018-11-14 08:48:25 +01:00
content = jobj . ToString ( Newtonsoft . Json . Formatting . Indented ) ;
File . WriteAllText ( Path . Combine ( langsDir , langFile ) , content ) ;
} ) . ToArray ( ) ) ;
}
}
public class TransifexClient
{
public TransifexClient ( string apiToken )
{
Client = new HttpClient ( ) ;
APIToken = apiToken ;
}
public HttpClient Client { get ; }
public string APIToken { get ; }
public async Task < JObject > GetTransifexAsync ( string uri )
{
var message = new HttpRequestMessage ( HttpMethod . Get , uri ) ;
message . Headers . Authorization = new System . Net . Http . Headers . AuthenticationHeaderValue ( "Basic" , Encoders . Base64 . EncodeData ( Encoding . ASCII . GetBytes ( $"api:{APIToken}" ) ) ) ;
message . Headers . Accept . Add ( new System . Net . Http . Headers . MediaTypeWithQualityHeaderValue ( "application/json" ) ) ;
var response = await Client . SendAsync ( message ) ;
return await response . Content . ReadAsAsync < JObject > ( ) ;
}
}
}