Skip to content

Commit

Permalink
Automated more of getting locale files from Twitter
Browse files Browse the repository at this point in the history
  • Loading branch information
insin committed Jan 30, 2023
1 parent d156f61 commit 3cdebba
Showing 10 changed files with 37 additions and 110 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ node_modules/
promo/*.png
!promo/blue_promo.png
scripts/locales/html/*.html
scripts/locales/js/_files.txt
scripts/locales/js/*.js
scripts/locales/locales.js
web-ext-artifacts/
3 changes: 0 additions & 3 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
@@ -64,9 +64,6 @@
"hideMoreTweetsLabel": {
"message": "Hide \"Discover more\" tweets"
},
"sharedTweetsOptionsLabel": {
"message": "Tweets shared by other users"
},
"retweetsLabel": {
"message": "Retweets"
},
3 changes: 0 additions & 3 deletions _locales/es/messages.json
Original file line number Diff line number Diff line change
@@ -64,9 +64,6 @@
"hideMoreTweetsLabel": {
"message": "Ocultar tweets de \"Ver más\""
},
"sharedTweetsOptionsLabel": {
"message": "Tweets compartidos por otros usuarios"
},
"retweetsLabel": {
"message": "Retweets"
},
3 changes: 0 additions & 3 deletions _locales/ja/messages.json
Original file line number Diff line number Diff line change
@@ -64,9 +64,6 @@
"hideMoreTweetsLabel": {
"message": "ツイートの「もっと見る」を非表示"
},
"sharedTweetsOptionsLabel": {
"message": "他のユーザーが共有したツイート"
},
"retweetsLabel": {
"message": "リツイート"
},
16 changes: 11 additions & 5 deletions scripts/locales/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
## Updating Locales

Tweak `html/_files.txt` if needed to add new locales.
### Get locale files from Twitter

In `html/` run `curl -K _files.txt` to retrieve HTML from Twitter with the latest URLs for hashed locale files.
> Tweak `html/_files.txt` and `create-js-curl-config.js` first if locales have changed.
Use the contents of the HTML files to update `js/_files.txt` - left as an exercise for the reader.
```sh
(cd html && curl -K _files.txt)
node create-js-curl-config.js
(cd js && curl -K _files.txt)
```

In `js/` run `curl -K _files.txt` to retrieve locale files.
### Create locale object for tweak-new-twitter.user.js

Run `node create-locales.js` to create a `locales.js` file and use it to update `tweak-new-twitter.user.js`.
Run `node create-locales.js` to create `locales.js`.

Open it, save to format it with Prettier, then use its contents to update `tweak-new-twitter.user.js`.
23 changes: 23 additions & 0 deletions scripts/locales/create-js-curl-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fs = require('fs')
const path = require('path')

let curlConfig = ''
for (let file of fs.readdirSync('./html')) {
if (!file.endsWith('.html')) continue
let localeCode = file.split('.')[0]
let html = fs.readFileSync(path.join('html', file), {encoding: 'utf8'})
let match = html.match(
new RegExp(
`https://abs\\.twimg\\.com/responsive-web/client-web-legacy/i18n/${localeCode}\\.(?:[a-z\\d]+)\\.js`
)
)

if (!match) {
console.log('could not find locale file URL', {file, localeCode})
continue
}

curlConfig += `-o ${localeCode}.js\nurl="${match[0]}"\n`
}

fs.writeFileSync('./js/_files.txt', curlConfig)
2 changes: 1 addition & 1 deletion scripts/locales/create-locales.js
Original file line number Diff line number Diff line change
@@ -231,6 +231,6 @@ for (let file of fs.readdirSync('./js')) {
locales[localeCode] = sortProperties(locale)
}

fs.writeFileSync('locales.js', JSON.stringify(locales, null, 2), {
fs.writeFileSync('locales.js', 'let locales = ' + JSON.stringify(locales, null, 2), {
encoding: 'utf8',
})
Empty file added scripts/locales/js/.gitkeep
Empty file.
94 changes: 0 additions & 94 deletions scripts/locales/js/_files.txt

This file was deleted.

2 changes: 1 addition & 1 deletion tweak-new-twitter.user.js
Original file line number Diff line number Diff line change
@@ -651,7 +651,7 @@ const locales = {
uk: {
ADD_MUTED_WORD: 'Додати слово до списку ігнорування',
DISCOVER_MORE: 'Дізнавайтеся більше',
FOLLOWING: 'Читає(те)',
FOLLOWING: 'Стрічка',
HOME: 'Головна',
MUTE_THIS_CONVERSATION: 'Ігнорувати цю розмову',
QUOTE_TWEET: 'Цитувати твіт',

0 comments on commit 3cdebba

Please sign in to comment.