forked from ImranR98/Obtainium
-
Notifications
You must be signed in to change notification settings - Fork 3
Feature/migrate to flutter localization #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 4 commits
Commits
Show all changes
40 commits
Select commit
Hold shift + click to select a range
8301e12
First step to migrare away from easy_localizaion
omeritzics 9932abf
Clean un-needed files
omeritzics 0110ecf
Merge remote-tracking branch 'origin/dev' into feature/migrate-to-flu…
omeritzics b68d65e
Migrate away from easy localization
omeritzics ecc1340
Update convert_l10n.py
omeritzics 16870b8
Update lib/generated/app_localizations_zh.dart
omeritzics 0305d9d
Update lib/generated/app_localizations_zh.dart
omeritzics bc06f74
Update lib/generated/app_localizations_en.dart
omeritzics b74bf3f
Update lib/generated/app_localizations_zh.dart
omeritzics 43a3334
Delete od_output.txt
omeritzics 847282e
Delete msg_list.txt
omeritzics 58275ab
Delete line_debug.txt
omeritzics c0e5591
Delete marker_debug.txt
omeritzics 832c1d4
Delete find_flutter.txt
omeritzics 4b2c39e
Delete home_list.txt
omeritzics 360a821
Delete flutter_path.txt
omeritzics d17b281
Delete gen_output.txt
omeritzics 46d29cf
Delete output.txt
omeritzics 7c0b7b1
Delete conflict_debug.txt
omeritzics 786755a
Update lib/generated/app_localizations_pt.dart
omeritzics 838dc81
Update lib/generated/app_localizations_en.dart
omeritzics 2a63e39
Update lib/generated/app_localizations_en.dart
omeritzics 7417331
Update lib/generated/app_localizations_pt.dart
omeritzics b716e85
Merge branch 'main' into feature/migrate-to-flutter-localization
omeritzics 6d42656
Update lib/generated/app_localizations_zh.dart
omeritzics a49f4c5
Update lib/generated/app_localizations_pt.dart
omeritzics 1a41b8e
Update lib/generated/app_localizations_pt.dart
omeritzics 38a2ee6
Update lib/generated/app_localizations_zh.dart
omeritzics 46e0302
Update lib/generated/app_localizations_pt.dart
omeritzics 3f72f98
Update lib/generated/app_localizations_en.dart
omeritzics 7dee0fb
Update lib/generated/app_localizations_zh.dart
omeritzics 0a1a589
Update lib/generated/app_localizations_pt.dart
omeritzics 18acb1e
Update lib/generated/app_localizations_en.dart
omeritzics 4814698
Update lib/generated/app_localizations_en.dart
omeritzics 7167d10
Update lib/generated/app_localizations.dart
omeritzics 54639ce
Update lib/generated/app_localizations.dart
omeritzics b9ca93f
Update lib/generated/app_localizations.dart
omeritzics 615e324
Update lib/generated/app_localizations_pt.dart
omeritzics 1b117c4
Update lib/generated/app_localizations_pt.dart
omeritzics aff0cb8
Update lib/generated/app_localizations_en.dart
omeritzics File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| <<<<<<< HEAD | ||
| action = Semantics( | ||
| button: true, | ||
| label: AppLocalizations.of(context)!.install, | ||
| hint: appsProvider.areDownloadsRunning() | ||
omeritzics marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| import json | ||
| import os | ||
| import re | ||
|
|
||
| def to_camel_case(s): | ||
| acronyms = ["APK", "URL", "GH", "PAT", "GPL", "HTTP", "ZIP"] | ||
| for a in acronyms: | ||
| if s.startswith(a): | ||
| s = a.lower() + s[len(a):] | ||
| break | ||
| if not s: | ||
| return s | ||
| return s[0].lower() + s[1:] | ||
|
|
||
| def extract_placeholders(s): | ||
| # Find all {name} or {} | ||
| return re.findall(r'\{([^{}]*)\}', s) | ||
|
|
||
| def convert_json_to_arb(source_dir, target_dir): | ||
| if not os.path.exists(target_dir): | ||
| os.makedirs(target_dir) | ||
|
|
||
| # Load English first as source of truth for placeholders | ||
| en_path = os.path.join(source_dir, "en.json") | ||
| with open(en_path, 'r', encoding='utf-8') as f: | ||
| en_data = json.load(f) | ||
|
|
||
| # Pre-process English to determine placeholder names and types | ||
| key_metadata = {} | ||
| for key, value in en_data.items(): | ||
| new_key = to_camel_case(key) | ||
| placeholders = [] | ||
| if isinstance(value, str): | ||
| # Replace {} with p0, p1... | ||
| count = 0 | ||
| def repl(m): | ||
| nonlocal count | ||
| p = f"p{count}" | ||
| count += 1 | ||
| return p | ||
| # We want to know the names | ||
| names = extract_placeholders(value) | ||
| p_names = [] | ||
| p_idx = 0 | ||
| for name in names: | ||
| if name == "": | ||
| p_names.append(f"p{p_idx}") | ||
| p_idx += 1 | ||
| else: | ||
| p_names.append(name) | ||
| key_metadata[new_key] = {"placeholders": p_names, "is_plural": False} | ||
| elif isinstance(value, dict) and "one" in value and "other" in value: | ||
| # Plural. We'll use 'count' and then any others | ||
| names = extract_placeholders(value['one']) + extract_placeholders(value['other']) | ||
| unique_names = [] | ||
| for n in names: | ||
| if n and n not in unique_names and n != "": | ||
| unique_names.append(n) | ||
| key_metadata[new_key] = {"placeholders": unique_names, "is_plural": True} | ||
|
|
||
| for filename in os.listdir(source_dir): | ||
| if filename.endswith(".json") and filename not in ["package-lock.json", "package.json"]: | ||
| locale = filename.replace(".json", "") | ||
| flutter_locale = locale.replace("-", "_") | ||
|
|
||
| source_path = os.path.join(source_dir, filename) | ||
| target_path = os.path.join(target_dir, f"app_{flutter_locale}.arb") | ||
|
|
||
| with open(source_path, 'r', encoding='utf-8') as f: | ||
| data = json.load(f) | ||
|
|
||
| arb_data = {"@@locale": flutter_locale} | ||
|
|
||
| for key, value in data.items(): | ||
| new_key = to_camel_case(key) | ||
| meta = key_metadata.get(new_key) | ||
| if not meta: | ||
| continue # Should not happen if en.json is complete | ||
|
|
||
| if isinstance(value, str): | ||
| # Replace placeholders by order | ||
| names = extract_placeholders(value) | ||
| new_value = value | ||
| # Replace {} first | ||
| p_idx = 0 | ||
| while "{}" in new_value: | ||
| new_value = new_value.replace("{}", f"{{p{p_idx}}}", 1) | ||
| p_idx += 1 | ||
|
|
||
| # Replace named placeholders if they were translated | ||
| # This is tricky. We'll replace all {anything} with meta placeholders by order. | ||
| current_placeholders = re.findall(r'\{([^{}]+)\}', new_value) | ||
| for i, old_p in enumerate(current_placeholders): | ||
| if i < len(meta['placeholders']): | ||
| new_p = meta['placeholders'][i] | ||
| # Avoid replacing if it's already correct or if it's a p0 style | ||
| if old_p != new_p: | ||
| new_value = new_value.replace(f"{{{old_p}}}", f"{{{new_p}}}") | ||
|
|
||
| arb_data[new_key] = new_value | ||
| if meta['placeholders']: | ||
| arb_data[f"@{new_key}"] = { | ||
| "placeholders": {p: {"type": "Object"} for p in meta['placeholders']} | ||
| } | ||
|
|
||
| elif isinstance(value, dict) and meta['is_plural']: | ||
| one = value.get('one', '') | ||
| other = value.get('other', '') | ||
|
|
||
| # Normalize placeholders in one/other | ||
| def normalize_plural_segment(seg): | ||
| if not seg: return seg | ||
| # Replace {} with {count} | ||
| seg = seg.replace("{}", "{count}") | ||
|
|
||
| # Find all placeholders EXCEPT 'count' | ||
| # Use a set to keep track of what we've seen if we want to replace by index | ||
| # but wait, if the same translated placeholder appears twice, we should replace both. | ||
| found_ps = [] | ||
| for p in re.findall(r'\{([^{}]+)\}', seg): | ||
| if p != 'count' and p not in found_ps: | ||
| found_ps.append(p) | ||
|
|
||
| for i, old_p in enumerate(found_ps): | ||
| if i < len(meta['placeholders']): | ||
| new_p = meta['placeholders'][i] | ||
| if old_p != new_p: | ||
| seg = seg.replace(f"{{{old_p}}}", f"{{{new_p}}}") | ||
| return seg | ||
|
|
||
| one = normalize_plural_segment(one) | ||
| other = normalize_plural_segment(other) | ||
|
|
||
| arb_data[new_key] = f"{{count, plural, one{{{one}}} other{{{other}}}}}" | ||
| p_meta = {"count": {"type": "num"}} | ||
| for p in meta['placeholders']: | ||
| p_meta[p] = {"type": "Object"} | ||
| arb_data[f"@{new_key}"] = {"placeholders": p_meta} | ||
|
|
||
| with open(target_path, 'w', encoding='utf-8') as f: | ||
| json.dump(arb_data, f, ensure_ascii=False, indent=2) | ||
| print(f"Converted {filename} to app_{flutter_locale}.arb") | ||
|
|
||
| if __name__ == "__main__": | ||
| convert_json_to_arb( | ||
| "/home/omeritzics/Dev-Projects/Updatium/assets/translations", | ||
| "/home/omeritzics/Dev-Projects/Updatium/lib/l10n" | ||
| ) | ||
omeritzics marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /home/omeritzics/Dev-Projects/Updatium/.flutter/bin/flutter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /usr/bin/which: no flutter in (/home/omeritzics/Dev-Projects/Updatium/depot_tools:/home/omeritzics/מסמכים/Projects/flutter/bin:/home/omeritzics/depot_tools:/home/omeritzics/מסמכים/Projects/flutter/bin:/home/omeritzics/depot_tools:/home/omeritzics/.local/bin:/home/omeritzics/bin:/usr/lib64/ccache:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/omeritzics/.dotnet/tools:/home/omeritzics/.lmstudio/bin:/home/omeritzics/.lmstudio/bin:/home/omeritzics/.lmstudio/bin:/home/omeritzics/.lmstudio/bin:/home/omeritzics/.lmstudio/bin) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file appears to be a merge conflict artifact. It, along with
find_flutter.txt,flutter_path.txt,gen_output.txt, andhome_list.txt, seems to be a temporary or debug file that was accidentally committed. These files should be removed from the pull request and added to.gitignoreto prevent them from being committed in the future.