vanilladocs
vanillapunch.com
API / misc/languagepack

misc/languagepack

언어 묶음. (자산의 표로 두는 화면 글자의 언어, LanguagePack 으로 들여온다)

  • Localization(코드 안의 표)과 달리 글자를 자산의 JSON 파일로 둔다. 언어가 많고 번역가가 파일을 따로 고치는 게임에 쓴다.
  • 경로는 setLanguagePaths 로 정한다. (처음은 ./assets/localization/languages.json 과 그 폴더)
  • 언어를 불러온 뒤 할 일(그 언어의 글꼴 받기 등)은 setLanguageLoadedHandler 로 건다.

글자는 소스가 아니라 자산의 표에 있다. 언어의 목록은 languages.json 이고, 언어마다 한 파일(열쇠 → 글자)이다. 모든 언어의 파일은 기본 언어의 파일과 같은 열쇠를 갖는다. 설정의 언어가 비어 있으면 플랫폼이 알려 준 언어, 그다음 기기(브라우저)의 언어를 따르고, 목록에 없으면 기본 언어(영어)이다. 고른 언어의 표에 없는 열쇠는 기본 언어의 글자로, 거기에도 없으면 열쇠 그대로 쓴다. 경로를 글자 그대로 적어 두어 빌드의 캐시 무효화가 지문을 붙이게 한다. (언어 파일의 지문은 빌드가 목록에 붙인다)

Language pack. (screen text languages kept as asset tables, imported as LanguagePack)

  • Unlike Localization (tables in code), keeps text in asset JSON files. Used for games with many languages where translators edit the files separately.
  • Paths are set with setLanguagePaths. (initially ./assets/localization/languages.json and its folder)
  • What to do after a language is loaded (fetching that language's font, etc.) is hooked with setLanguageLoadedHandler.

Text lives in asset tables, not in source. The language list is languages.json, and each language has one file (key → text). Every language file has the same keys as the default language file. If the setting's language is empty, it follows the language the platform reported, then the device (browser) language, and if that is not in the list, the default language (English). Keys missing from the chosen language's table use the default language's text, and if missing there too, the key itself. Paths are written as literal strings so the build's cache busting attaches fingerprints. (the build attaches the language file fingerprints to the list)

예제Example파일 없이 목록과 표를 넣고 글자 얻기Set the list and tables without files and get text
import { LanguagePack } from "./build/vanilla.js";

LanguagePack.setLanguageList({
	fallbackLanguage: "en",
	languages: [
		{ code: "en", name: "English", englishName: "English", direction: "ltr", aliases: [], file: "en.json" },
		{ code: "ar", name: "Arabic", englishName: "Arabic", direction: "rtl", aliases: ["ar-eg"], file: "ar.json" }
	]
});
const englishTexts = { "hud.restart": "Restart {seconds}", "hud.ammo": "{count}/{limit}" };
LanguagePack.setLanguageTexts("en", englishTexts, englishTexts);
console.log(LanguagePack.readLanguageCode(), LanguagePack.translate("hud.restart"), LanguagePack.formatText("hud.restart", { seconds: 3 }));
const matchedLanguage = LanguagePack.matchLanguageTag("ar-EG");
console.log(matchedLanguage.code, LanguagePack.readTextDirection(), LanguagePack.resolveLanguageCode("ar"));

함수Functions

함수functionasync loadLanguageList()#0.4.0 부터since 0.4.0소스source

언어의 목록 불러오기. (한 번만)

Loads the language list. (once only)

짝Counterpart: vanilla-engine load_language_list

함수functionreadLanguages()#0.4.0 부터since 0.4.0소스source

언어의 목록 반환. (못 읽었으면 빈 배열)

Returns the language list. (empty array if it could not be read)

반환Returnsobject[] 언어 목록의 줄 배열. (못 읽었으면 빈 배열)Array of rows of the language list. (empty if not loaded)

짝Counterpart: vanilla-engine read_languages

함수functionfindLanguage(languageCode)#0.4.0 부터since 0.4.0소스source

언어 찾기. (코드는 대소문자를 가리지 않는다, 없으면 null)

Finds a language. (code is case-insensitive, null if none)

languageCodestring
찾을 언어 코드.Language code to look for.

반환Returnsobject | null 목록의 그 언어 줄. 없으면 null.That language's row in the list. null if missing.

짝Counterpart: vanilla-engine find_language

함수functionmatchLanguageTag(languageTag)#0.4.0 부터since 0.4.0소스source

언어 태그에 맞는 언어. ("pt-BR" → pt, "zh-Hant-TW" → zh-Hant, 뒤의 조각을 하나씩 떼며 코드와 별칭을 본다, 없으면 null)

Language matching a language tag. ("pt-BR" → pt, "zh-Hant-TW" → zh-Hant, strips trailing subtags one by one and checks codes and aliases, null if none)

languageTagstring
언어 태그. ("pt-BR" 같은 꼴, 밑줄도 받는다)Language tag. (in a form like "pt-BR", underscores accepted)

반환Returnsobject | null 맞는 언어 줄. 없으면 null.The matching language row. null if none.

짝Counterpart: vanilla-engine match_language_tag

함수functionsetPreferredLanguageTag(languageTag)#0.4.0 부터since 0.4.0소스source

앞세울 언어 태그 넣기. (플랫폼이 알려 준 사용자의 언어, 기기의 언어보다 먼저 본다, 없으면 "")

Sets the preferred language tag. (the user's language reported by the platform, checked before the device language, "" if none)

languageTagstring
앞세울 언어 태그. (글이 아니면 "")Language tag to prefer. ("" if not a string)

짝Counterpart: vanilla-engine set_preferred_language_tag

함수functionresolveDeviceLanguageCode()#0.4.0 부터since 0.4.0소스source

기기(브라우저)의 언어. (기기가 앞세우는 순서대로 목록에 있는 첫 언어, 없으면 기본 언어)

Device (browser) language. (the first language in the list in the device's preference order, default language if none)

반환Returnsstring 고른 언어 코드. (맞는 것이 없으면 기본 언어 코드)The chosen language code. (the default language code if none matches)

짝Counterpart: vanilla-engine resolve_device_language_code

함수functionreadFallbackLanguageCode()#0.4.0 부터since 0.4.0소스source

기본 언어의 코드.

Default language code.

반환Returnsstring 기본 언어 코드. (목록이 없거나 정하지 않았으면 "en")The default language code. ("en" if there is no list or it is not set)

짝Counterpart: vanilla-engine read_fallback_language_code

함수functionresolveLanguageCode(settingLanguageCode)#0.4.0 부터since 0.4.0소스source

설정의 언어 → 쓸 언어의 코드. (비었거나 목록에 없으면 기기의 언어)

Language setting → code of the language to use. (device language if empty or not in the list)

settingLanguageCodestring
설정에 저장된 언어 코드. (비었거나 목록에 없으면 기기의 언어)Language code saved in the settings. (the device language if empty or not in the list)

반환Returnsstring 쓸 언어 코드.Language code to use.

짝Counterpart: vanilla-engine resolve_language_code

함수functionasync loadLanguage(languageCode)#0.4.0 부터since 0.4.0소스source

언어 불러오기. (그 언어의 표와 기본 언어의 표, 목록에 없는 코드면 기본 언어)

Loads a language. (that language's table and the default language's table, default language if the code is not in the list)

languageCodestring
불러올 언어 코드. (목록에 없으면 기본 언어)Language code to load. (the default language if not in the list)

짝Counterpart: vanilla-engine load_language

함수functionasync applyLanguageSetting(settingLanguageCode)#0.4.0 부터since 0.4.0소스source

설정의 언어 적용하기. (쓸 언어를 정해 그 표를 불러오고, setLanguageLoadedHandler 로 건 함수를 부른다)

Applies the language setting. (decides the language to use, loads its table, and calls the function registered with setLanguageLoadedHandler)

settingLanguageCodestring
"" 이면 기기의 언어."" means the device language.

짝Counterpart: vanilla-engine apply_language_setting

함수functionreadLanguage()#0.4.0 부터since 0.4.0소스source

지금 언어. (목록의 한 줄, 아직 없으면 null)

Current language. (a row of the list, null if none yet)

반환Returnsobject | null 지금 언어 줄. 아직 없으면 null.The current language row. null if none yet.

짝Counterpart: vanilla-engine read_language

함수functionreadLanguageCode()#0.4.0 부터since 0.4.0소스source

지금 언어의 코드. (아직 없으면 기본 언어)

Current language code. (default language if none yet)

반환Returnsstring 지금 언어 코드. 아직 없으면 기본 언어 코드.The current language code. The default language code if none yet.

짝Counterpart: vanilla-engine read_language_code

함수functionreadTextDirection()#0.4.0 부터since 0.4.0소스source

지금 언어의 글자 방향. ("ltr" 또는 "rtl", 언어 목록의 direction, 아직 없으면 "ltr") 오른쪽에서 왼쪽으로 쓰는 언어(아랍어, 페르시아어, 히브리어 등)면 화면의 글자를 그 방향으로 굽고 배치를 좌우로 뒤집는다.

Text direction of the current language. ("ltr" or "rtl", direction in the language list, "ltr" if none yet) For right-to-left languages (Arabic, Persian, Hebrew, etc.), on-screen text is baked in that direction and the layout is mirrored horizontally.

반환Returnsstring "ltr" 또는 "rtl"."ltr" or "rtl".

짝Counterpart: vanilla-engine read_text_direction

함수functionisRightToLeft()#0.4.0 부터since 0.4.0소스source

지금 언어가 오른쪽에서 왼쪽으로 쓰는지. (readTextDirection 이 "rtl" 이면 true)

Whether the current language is written right to left. (true if readTextDirection is "rtl")

반환Returnsboolean 오른쪽에서 왼쪽으로 쓰면 참.True if written right to left.

짝Counterpart: vanilla-engine is_right_to_left

함수functionmirrorLayoutX(x, width, parentWidth)#0.4.0 부터since 0.4.0소스source

배치의 가로 자리. (오른쪽에서 왼쪽 언어면 부모 너비 안에서 좌우를 뒤집은 왼쪽, 아니면 그대로) 창과 표의 왼쪽, 오른쪽 배치를 언어 방향에 맞춰 뒤집을 때 쓴다. 화면 구석의 자리나 슬라이더의 방향처럼 뒤집지 않을 것은 부르지 않는다.

Horizontal layout position. (for right-to-left languages, the left edge mirrored within the parent width, otherwise unchanged) Used to mirror the left and right placement of windows and tables to match the language direction. Not called for things that should not be mirrored, such as screen-corner positions or slider direction.

xnumber
왼쪽에서 오른쪽 언어일 때의 왼쪽.Left for a left-to-right language.
widthnumber
놓을 것의 너비.Width of the thing being placed.
parentWidthnumber
부모의 너비.Width of the parent.

반환Returnsnumber 지금 언어 방향에 맞춘 왼쪽.Left position matched to the current language direction.

짝Counterpart: vanilla-engine mirror_layout_x

함수functionreadLayoutTextAlign(textAlign)#0.4.0 부터since 0.4.0소스source

배치의 글자 정렬. (오른쪽에서 왼쪽 언어면 "left" 와 "right" 를 바꾸고, 나머지는 그대로)

Layout text alignment. (for right-to-left languages, swaps "left" and "right", others unchanged)

textAlignstring
"left", "center", "right", "start", "end"

반환Returnsstring 지금 언어 방향에 맞춘 정렬 값.Alignment value matched to the current language direction.

짝Counterpart: vanilla-engine read_layout_text_align

함수functionisolateText(text)#0.4.0 부터since 0.4.0소스source

글 사이에 끼우는 값 묶기. (오른쪽에서 왼쪽 언어면 방향 격리 문자 FSI ... PDI 로 묶고, 아니면 그대로) 오른쪽에서 왼쪽으로 쓰는 글 안의 숫자와 단위(예: 123°/s, +60%, 88 / 1.5s)나 사람 이름이 앞뒤 글과 섞여 흩어지지 않게, 묶은 값은 제 방향(처음 나오는 방향이 강한 글자, 숫자뿐이면 왼쪽에서 오른쪽)으로 한 덩어리로 놓인다.

Wraps a value inserted into text. (for right-to-left languages, wraps it with the directional isolate characters FSI ... PDI, otherwise unchanged) So that numbers and units (e.g. 123°/s, +60%, 88 / 1.5s) or person names inside right-to-left text do not get mixed with and scattered among the surrounding text, a wrapped value is laid out as one block in its own direction (the first strong directional character, left to right if digits only).

textstring
묶을 값의 글.Text of the value to wrap.

반환Returnsstring 오른쪽에서 왼쪽 언어면 방향 격리 문자로 묶은 글, 아니면 그대로. (빈 글은 그대로)Text wrapped in directional isolate characters for right-to-left languages, otherwise unchanged. (empty text is unchanged)

짝Counterpart: vanilla-engine isolate_text

함수functiontranslate(key)#0.4.0 부터since 0.4.0소스source

열쇠의 글자. (지금 언어 → 기본 언어 → 열쇠 그대로)

Text for a key. (current language → default language → the key itself)

keystring
글자의 열쇠.Key of the text.

반환Returnsstring 지금 언어의 글자. 없으면 기본 언어의 글자, 거기에도 없으면 열쇠 그대로.Text in the current language. The default language text if missing, or the key itself if missing there too.

짝Counterpart: vanilla-engine translate

함수functionformatText(key, values)#0.4.0 부터since 0.4.0소스source

열쇠의 글자에 값 넣기. ("Restart {seconds}" + { seconds: 3 } → "Restart 3") 오른쪽에서 왼쪽 언어면 넣는 값을 방향 격리 문자로 묶는다. (isolateText) 빗금으로만 이은 값들("{count}/{limit}")은 값마다가 아니라 통째로 한 번 묶는다. 값마다 묶으면 두 덩어리가 오른쪽부터 놓여 1/40 이 40/1 로 보인다.

Inserts values into a key's text. ("Restart {seconds}" + { seconds: 3 } → "Restart 3") For right-to-left languages, inserted values are wrapped with directional isolate characters. (isolateText) Values joined only by slashes ("{count}/{limit}") are wrapped once as a whole, not per value. Wrapping each value lays the two blocks out from the right, so 1/40 shows as 40/1.

keystring
글자의 열쇠.Key of the text.
valuesobject
자리 이름 → 값.Placeholder name → value.

반환Returnsstring 자리를 채운 글자.Text with placeholders filled.

짝Counterpart: vanilla-engine format_text

함수functionreadAllTexts()#0.4.0 부터since 0.4.0소스source

지금 언어의 글자 모두. (글꼴의 조각을 미리 받을 때 쓴다)

All text of the current language. (used to prefetch font subsets)

반환Returnsstring 지금 언어 표의 글자를 모두 이은 글.All texts of the current language table joined together.

짝Counterpart: vanilla-engine read_all_texts

함수functionsetLanguageList(data)#0.4.0 부터since 0.4.0소스source

언어의 목록 넣기. (파일 없이 바로 줄 때, languages.json 과 같은 꼴, 읽었으면 true)

  • loadLanguageList 가 읽은 파일도 여기로 넣는다. code 가 글이 아닌 줄은 빼고, aliases 와 countries 가 없으면 빈 배열, 글은 소문자로 맞춘다.

Sets the language list. (For giving it directly without a file, same shape as languages.json, true if set)

  • The file read by loadLanguageList also goes through here. Rows whose code is not a string are dropped, missing aliases and countries become empty arrays, and their strings are lowercased.
dataobject
{ fallbackLanguage, languages: [...] }

반환Returnsboolean 목록을 넣었으면 참. (languages 배열이 없으면 거짓)True if the list was set. (false if there is no languages array)

예제Examplealiases 가 없는 줄도 받기Accept rows without aliases
import { LanguagePack } from "./build/vanilla.js";

const isLanguageListSet = LanguagePack.setLanguageList({
	fallbackLanguage: "en",
	languages: [
		{ code: "en", name: "English", file: "en.json" },
		{ code: "pt", name: "Portugues", aliases: ["PT-BR"], file: "pt.json" }
	]
});
const englishLanguage = LanguagePack.matchLanguageTag("en-US");
const portugueseLanguage = LanguagePack.matchLanguageTag("pt_BR");
console.log(isLanguageListSet, englishLanguage.code, englishLanguage.aliases.length, portugueseLanguage.code);
함수functionsetLanguageTexts(languageCode, texts, fallbackTextTable)#0.4.0 부터since 0.4.0소스source

지금 언어의 표 넣기. (파일 없이 바로 줄 때, 기본 언어 표도 함께. 언어가 목록에 없으면 false)

Sets the current language's table. (when given directly without a file, together with the default language table. false if the language is not in the list)

languageCodestring
지금 언어로 정할 코드. (목록에 있어야 한다)Code to set as the current language. (must be in the list)
textsobject
열쇠 → 글자.Key → text.
fallbackTextTableobject
기본 언어의 열쇠 → 글자.Default language key → text.

반환Returnsboolean 넣었으면 참. 언어가 목록에 없으면 거짓.True if set. False if the language is not in the list.

함수functionsetLanguagePaths(listPath, directoryPath)#0.4.0 부터since 0.4.0소스source

경로 정하기. (언어 목록 파일, 언어 파일들이 있는 폴더, 불러오기 전에 부른다)

Sets the paths. (language list file, folder containing the language files, call before loading)

listPathstring
언어 목록 파일(languages.json) 경로.Path of the language list file (languages.json).
directoryPathstring
"/" 로 끝난다.Ends with "/".

짝Counterpart: vanilla-engine set_language_paths

함수functionsetDocumentTitleKey(titleKey)#0.4.0 부터since 0.4.0소스source

탭 제목이 될 글자의 열쇠 정하기. ("" 이면 탭 제목을 바꾸지 않는다)

Sets the key of the text used as the tab title. ("" leaves the tab title unchanged)

titleKeystring
탭 제목이 될 글자의 열쇠. ("" 이면 탭 제목을 바꾸지 않는다)Key of the text that becomes the tab title. (the tab title is not changed if "")

짝Counterpart: vanilla-engine set_document_title_key

함수functionsetLanguageLoadedHandler(handler)#0.4.0 부터since 0.4.0소스source

언어를 불러온 뒤 부를 함수 정하기. (applyLanguageSetting 이 부른다, 그 언어의 글꼴 받기 등, null 이면 없음)

Sets the function to call after a language is loaded. (called by applyLanguageSetting, e.g. to fetch that language's font, null for none)

handlerfunction(object, string): Promise<void> | void | null
(언어의 한 줄, 모든 글자)(a row of the language, all text)

짝Counterpart: vanilla-engine set_language_loaded_handler